~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/nfs/file.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/fs/nfs/file.c
  3  *
  4  *  Copyright (C) 1992  Rick Sladkey
  5  *
  6  *  Changes Copyright (C) 1994 by Florian La Roche
  7  *   - Do not copy data too often around in the kernel.
  8  *   - In nfs_file_read the return value of kmalloc wasn't checked.
  9  *   - Put in a better version of read look-ahead buffering. Original idea
 10  *     and implementation by Wai S Kok elekokws@ee.nus.sg.
 11  *
 12  *  Expire cache on write to a file by Wai S Kok (Oct 1994).
 13  *
 14  *  Total rewrite of read side for new NFS buffer cache.. Linus.
 15  *
 16  *  nfs regular file handling functions
 17  */
 18 
 19 #include <linux/sched.h>
 20 #include <linux/kernel.h>
 21 #include <linux/errno.h>
 22 #include <linux/fcntl.h>
 23 #include <linux/stat.h>
 24 #include <linux/nfs_fs.h>
 25 #include <linux/nfs_mount.h>
 26 #include <linux/mm.h>
 27 #include <linux/malloc.h>
 28 #include <linux/pagemap.h>
 29 #include <linux/lockd/bind.h>
 30 #include <linux/smp_lock.h>
 31 
 32 #include <asm/uaccess.h>
 33 #include <asm/system.h>
 34 
 35 #define NFSDBG_FACILITY         NFSDBG_FILE
 36 
 37 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
 38 static ssize_t nfs_file_read(struct file *, char *, size_t, loff_t *);
 39 static ssize_t nfs_file_write(struct file *, const char *, size_t, loff_t *);
 40 static int  nfs_file_flush(struct file *);
 41 static int  nfs_fsync(struct file *, struct dentry *dentry, int datasync);
 42 
 43 struct file_operations nfs_file_operations = {
 44         read:           nfs_file_read,
 45         write:          nfs_file_write,
 46         mmap:           nfs_file_mmap,
 47         open:           nfs_open,
 48         flush:          nfs_file_flush,
 49         release:        nfs_release,
 50         fsync:          nfs_fsync,
 51         lock:           nfs_lock,
 52 };
 53 
 54 struct inode_operations nfs_file_inode_operations = {
 55         permission:     nfs_permission,
 56         revalidate:     nfs_revalidate,
 57         setattr:        nfs_notify_change,
 58 };
 59 
 60 /* Hack for future NFS swap support */
 61 #ifndef IS_SWAPFILE
 62 # define IS_SWAPFILE(inode)     (0)
 63 #endif
 64 
 65 /*
 66  * Flush all dirty pages, and check for write errors.
 67  *
 68  */
 69 static int
 70 nfs_file_flush(struct file *file)
 71 {
 72         struct inode    *inode = file->f_dentry->d_inode;
 73         int             status;
 74 
 75         dfprintk(VFS, "nfs: flush(%x/%ld)\n", inode->i_dev, inode->i_ino);
 76 
 77         /* Make sure all async reads have been sent off. We don't bother
 78          * waiting on them though... */
 79         if (file->f_mode & FMODE_READ)
 80                 nfs_pagein_inode(inode, 0, 0);
 81 
 82         status = nfs_wb_file(inode, file);
 83         if (!status) {
 84                 status = file->f_error;
 85                 file->f_error = 0;
 86         }
 87         return status;
 88 }
 89 
 90 static ssize_t
 91 nfs_file_read(struct file * file, char * buf, size_t count, loff_t *ppos)
 92 {
 93         struct dentry * dentry = file->f_dentry;
 94         struct inode * inode = dentry->d_inode;
 95         ssize_t result;
 96 
 97         dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n",
 98                 dentry->d_parent->d_name.name, dentry->d_name.name,
 99                 (unsigned long) count, (unsigned long) *ppos);
100 
101         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
102         if (!result)
103                 result = generic_file_read(file, buf, count, ppos);
104         return result;
105 }
106 
107 static int
108 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
109 {
110         struct dentry *dentry = file->f_dentry;
111         struct inode *inode = dentry->d_inode;
112         int     status;
113 
114         dfprintk(VFS, "nfs: mmap(%s/%s)\n",
115                 dentry->d_parent->d_name.name, dentry->d_name.name);
116 
117         status = nfs_revalidate_inode(NFS_SERVER(inode), inode);
118         if (!status)
119                 status = generic_file_mmap(file, vma);
120         return status;
121 }
122 
123 /*
124  * Flush any dirty pages for this process, and check for write errors.
125  * The return status from this call provides a reliable indication of
126  * whether any write errors occurred for this process.
127  */
128 static int
129 nfs_fsync(struct file *file, struct dentry *dentry, int datasync)
130 {
131         struct inode *inode = dentry->d_inode;
132         int status;
133 
134         dfprintk(VFS, "nfs: fsync(%x/%ld)\n", inode->i_dev, inode->i_ino);
135 
136         lock_kernel();
137         status = nfs_wb_file(inode, file);
138         if (!status) {
139                 status = file->f_error;
140                 file->f_error = 0;
141         }
142         unlock_kernel();
143         return status;
144 }
145 
146 /*
147  * This does the "real" work of the write. The generic routine has
148  * allocated the page, locked it, done all the page alignment stuff
149  * calculations etc. Now we should just copy the data from user
150  * space and write it back to the real medium..
151  *
152  * If the writer ends up delaying the write, the writer needs to
153  * increment the page use counts until he is done with the page.
154  */
155 static int nfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
156 {
157         kmap(page);
158         return nfs_flush_incompatible(file, page);
159 }
160 static int nfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
161 {
162         long status;
163         loff_t pos = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + to;
164         struct inode *inode = page->mapping->host;
165 
166         kunmap(page);
167         lock_kernel();
168         status = nfs_updatepage(file, page, offset, to-offset);
169         unlock_kernel();
170         /* most likely it's already done. CHECKME */
171         if (pos > inode->i_size)
172                 inode->i_size = pos;
173         return status;
174 }
175 
176 /*
177  * The following is used by wait_on_page(), generic_file_readahead()
178  * to initiate the completion of any page readahead operations.
179  */
180 static int nfs_sync_page(struct page *page)
181 {
182         struct address_space *mapping;
183         struct inode    *inode;
184         unsigned long   index = page_index(page);
185         unsigned int    rpages;
186         int             result;
187 
188         mapping = page->mapping;
189         if (!mapping)
190                 return 0;
191         inode = mapping->host;
192         if (!inode)
193                 return 0;
194 
195         rpages = NFS_SERVER(inode)->rpages;
196         result = nfs_pagein_inode(inode, index, rpages);
197         if (result < 0)
198                 return result;
199         return 0;
200 }
201 
202 struct address_space_operations nfs_file_aops = {
203         readpage: nfs_readpage,
204         sync_page: nfs_sync_page,
205         writepage: nfs_writepage,
206         prepare_write: nfs_prepare_write,
207         commit_write: nfs_commit_write
208 };
209 
210 /* 
211  * Write to a file (through the page cache).
212  */
213 static ssize_t
214 nfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
215 {
216         struct dentry * dentry = file->f_dentry;
217         struct inode * inode = dentry->d_inode;
218         ssize_t result;
219 
220         dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%lu)\n",
221                 dentry->d_parent->d_name.name, dentry->d_name.name,
222                 inode->i_ino, (unsigned long) count, (unsigned long) *ppos);
223 
224         result = -EBUSY;
225         if (IS_SWAPFILE(inode))
226                 goto out_swapfile;
227         result = nfs_revalidate_inode(NFS_SERVER(inode), inode);
228         if (result)
229                 goto out;
230 
231         result = count;
232         if (!count)
233                 goto out;
234 
235         result = generic_file_write(file, buf, count, ppos);
236 out:
237         return result;
238 
239 out_swapfile:
240         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
241         goto out;
242 }
243 
244 /*
245  * Lock a (portion of) a file
246  */
247 int
248 nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
249 {
250         struct inode * inode = filp->f_dentry->d_inode;
251         int     status = 0;
252 
253         dprintk("NFS: nfs_lock(f=%4x/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n",
254                         inode->i_dev, inode->i_ino,
255                         fl->fl_type, fl->fl_flags,
256                         (long long)fl->fl_start, (long long)fl->fl_end);
257 
258         if (!inode)
259                 return -EINVAL;
260 
261         /* No mandatory locks over NFS */
262         if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
263                 return -ENOLCK;
264 
265         /* Fake OK code if mounted without NLM support */
266         if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM) {
267                 if (cmd == F_GETLK)
268                         status = LOCK_USE_CLNT;
269                 goto out_ok;
270         }
271 
272         /*
273          * No BSD flocks over NFS allowed.
274          * Note: we could try to fake a POSIX lock request here by
275          * using ((u32) filp | 0x80000000) or some such as the pid.
276          * Not sure whether that would be unique, though, or whether
277          * that would break in other places.
278          */
279         if (!fl->fl_owner || (fl->fl_flags & (FL_POSIX|FL_BROKEN)) != FL_POSIX)
280                 return -ENOLCK;
281 
282         /*
283          * Flush all pending writes before doing anything
284          * with locks..
285          */
286         down(&filp->f_dentry->d_inode->i_sem);
287         status = nfs_wb_all(inode);
288         up(&filp->f_dentry->d_inode->i_sem);
289         if (status < 0)
290                 return status;
291 
292         if ((status = nlmclnt_proc(inode, cmd, fl)) < 0)
293                 return status;
294         else
295                 status = 0;
296 
297         /*
298          * Make sure we clear the cache whenever we try to get the lock.
299          * This makes locking act as a cache coherency point.
300          */
301  out_ok:
302         if ((cmd == F_SETLK || cmd == F_SETLKW) && fl->fl_type != F_UNLCK) {
303                 down(&filp->f_dentry->d_inode->i_sem);
304                 nfs_wb_all(inode);      /* we may have slept */
305                 nfs_zap_caches(inode);
306                 up(&filp->f_dentry->d_inode->i_sem);
307         }
308         return status;
309 }
310 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.