Merge branch 'patches_cel-for-2.6.32' into nfs-for-2.6.32
[pandora-kernel.git] / fs / nfs / inode.c
1 /*
2  *  linux/fs/nfs/inode.c
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  nfs inode and superblock handling functions
7  *
8  *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9  *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
10  *
11  *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12  *  J.S.Peatfield@damtp.cam.ac.uk
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/time.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/stat.h>
24 #include <linux/errno.h>
25 #include <linux/unistd.h>
26 #include <linux/sunrpc/clnt.h>
27 #include <linux/sunrpc/stats.h>
28 #include <linux/sunrpc/metrics.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/seq_file.h>
34 #include <linux/mount.h>
35 #include <linux/nfs_idmap.h>
36 #include <linux/vfs.h>
37 #include <linux/inet.h>
38 #include <linux/nfs_xdr.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47 #include "internal.h"
48 #include "fscache.h"
49
50 #define NFSDBG_FACILITY         NFSDBG_VFS
51
52 #define NFS_64_BIT_INODE_NUMBERS_ENABLED        1
53
54 /* Default is to see 64-bit inode numbers */
55 static int enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
56
57 static void nfs_invalidate_inode(struct inode *);
58 static int nfs_update_inode(struct inode *, struct nfs_fattr *);
59
60 static struct kmem_cache * nfs_inode_cachep;
61
62 static inline unsigned long
63 nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
64 {
65         return nfs_fileid_to_ino_t(fattr->fileid);
66 }
67
68 /**
69  * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
70  * @word: long word containing the bit lock
71  */
72 int nfs_wait_bit_killable(void *word)
73 {
74         if (fatal_signal_pending(current))
75                 return -ERESTARTSYS;
76         schedule();
77         return 0;
78 }
79
80 /**
81  * nfs_compat_user_ino64 - returns the user-visible inode number
82  * @fileid: 64-bit fileid
83  *
84  * This function returns a 32-bit inode number if the boot parameter
85  * nfs.enable_ino64 is zero.
86  */
87 u64 nfs_compat_user_ino64(u64 fileid)
88 {
89         int ino;
90
91         if (enable_ino64)
92                 return fileid;
93         ino = fileid;
94         if (sizeof(ino) < sizeof(fileid))
95                 ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
96         return ino;
97 }
98
99 int nfs_write_inode(struct inode *inode, int sync)
100 {
101         int ret;
102
103         if (sync) {
104                 ret = filemap_fdatawait(inode->i_mapping);
105                 if (ret == 0)
106                         ret = nfs_commit_inode(inode, FLUSH_SYNC);
107         } else
108                 ret = nfs_commit_inode(inode, 0);
109         if (ret >= 0)
110                 return 0;
111         __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
112         return ret;
113 }
114
115 void nfs_clear_inode(struct inode *inode)
116 {
117         /*
118          * The following should never happen...
119          */
120         BUG_ON(nfs_have_writebacks(inode));
121         BUG_ON(!list_empty(&NFS_I(inode)->open_files));
122         nfs_zap_acl_cache(inode);
123         nfs_access_zap_cache(inode);
124         nfs_fscache_release_inode_cookie(inode);
125 }
126
127 /**
128  * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
129  */
130 int nfs_sync_mapping(struct address_space *mapping)
131 {
132         int ret;
133
134         if (mapping->nrpages == 0)
135                 return 0;
136         unmap_mapping_range(mapping, 0, 0, 0);
137         ret = filemap_write_and_wait(mapping);
138         if (ret != 0)
139                 goto out;
140         ret = nfs_wb_all(mapping->host);
141 out:
142         return ret;
143 }
144
145 /*
146  * Invalidate the local caches
147  */
148 static void nfs_zap_caches_locked(struct inode *inode)
149 {
150         struct nfs_inode *nfsi = NFS_I(inode);
151         int mode = inode->i_mode;
152
153         nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
154
155         nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
156         nfsi->attrtimeo_timestamp = jiffies;
157
158         memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
159         if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
160                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
161         else
162                 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
163 }
164
165 void nfs_zap_caches(struct inode *inode)
166 {
167         spin_lock(&inode->i_lock);
168         nfs_zap_caches_locked(inode);
169         spin_unlock(&inode->i_lock);
170 }
171
172 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
173 {
174         if (mapping->nrpages != 0) {
175                 spin_lock(&inode->i_lock);
176                 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
177                 spin_unlock(&inode->i_lock);
178         }
179 }
180
181 void nfs_zap_acl_cache(struct inode *inode)
182 {
183         void (*clear_acl_cache)(struct inode *);
184
185         clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
186         if (clear_acl_cache != NULL)
187                 clear_acl_cache(inode);
188         spin_lock(&inode->i_lock);
189         NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
190         spin_unlock(&inode->i_lock);
191 }
192
193 void nfs_invalidate_atime(struct inode *inode)
194 {
195         spin_lock(&inode->i_lock);
196         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
197         spin_unlock(&inode->i_lock);
198 }
199
200 /*
201  * Invalidate, but do not unhash, the inode.
202  * NB: must be called with inode->i_lock held!
203  */
204 static void nfs_invalidate_inode(struct inode *inode)
205 {
206         set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
207         nfs_zap_caches_locked(inode);
208 }
209
210 struct nfs_find_desc {
211         struct nfs_fh           *fh;
212         struct nfs_fattr        *fattr;
213 };
214
215 /*
216  * In NFSv3 we can have 64bit inode numbers. In order to support
217  * this, and re-exported directories (also seen in NFSv2)
218  * we are forced to allow 2 different inodes to have the same
219  * i_ino.
220  */
221 static int
222 nfs_find_actor(struct inode *inode, void *opaque)
223 {
224         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
225         struct nfs_fh           *fh = desc->fh;
226         struct nfs_fattr        *fattr = desc->fattr;
227
228         if (NFS_FILEID(inode) != fattr->fileid)
229                 return 0;
230         if (nfs_compare_fh(NFS_FH(inode), fh))
231                 return 0;
232         if (is_bad_inode(inode) || NFS_STALE(inode))
233                 return 0;
234         return 1;
235 }
236
237 static int
238 nfs_init_locked(struct inode *inode, void *opaque)
239 {
240         struct nfs_find_desc    *desc = (struct nfs_find_desc *)opaque;
241         struct nfs_fattr        *fattr = desc->fattr;
242
243         set_nfs_fileid(inode, fattr->fileid);
244         nfs_copy_fh(NFS_FH(inode), desc->fh);
245         return 0;
246 }
247
248 /* Don't use READDIRPLUS on directories that we believe are too large */
249 #define NFS_LIMIT_READDIRPLUS (8*PAGE_SIZE)
250
251 /*
252  * This is our front-end to iget that looks up inodes by file handle
253  * instead of inode number.
254  */
255 struct inode *
256 nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
257 {
258         struct nfs_find_desc desc = {
259                 .fh     = fh,
260                 .fattr  = fattr
261         };
262         struct inode *inode = ERR_PTR(-ENOENT);
263         unsigned long hash;
264
265         if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0)
266                 goto out_no_inode;
267         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
268                 goto out_no_inode;
269
270         hash = nfs_fattr_to_ino_t(fattr);
271
272         inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
273         if (inode == NULL) {
274                 inode = ERR_PTR(-ENOMEM);
275                 goto out_no_inode;
276         }
277
278         if (inode->i_state & I_NEW) {
279                 struct nfs_inode *nfsi = NFS_I(inode);
280                 unsigned long now = jiffies;
281
282                 /* We set i_ino for the few things that still rely on it,
283                  * such as stat(2) */
284                 inode->i_ino = hash;
285
286                 /* We can't support update_atime(), since the server will reset it */
287                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
288                 inode->i_mode = fattr->mode;
289                 if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
290                                 && nfs_server_capable(inode, NFS_CAP_MODE))
291                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
292                                 | NFS_INO_INVALID_ACCESS
293                                 | NFS_INO_INVALID_ACL;
294                 /* Why so? Because we want revalidate for devices/FIFOs, and
295                  * that's precisely what we have in nfs_file_inode_operations.
296                  */
297                 inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
298                 if (S_ISREG(inode->i_mode)) {
299                         inode->i_fop = &nfs_file_operations;
300                         inode->i_data.a_ops = &nfs_file_aops;
301                         inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
302                 } else if (S_ISDIR(inode->i_mode)) {
303                         inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
304                         inode->i_fop = &nfs_dir_operations;
305                         if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS)
306                             && fattr->size <= NFS_LIMIT_READDIRPLUS)
307                                 set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
308                         /* Deal with crossing mountpoints */
309                         if ((fattr->valid & NFS_ATTR_FATTR_FSID)
310                                         && !nfs_fsid_equal(&NFS_SB(sb)->fsid, &fattr->fsid)) {
311                                 if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
312                                         inode->i_op = &nfs_referral_inode_operations;
313                                 else
314                                         inode->i_op = &nfs_mountpoint_inode_operations;
315                                 inode->i_fop = NULL;
316                                 set_bit(NFS_INO_MOUNTPOINT, &nfsi->flags);
317                         }
318                 } else if (S_ISLNK(inode->i_mode))
319                         inode->i_op = &nfs_symlink_inode_operations;
320                 else
321                         init_special_inode(inode, inode->i_mode, fattr->rdev);
322
323                 memset(&inode->i_atime, 0, sizeof(inode->i_atime));
324                 memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
325                 memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
326                 nfsi->change_attr = 0;
327                 inode->i_size = 0;
328                 inode->i_nlink = 0;
329                 inode->i_uid = -2;
330                 inode->i_gid = -2;
331                 inode->i_blocks = 0;
332                 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
333
334                 nfsi->read_cache_jiffies = fattr->time_start;
335                 nfsi->attr_gencount = fattr->gencount;
336                 if (fattr->valid & NFS_ATTR_FATTR_ATIME)
337                         inode->i_atime = fattr->atime;
338                 else if (nfs_server_capable(inode, NFS_CAP_ATIME))
339                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
340                 if (fattr->valid & NFS_ATTR_FATTR_MTIME)
341                         inode->i_mtime = fattr->mtime;
342                 else if (nfs_server_capable(inode, NFS_CAP_MTIME))
343                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
344                                 | NFS_INO_INVALID_DATA;
345                 if (fattr->valid & NFS_ATTR_FATTR_CTIME)
346                         inode->i_ctime = fattr->ctime;
347                 else if (nfs_server_capable(inode, NFS_CAP_CTIME))
348                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
349                                 | NFS_INO_INVALID_ACCESS
350                                 | NFS_INO_INVALID_ACL;
351                 if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
352                         nfsi->change_attr = fattr->change_attr;
353                 else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
354                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
355                                 | NFS_INO_INVALID_DATA;
356                 if (fattr->valid & NFS_ATTR_FATTR_SIZE)
357                         inode->i_size = nfs_size_to_loff_t(fattr->size);
358                 else
359                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
360                                 | NFS_INO_INVALID_DATA
361                                 | NFS_INO_REVAL_PAGECACHE;
362                 if (fattr->valid & NFS_ATTR_FATTR_NLINK)
363                         inode->i_nlink = fattr->nlink;
364                 else if (nfs_server_capable(inode, NFS_CAP_NLINK))
365                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
366                 if (fattr->valid & NFS_ATTR_FATTR_OWNER)
367                         inode->i_uid = fattr->uid;
368                 else if (nfs_server_capable(inode, NFS_CAP_OWNER))
369                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
370                                 | NFS_INO_INVALID_ACCESS
371                                 | NFS_INO_INVALID_ACL;
372                 if (fattr->valid & NFS_ATTR_FATTR_GROUP)
373                         inode->i_gid = fattr->gid;
374                 else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
375                         nfsi->cache_validity |= NFS_INO_INVALID_ATTR
376                                 | NFS_INO_INVALID_ACCESS
377                                 | NFS_INO_INVALID_ACL;
378                 if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
379                         inode->i_blocks = fattr->du.nfs2.blocks;
380                 if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
381                         /*
382                          * report the blocks in 512byte units
383                          */
384                         inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
385                 }
386                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
387                 nfsi->attrtimeo_timestamp = now;
388                 nfsi->access_cache = RB_ROOT;
389
390                 nfs_fscache_init_inode_cookie(inode);
391
392                 unlock_new_inode(inode);
393         } else
394                 nfs_refresh_inode(inode, fattr);
395         dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
396                 inode->i_sb->s_id,
397                 (long long)NFS_FILEID(inode),
398                 atomic_read(&inode->i_count));
399
400 out:
401         return inode;
402
403 out_no_inode:
404         dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
405         goto out;
406 }
407
408 #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
409
410 int
411 nfs_setattr(struct dentry *dentry, struct iattr *attr)
412 {
413         struct inode *inode = dentry->d_inode;
414         struct nfs_fattr fattr;
415         int error;
416
417         nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
418
419         /* skip mode change if it's just for clearing setuid/setgid */
420         if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
421                 attr->ia_valid &= ~ATTR_MODE;
422
423         if (attr->ia_valid & ATTR_SIZE) {
424                 if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
425                         attr->ia_valid &= ~ATTR_SIZE;
426         }
427
428         /* Optimization: if the end result is no change, don't RPC */
429         attr->ia_valid &= NFS_VALID_ATTRS;
430         if ((attr->ia_valid & ~ATTR_FILE) == 0)
431                 return 0;
432
433         /* Write all dirty data */
434         if (S_ISREG(inode->i_mode)) {
435                 filemap_write_and_wait(inode->i_mapping);
436                 nfs_wb_all(inode);
437         }
438         /*
439          * Return any delegations if we're going to change ACLs
440          */
441         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
442                 nfs_inode_return_delegation(inode);
443         error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
444         if (error == 0)
445                 nfs_refresh_inode(inode, &fattr);
446         return error;
447 }
448
449 /**
450  * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
451  * @inode: inode of the file used
452  * @offset: file offset to start truncating
453  *
454  * This is a copy of the common vmtruncate, but with the locking
455  * corrected to take into account the fact that NFS requires
456  * inode->i_size to be updated under the inode->i_lock.
457  */
458 static int nfs_vmtruncate(struct inode * inode, loff_t offset)
459 {
460         if (i_size_read(inode) < offset) {
461                 unsigned long limit;
462
463                 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
464                 if (limit != RLIM_INFINITY && offset > limit)
465                         goto out_sig;
466                 if (offset > inode->i_sb->s_maxbytes)
467                         goto out_big;
468                 spin_lock(&inode->i_lock);
469                 i_size_write(inode, offset);
470                 spin_unlock(&inode->i_lock);
471         } else {
472                 struct address_space *mapping = inode->i_mapping;
473
474                 /*
475                  * truncation of in-use swapfiles is disallowed - it would
476                  * cause subsequent swapout to scribble on the now-freed
477                  * blocks.
478                  */
479                 if (IS_SWAPFILE(inode))
480                         return -ETXTBSY;
481                 spin_lock(&inode->i_lock);
482                 i_size_write(inode, offset);
483                 spin_unlock(&inode->i_lock);
484
485                 /*
486                  * unmap_mapping_range is called twice, first simply for
487                  * efficiency so that truncate_inode_pages does fewer
488                  * single-page unmaps.  However after this first call, and
489                  * before truncate_inode_pages finishes, it is possible for
490                  * private pages to be COWed, which remain after
491                  * truncate_inode_pages finishes, hence the second
492                  * unmap_mapping_range call must be made for correctness.
493                  */
494                 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
495                 truncate_inode_pages(mapping, offset);
496                 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
497         }
498         return 0;
499 out_sig:
500         send_sig(SIGXFSZ, current, 0);
501 out_big:
502         return -EFBIG;
503 }
504
505 /**
506  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
507  * @inode: pointer to struct inode
508  * @attr: pointer to struct iattr
509  *
510  * Note: we do this in the *proc.c in order to ensure that
511  *       it works for things like exclusive creates too.
512  */
513 void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
514 {
515         if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
516                 spin_lock(&inode->i_lock);
517                 if ((attr->ia_valid & ATTR_MODE) != 0) {
518                         int mode = attr->ia_mode & S_IALLUGO;
519                         mode |= inode->i_mode & ~S_IALLUGO;
520                         inode->i_mode = mode;
521                 }
522                 if ((attr->ia_valid & ATTR_UID) != 0)
523                         inode->i_uid = attr->ia_uid;
524                 if ((attr->ia_valid & ATTR_GID) != 0)
525                         inode->i_gid = attr->ia_gid;
526                 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
527                 spin_unlock(&inode->i_lock);
528         }
529         if ((attr->ia_valid & ATTR_SIZE) != 0) {
530                 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
531                 nfs_vmtruncate(inode, attr->ia_size);
532         }
533 }
534
535 int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
536 {
537         struct inode *inode = dentry->d_inode;
538         int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
539         int err;
540
541         /*
542          * Flush out writes to the server in order to update c/mtime.
543          *
544          * Hold the i_mutex to suspend application writes temporarily;
545          * this prevents long-running writing applications from blocking
546          * nfs_wb_nocommit.
547          */
548         if (S_ISREG(inode->i_mode)) {
549                 mutex_lock(&inode->i_mutex);
550                 nfs_wb_nocommit(inode);
551                 mutex_unlock(&inode->i_mutex);
552         }
553
554         /*
555          * We may force a getattr if the user cares about atime.
556          *
557          * Note that we only have to check the vfsmount flags here:
558          *  - NFS always sets S_NOATIME by so checking it would give a
559          *    bogus result
560          *  - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
561          *    no point in checking those.
562          */
563         if ((mnt->mnt_flags & MNT_NOATIME) ||
564             ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
565                 need_atime = 0;
566
567         if (need_atime)
568                 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
569         else
570                 err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
571         if (!err) {
572                 generic_fillattr(inode, stat);
573                 stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
574         }
575         return err;
576 }
577
578 /**
579  * nfs_close_context - Common close_context() routine NFSv2/v3
580  * @ctx: pointer to context
581  * @is_sync: is this a synchronous close
582  *
583  * always ensure that the attributes are up to date if we're mounted
584  * with close-to-open semantics
585  */
586 void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
587 {
588         struct inode *inode;
589         struct nfs_server *server;
590
591         if (!(ctx->mode & FMODE_WRITE))
592                 return;
593         if (!is_sync)
594                 return;
595         inode = ctx->path.dentry->d_inode;
596         if (!list_empty(&NFS_I(inode)->open_files))
597                 return;
598         server = NFS_SERVER(inode);
599         if (server->flags & NFS_MOUNT_NOCTO)
600                 return;
601         nfs_revalidate_inode(server, inode);
602 }
603
604 static struct nfs_open_context *alloc_nfs_open_context(struct vfsmount *mnt, struct dentry *dentry, struct rpc_cred *cred)
605 {
606         struct nfs_open_context *ctx;
607
608         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
609         if (ctx != NULL) {
610                 ctx->path.dentry = dget(dentry);
611                 ctx->path.mnt = mntget(mnt);
612                 ctx->cred = get_rpccred(cred);
613                 ctx->state = NULL;
614                 ctx->lockowner = current->files;
615                 ctx->flags = 0;
616                 ctx->error = 0;
617                 ctx->dir_cookie = 0;
618                 atomic_set(&ctx->count, 1);
619         }
620         return ctx;
621 }
622
623 struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
624 {
625         if (ctx != NULL)
626                 atomic_inc(&ctx->count);
627         return ctx;
628 }
629
630 static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
631 {
632         struct inode *inode = ctx->path.dentry->d_inode;
633
634         if (!atomic_dec_and_lock(&ctx->count, &inode->i_lock))
635                 return;
636         list_del(&ctx->list);
637         spin_unlock(&inode->i_lock);
638         NFS_PROTO(inode)->close_context(ctx, is_sync);
639         if (ctx->cred != NULL)
640                 put_rpccred(ctx->cred);
641         path_put(&ctx->path);
642         kfree(ctx);
643 }
644
645 void put_nfs_open_context(struct nfs_open_context *ctx)
646 {
647         __put_nfs_open_context(ctx, 0);
648 }
649
650 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
651 {
652         __put_nfs_open_context(ctx, 1);
653 }
654
655 /*
656  * Ensure that mmap has a recent RPC credential for use when writing out
657  * shared pages
658  */
659 static void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
660 {
661         struct inode *inode = filp->f_path.dentry->d_inode;
662         struct nfs_inode *nfsi = NFS_I(inode);
663
664         filp->private_data = get_nfs_open_context(ctx);
665         spin_lock(&inode->i_lock);
666         list_add(&ctx->list, &nfsi->open_files);
667         spin_unlock(&inode->i_lock);
668 }
669
670 /*
671  * Given an inode, search for an open context with the desired characteristics
672  */
673 struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
674 {
675         struct nfs_inode *nfsi = NFS_I(inode);
676         struct nfs_open_context *pos, *ctx = NULL;
677
678         spin_lock(&inode->i_lock);
679         list_for_each_entry(pos, &nfsi->open_files, list) {
680                 if (cred != NULL && pos->cred != cred)
681                         continue;
682                 if ((pos->mode & mode) == mode) {
683                         ctx = get_nfs_open_context(pos);
684                         break;
685                 }
686         }
687         spin_unlock(&inode->i_lock);
688         return ctx;
689 }
690
691 static void nfs_file_clear_open_context(struct file *filp)
692 {
693         struct inode *inode = filp->f_path.dentry->d_inode;
694         struct nfs_open_context *ctx = nfs_file_open_context(filp);
695
696         if (ctx) {
697                 filp->private_data = NULL;
698                 spin_lock(&inode->i_lock);
699                 list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
700                 spin_unlock(&inode->i_lock);
701                 put_nfs_open_context_sync(ctx);
702         }
703 }
704
705 /*
706  * These allocate and release file read/write context information.
707  */
708 int nfs_open(struct inode *inode, struct file *filp)
709 {
710         struct nfs_open_context *ctx;
711         struct rpc_cred *cred;
712
713         cred = rpc_lookup_cred();
714         if (IS_ERR(cred))
715                 return PTR_ERR(cred);
716         ctx = alloc_nfs_open_context(filp->f_path.mnt, filp->f_path.dentry, cred);
717         put_rpccred(cred);
718         if (ctx == NULL)
719                 return -ENOMEM;
720         ctx->mode = filp->f_mode;
721         nfs_file_set_open_context(filp, ctx);
722         put_nfs_open_context(ctx);
723         nfs_fscache_set_inode_cookie(inode, filp);
724         return 0;
725 }
726
727 int nfs_release(struct inode *inode, struct file *filp)
728 {
729         nfs_file_clear_open_context(filp);
730         return 0;
731 }
732
733 /*
734  * This function is called whenever some part of NFS notices that
735  * the cached attributes have to be refreshed.
736  */
737 int
738 __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
739 {
740         int              status = -ESTALE;
741         struct nfs_fattr fattr;
742         struct nfs_inode *nfsi = NFS_I(inode);
743
744         dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
745                 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
746
747         if (is_bad_inode(inode))
748                 goto out;
749         if (NFS_STALE(inode))
750                 goto out;
751
752         nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
753         status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), &fattr);
754         if (status != 0) {
755                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
756                          inode->i_sb->s_id,
757                          (long long)NFS_FILEID(inode), status);
758                 if (status == -ESTALE) {
759                         nfs_zap_caches(inode);
760                         if (!S_ISDIR(inode->i_mode))
761                                 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
762                 }
763                 goto out;
764         }
765
766         status = nfs_refresh_inode(inode, &fattr);
767         if (status) {
768                 dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
769                          inode->i_sb->s_id,
770                          (long long)NFS_FILEID(inode), status);
771                 goto out;
772         }
773
774         if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
775                 nfs_zap_acl_cache(inode);
776
777         dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
778                 inode->i_sb->s_id,
779                 (long long)NFS_FILEID(inode));
780
781  out:
782         return status;
783 }
784
785 int nfs_attribute_timeout(struct inode *inode)
786 {
787         struct nfs_inode *nfsi = NFS_I(inode);
788
789         if (nfs_have_delegation(inode, FMODE_READ))
790                 return 0;
791         return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
792 }
793
794 /**
795  * nfs_revalidate_inode - Revalidate the inode attributes
796  * @server - pointer to nfs_server struct
797  * @inode - pointer to inode struct
798  *
799  * Updates inode attribute information by retrieving the data from the server.
800  */
801 int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
802 {
803         if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
804                         && !nfs_attribute_timeout(inode))
805                 return NFS_STALE(inode) ? -ESTALE : 0;
806         return __nfs_revalidate_inode(server, inode);
807 }
808
809 static int nfs_invalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
810 {
811         struct nfs_inode *nfsi = NFS_I(inode);
812         
813         if (mapping->nrpages != 0) {
814                 int ret = invalidate_inode_pages2(mapping);
815                 if (ret < 0)
816                         return ret;
817         }
818         spin_lock(&inode->i_lock);
819         nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
820         if (S_ISDIR(inode->i_mode))
821                 memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
822         spin_unlock(&inode->i_lock);
823         nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
824         nfs_fscache_reset_inode_cookie(inode);
825         dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
826                         inode->i_sb->s_id, (long long)NFS_FILEID(inode));
827         return 0;
828 }
829
830 static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
831 {
832         int ret = 0;
833
834         mutex_lock(&inode->i_mutex);
835         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_DATA) {
836                 ret = nfs_sync_mapping(mapping);
837                 if (ret == 0)
838                         ret = nfs_invalidate_mapping_nolock(inode, mapping);
839         }
840         mutex_unlock(&inode->i_mutex);
841         return ret;
842 }
843
844 /**
845  * nfs_revalidate_mapping_nolock - Revalidate the pagecache
846  * @inode - pointer to host inode
847  * @mapping - pointer to mapping
848  */
849 int nfs_revalidate_mapping_nolock(struct inode *inode, struct address_space *mapping)
850 {
851         struct nfs_inode *nfsi = NFS_I(inode);
852         int ret = 0;
853
854         if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
855                         || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
856                 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
857                 if (ret < 0)
858                         goto out;
859         }
860         if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
861                 ret = nfs_invalidate_mapping_nolock(inode, mapping);
862 out:
863         return ret;
864 }
865
866 /**
867  * nfs_revalidate_mapping - Revalidate the pagecache
868  * @inode - pointer to host inode
869  * @mapping - pointer to mapping
870  *
871  * This version of the function will take the inode->i_mutex and attempt to
872  * flush out all dirty data if it needs to invalidate the page cache.
873  */
874 int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
875 {
876         struct nfs_inode *nfsi = NFS_I(inode);
877         int ret = 0;
878
879         if ((nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
880                         || nfs_attribute_timeout(inode) || NFS_STALE(inode)) {
881                 ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
882                 if (ret < 0)
883                         goto out;
884         }
885         if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
886                 ret = nfs_invalidate_mapping(inode, mapping);
887 out:
888         return ret;
889 }
890
891 static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
892 {
893         struct nfs_inode *nfsi = NFS_I(inode);
894
895         if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
896                         && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
897                         && nfsi->change_attr == fattr->pre_change_attr) {
898                 nfsi->change_attr = fattr->change_attr;
899                 if (S_ISDIR(inode->i_mode))
900                         nfsi->cache_validity |= NFS_INO_INVALID_DATA;
901         }
902         /* If we have atomic WCC data, we may update some attributes */
903         if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
904                         && (fattr->valid & NFS_ATTR_FATTR_CTIME)
905                         && timespec_equal(&inode->i_ctime, &fattr->pre_ctime))
906                         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
907
908         if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
909                         && (fattr->valid & NFS_ATTR_FATTR_MTIME)
910                         && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
911                         memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
912                         if (S_ISDIR(inode->i_mode))
913                                 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
914         }
915         if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
916                         && (fattr->valid & NFS_ATTR_FATTR_SIZE)
917                         && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
918                         && nfsi->npages == 0)
919                         i_size_write(inode, nfs_size_to_loff_t(fattr->size));
920 }
921
922 /**
923  * nfs_check_inode_attributes - verify consistency of the inode attribute cache
924  * @inode - pointer to inode
925  * @fattr - updated attributes
926  *
927  * Verifies the attribute cache. If we have just changed the attributes,
928  * so that fattr carries weak cache consistency data, then it may
929  * also update the ctime/mtime/change_attribute.
930  */
931 static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
932 {
933         struct nfs_inode *nfsi = NFS_I(inode);
934         loff_t cur_size, new_isize;
935         unsigned long invalid = 0;
936
937
938         /* Has the inode gone and changed behind our back? */
939         if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
940                 return -EIO;
941         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
942                 return -EIO;
943
944         if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
945                         nfsi->change_attr != fattr->change_attr)
946                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
947
948         /* Verify a few of the more important attributes */
949         if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
950                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
951
952         if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
953                 cur_size = i_size_read(inode);
954                 new_isize = nfs_size_to_loff_t(fattr->size);
955                 if (cur_size != new_isize && nfsi->npages == 0)
956                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
957         }
958
959         /* Have any file permissions changed? */
960         if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
961                 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
962         if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
963                 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
964         if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
965                 invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
966
967         /* Has the link count changed? */
968         if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
969                 invalid |= NFS_INO_INVALID_ATTR;
970
971         if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
972                 invalid |= NFS_INO_INVALID_ATIME;
973
974         if (invalid != 0)
975                 nfsi->cache_validity |= invalid;
976
977         nfsi->read_cache_jiffies = fattr->time_start;
978         return 0;
979 }
980
981 static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
982 {
983         if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
984                 return 0;
985         return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
986 }
987
988 static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
989 {
990         if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
991                 return 0;
992         return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
993 }
994
995 static atomic_long_t nfs_attr_generation_counter;
996
997 static unsigned long nfs_read_attr_generation_counter(void)
998 {
999         return atomic_long_read(&nfs_attr_generation_counter);
1000 }
1001
1002 unsigned long nfs_inc_attr_generation_counter(void)
1003 {
1004         return atomic_long_inc_return(&nfs_attr_generation_counter);
1005 }
1006
1007 void nfs_fattr_init(struct nfs_fattr *fattr)
1008 {
1009         fattr->valid = 0;
1010         fattr->time_start = jiffies;
1011         fattr->gencount = nfs_inc_attr_generation_counter();
1012 }
1013
1014 /**
1015  * nfs_inode_attrs_need_update - check if the inode attributes need updating
1016  * @inode - pointer to inode
1017  * @fattr - attributes
1018  *
1019  * Attempt to divine whether or not an RPC call reply carrying stale
1020  * attributes got scheduled after another call carrying updated ones.
1021  *
1022  * To do so, the function first assumes that a more recent ctime means
1023  * that the attributes in fattr are newer, however it also attempt to
1024  * catch the case where ctime either didn't change, or went backwards
1025  * (if someone reset the clock on the server) by looking at whether
1026  * or not this RPC call was started after the inode was last updated.
1027  * Note also the check for wraparound of 'attr_gencount'
1028  *
1029  * The function returns 'true' if it thinks the attributes in 'fattr' are
1030  * more recent than the ones cached in the inode.
1031  *
1032  */
1033 static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
1034 {
1035         const struct nfs_inode *nfsi = NFS_I(inode);
1036
1037         return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
1038                 nfs_ctime_need_update(inode, fattr) ||
1039                 nfs_size_need_update(inode, fattr) ||
1040                 ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
1041 }
1042
1043 static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1044 {
1045         if (nfs_inode_attrs_need_update(inode, fattr))
1046                 return nfs_update_inode(inode, fattr);
1047         return nfs_check_inode_attributes(inode, fattr);
1048 }
1049
1050 /**
1051  * nfs_refresh_inode - try to update the inode attribute cache
1052  * @inode - pointer to inode
1053  * @fattr - updated attributes
1054  *
1055  * Check that an RPC call that returned attributes has not overlapped with
1056  * other recent updates of the inode metadata, then decide whether it is
1057  * safe to do a full update of the inode attributes, or whether just to
1058  * call nfs_check_inode_attributes.
1059  */
1060 int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
1061 {
1062         int status;
1063
1064         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1065                 return 0;
1066         spin_lock(&inode->i_lock);
1067         status = nfs_refresh_inode_locked(inode, fattr);
1068         spin_unlock(&inode->i_lock);
1069
1070         return status;
1071 }
1072
1073 static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
1074 {
1075         struct nfs_inode *nfsi = NFS_I(inode);
1076
1077         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
1078         if (S_ISDIR(inode->i_mode))
1079                 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1080         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
1081                 return 0;
1082         return nfs_refresh_inode_locked(inode, fattr);
1083 }
1084
1085 /**
1086  * nfs_post_op_update_inode - try to update the inode attribute cache
1087  * @inode - pointer to inode
1088  * @fattr - updated attributes
1089  *
1090  * After an operation that has changed the inode metadata, mark the
1091  * attribute cache as being invalid, then try to update it.
1092  *
1093  * NB: if the server didn't return any post op attributes, this
1094  * function will force the retrieval of attributes before the next
1095  * NFS request.  Thus it should be used only for operations that
1096  * are expected to change one or more attributes, to avoid
1097  * unnecessary NFS requests and trips through nfs_update_inode().
1098  */
1099 int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1100 {
1101         int status;
1102
1103         spin_lock(&inode->i_lock);
1104         status = nfs_post_op_update_inode_locked(inode, fattr);
1105         spin_unlock(&inode->i_lock);
1106         return status;
1107 }
1108
1109 /**
1110  * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1111  * @inode - pointer to inode
1112  * @fattr - updated attributes
1113  *
1114  * After an operation that has changed the inode metadata, mark the
1115  * attribute cache as being invalid, then try to update it. Fake up
1116  * weak cache consistency data, if none exist.
1117  *
1118  * This function is mainly designed to be used by the ->write_done() functions.
1119  */
1120 int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
1121 {
1122         int status;
1123
1124         spin_lock(&inode->i_lock);
1125         /* Don't do a WCC update if these attributes are already stale */
1126         if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
1127                         !nfs_inode_attrs_need_update(inode, fattr)) {
1128                 fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
1129                                 | NFS_ATTR_FATTR_PRESIZE
1130                                 | NFS_ATTR_FATTR_PREMTIME
1131                                 | NFS_ATTR_FATTR_PRECTIME);
1132                 goto out_noforce;
1133         }
1134         if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
1135                         (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
1136                 fattr->pre_change_attr = NFS_I(inode)->change_attr;
1137                 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
1138         }
1139         if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
1140                         (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
1141                 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
1142                 fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
1143         }
1144         if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
1145                         (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
1146                 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
1147                 fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
1148         }
1149         if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
1150                         (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
1151                 fattr->pre_size = i_size_read(inode);
1152                 fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
1153         }
1154 out_noforce:
1155         status = nfs_post_op_update_inode_locked(inode, fattr);
1156         spin_unlock(&inode->i_lock);
1157         return status;
1158 }
1159
1160 /*
1161  * Many nfs protocol calls return the new file attributes after
1162  * an operation.  Here we update the inode to reflect the state
1163  * of the server's inode.
1164  *
1165  * This is a bit tricky because we have to make sure all dirty pages
1166  * have been sent off to the server before calling invalidate_inode_pages.
1167  * To make sure no other process adds more write requests while we try
1168  * our best to flush them, we make them sleep during the attribute refresh.
1169  *
1170  * A very similar scenario holds for the dir cache.
1171  */
1172 static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1173 {
1174         struct nfs_server *server;
1175         struct nfs_inode *nfsi = NFS_I(inode);
1176         loff_t cur_isize, new_isize;
1177         unsigned long invalid = 0;
1178         unsigned long now = jiffies;
1179         unsigned long save_cache_validity;
1180
1181         dfprintk(VFS, "NFS: %s(%s/%ld ct=%d info=0x%x)\n",
1182                         __func__, inode->i_sb->s_id, inode->i_ino,
1183                         atomic_read(&inode->i_count), fattr->valid);
1184
1185         if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
1186                 goto out_fileid;
1187
1188         /*
1189          * Make sure the inode's type hasn't changed.
1190          */
1191         if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
1192                 goto out_changed;
1193
1194         server = NFS_SERVER(inode);
1195         /* Update the fsid? */
1196         if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
1197                         !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
1198                         !test_bit(NFS_INO_MOUNTPOINT, &nfsi->flags))
1199                 server->fsid = fattr->fsid;
1200
1201         /*
1202          * Update the read time so we don't revalidate too often.
1203          */
1204         nfsi->read_cache_jiffies = fattr->time_start;
1205
1206         save_cache_validity = nfsi->cache_validity;
1207         nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
1208                         | NFS_INO_INVALID_ATIME
1209                         | NFS_INO_REVAL_FORCED
1210                         | NFS_INO_REVAL_PAGECACHE);
1211
1212         /* Do atomic weak cache consistency updates */
1213         nfs_wcc_update_inode(inode, fattr);
1214
1215         /* More cache consistency checks */
1216         if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
1217                 if (nfsi->change_attr != fattr->change_attr) {
1218                         dprintk("NFS: change_attr change on server for file %s/%ld\n",
1219                                         inode->i_sb->s_id, inode->i_ino);
1220                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1221                         if (S_ISDIR(inode->i_mode))
1222                                 nfs_force_lookup_revalidate(inode);
1223                         nfsi->change_attr = fattr->change_attr;
1224                 }
1225         } else if (server->caps & NFS_CAP_CHANGE_ATTR)
1226                 invalid |= save_cache_validity;
1227
1228         if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
1229                 /* NFSv2/v3: Check if the mtime agrees */
1230                 if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
1231                         dprintk("NFS: mtime change on server for file %s/%ld\n",
1232                                         inode->i_sb->s_id, inode->i_ino);
1233                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1234                         if (S_ISDIR(inode->i_mode))
1235                                 nfs_force_lookup_revalidate(inode);
1236                         memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
1237                 }
1238         } else if (server->caps & NFS_CAP_MTIME)
1239                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1240                                 | NFS_INO_INVALID_DATA
1241                                 | NFS_INO_REVAL_PAGECACHE
1242                                 | NFS_INO_REVAL_FORCED);
1243
1244         if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
1245                 /* If ctime has changed we should definitely clear access+acl caches */
1246                 if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
1247                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1248                         /* and probably clear data for a directory too as utimes can cause
1249                          * havoc with our cache.
1250                          */
1251                         if (S_ISDIR(inode->i_mode)) {
1252                                 invalid |= NFS_INO_INVALID_DATA;
1253                                 nfs_force_lookup_revalidate(inode);
1254                         }
1255                         memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
1256                 }
1257         } else if (server->caps & NFS_CAP_CTIME)
1258                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1259                                 | NFS_INO_INVALID_ACCESS
1260                                 | NFS_INO_INVALID_ACL
1261                                 | NFS_INO_REVAL_FORCED);
1262
1263         /* Check if our cached file size is stale */
1264         if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
1265                 new_isize = nfs_size_to_loff_t(fattr->size);
1266                 cur_isize = i_size_read(inode);
1267                 if (new_isize != cur_isize) {
1268                         /* Do we perhaps have any outstanding writes, or has
1269                          * the file grown beyond our last write? */
1270                         if (nfsi->npages == 0 || new_isize > cur_isize) {
1271                                 i_size_write(inode, new_isize);
1272                                 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1273                         }
1274                         dprintk("NFS: isize change on server for file %s/%ld\n",
1275                                         inode->i_sb->s_id, inode->i_ino);
1276                 }
1277         } else
1278                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1279                                 | NFS_INO_REVAL_PAGECACHE
1280                                 | NFS_INO_REVAL_FORCED);
1281
1282
1283         if (fattr->valid & NFS_ATTR_FATTR_ATIME)
1284                 memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
1285         else if (server->caps & NFS_CAP_ATIME)
1286                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
1287                                 | NFS_INO_REVAL_FORCED);
1288
1289         if (fattr->valid & NFS_ATTR_FATTR_MODE) {
1290                 if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
1291                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1292                         inode->i_mode = fattr->mode;
1293                 }
1294         } else if (server->caps & NFS_CAP_MODE)
1295                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1296                                 | NFS_INO_INVALID_ACCESS
1297                                 | NFS_INO_INVALID_ACL
1298                                 | NFS_INO_REVAL_FORCED);
1299
1300         if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
1301                 if (inode->i_uid != fattr->uid) {
1302                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1303                         inode->i_uid = fattr->uid;
1304                 }
1305         } else if (server->caps & NFS_CAP_OWNER)
1306                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1307                                 | NFS_INO_INVALID_ACCESS
1308                                 | NFS_INO_INVALID_ACL
1309                                 | NFS_INO_REVAL_FORCED);
1310
1311         if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
1312                 if (inode->i_gid != fattr->gid) {
1313                         invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
1314                         inode->i_gid = fattr->gid;
1315                 }
1316         } else if (server->caps & NFS_CAP_OWNER_GROUP)
1317                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1318                                 | NFS_INO_INVALID_ACCESS
1319                                 | NFS_INO_INVALID_ACL
1320                                 | NFS_INO_REVAL_FORCED);
1321
1322         if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
1323                 if (inode->i_nlink != fattr->nlink) {
1324                         invalid |= NFS_INO_INVALID_ATTR;
1325                         if (S_ISDIR(inode->i_mode))
1326                                 invalid |= NFS_INO_INVALID_DATA;
1327                         inode->i_nlink = fattr->nlink;
1328                 }
1329         } else if (server->caps & NFS_CAP_NLINK)
1330                 invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
1331                                 | NFS_INO_REVAL_FORCED);
1332
1333         if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
1334                 /*
1335                  * report the blocks in 512byte units
1336                  */
1337                 inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
1338         }
1339         if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
1340                 inode->i_blocks = fattr->du.nfs2.blocks;
1341
1342         /* Update attrtimeo value if we're out of the unstable period */
1343         if (invalid & NFS_INO_INVALID_ATTR) {
1344                 nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
1345                 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1346                 nfsi->attrtimeo_timestamp = now;
1347                 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1348         } else {
1349                 if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
1350                         if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
1351                                 nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
1352                         nfsi->attrtimeo_timestamp = now;
1353                 }
1354         }
1355         invalid &= ~NFS_INO_INVALID_ATTR;
1356         /* Don't invalidate the data if we were to blame */
1357         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1358                                 || S_ISLNK(inode->i_mode)))
1359                 invalid &= ~NFS_INO_INVALID_DATA;
1360         if (!nfs_have_delegation(inode, FMODE_READ) ||
1361                         (save_cache_validity & NFS_INO_REVAL_FORCED))
1362                 nfsi->cache_validity |= invalid;
1363
1364         return 0;
1365  out_changed:
1366         /*
1367          * Big trouble! The inode has become a different object.
1368          */
1369         printk(KERN_DEBUG "%s: inode %ld mode changed, %07o to %07o\n",
1370                         __func__, inode->i_ino, inode->i_mode, fattr->mode);
1371  out_err:
1372         /*
1373          * No need to worry about unhashing the dentry, as the
1374          * lookup validation will know that the inode is bad.
1375          * (But we fall through to invalidate the caches.)
1376          */
1377         nfs_invalidate_inode(inode);
1378         return -ESTALE;
1379
1380  out_fileid:
1381         printk(KERN_ERR "NFS: server %s error: fileid changed\n"
1382                 "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
1383                 NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
1384                 (long long)nfsi->fileid, (long long)fattr->fileid);
1385         goto out_err;
1386 }
1387
1388
1389 #ifdef CONFIG_NFS_V4
1390
1391 /*
1392  * Clean out any remaining NFSv4 state that might be left over due
1393  * to open() calls that passed nfs_atomic_lookup, but failed to call
1394  * nfs_open().
1395  */
1396 void nfs4_clear_inode(struct inode *inode)
1397 {
1398         /* If we are holding a delegation, return it! */
1399         nfs_inode_return_delegation_noreclaim(inode);
1400         /* First call standard NFS clear_inode() code */
1401         nfs_clear_inode(inode);
1402 }
1403 #endif
1404
1405 struct inode *nfs_alloc_inode(struct super_block *sb)
1406 {
1407         struct nfs_inode *nfsi;
1408         nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
1409         if (!nfsi)
1410                 return NULL;
1411         nfsi->flags = 0UL;
1412         nfsi->cache_validity = 0UL;
1413 #ifdef CONFIG_NFS_V3_ACL
1414         nfsi->acl_access = ERR_PTR(-EAGAIN);
1415         nfsi->acl_default = ERR_PTR(-EAGAIN);
1416 #endif
1417 #ifdef CONFIG_NFS_V4
1418         nfsi->nfs4_acl = NULL;
1419 #endif /* CONFIG_NFS_V4 */
1420         return &nfsi->vfs_inode;
1421 }
1422
1423 void nfs_destroy_inode(struct inode *inode)
1424 {
1425         kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
1426 }
1427
1428 static inline void nfs4_init_once(struct nfs_inode *nfsi)
1429 {
1430 #ifdef CONFIG_NFS_V4
1431         INIT_LIST_HEAD(&nfsi->open_states);
1432         nfsi->delegation = NULL;
1433         nfsi->delegation_state = 0;
1434         init_rwsem(&nfsi->rwsem);
1435 #endif
1436 }
1437
1438 static void init_once(void *foo)
1439 {
1440         struct nfs_inode *nfsi = (struct nfs_inode *) foo;
1441
1442         inode_init_once(&nfsi->vfs_inode);
1443         INIT_LIST_HEAD(&nfsi->open_files);
1444         INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
1445         INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
1446         INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
1447         nfsi->npages = 0;
1448         atomic_set(&nfsi->silly_count, 1);
1449         INIT_HLIST_HEAD(&nfsi->silly_list);
1450         init_waitqueue_head(&nfsi->waitqueue);
1451         nfs4_init_once(nfsi);
1452 }
1453
1454 static int __init nfs_init_inodecache(void)
1455 {
1456         nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
1457                                              sizeof(struct nfs_inode),
1458                                              0, (SLAB_RECLAIM_ACCOUNT|
1459                                                 SLAB_MEM_SPREAD),
1460                                              init_once);
1461         if (nfs_inode_cachep == NULL)
1462                 return -ENOMEM;
1463
1464         return 0;
1465 }
1466
1467 static void nfs_destroy_inodecache(void)
1468 {
1469         kmem_cache_destroy(nfs_inode_cachep);
1470 }
1471
1472 struct workqueue_struct *nfsiod_workqueue;
1473
1474 /*
1475  * start up the nfsiod workqueue
1476  */
1477 static int nfsiod_start(void)
1478 {
1479         struct workqueue_struct *wq;
1480         dprintk("RPC:       creating workqueue nfsiod\n");
1481         wq = create_singlethread_workqueue("nfsiod");
1482         if (wq == NULL)
1483                 return -ENOMEM;
1484         nfsiod_workqueue = wq;
1485         return 0;
1486 }
1487
1488 /*
1489  * Destroy the nfsiod workqueue
1490  */
1491 static void nfsiod_stop(void)
1492 {
1493         struct workqueue_struct *wq;
1494
1495         wq = nfsiod_workqueue;
1496         if (wq == NULL)
1497                 return;
1498         nfsiod_workqueue = NULL;
1499         destroy_workqueue(wq);
1500 }
1501
1502 /*
1503  * Initialize NFS
1504  */
1505 static int __init init_nfs_fs(void)
1506 {
1507         int err;
1508
1509         err = nfs_fscache_register();
1510         if (err < 0)
1511                 goto out7;
1512
1513         err = nfsiod_start();
1514         if (err)
1515                 goto out6;
1516
1517         err = nfs_fs_proc_init();
1518         if (err)
1519                 goto out5;
1520
1521         err = nfs_init_nfspagecache();
1522         if (err)
1523                 goto out4;
1524
1525         err = nfs_init_inodecache();
1526         if (err)
1527                 goto out3;
1528
1529         err = nfs_init_readpagecache();
1530         if (err)
1531                 goto out2;
1532
1533         err = nfs_init_writepagecache();
1534         if (err)
1535                 goto out1;
1536
1537         err = nfs_init_directcache();
1538         if (err)
1539                 goto out0;
1540
1541 #ifdef CONFIG_PROC_FS
1542         rpc_proc_register(&nfs_rpcstat);
1543 #endif
1544         if ((err = register_nfs_fs()) != 0)
1545                 goto out;
1546         return 0;
1547 out:
1548 #ifdef CONFIG_PROC_FS
1549         rpc_proc_unregister("nfs");
1550 #endif
1551         nfs_destroy_directcache();
1552 out0:
1553         nfs_destroy_writepagecache();
1554 out1:
1555         nfs_destroy_readpagecache();
1556 out2:
1557         nfs_destroy_inodecache();
1558 out3:
1559         nfs_destroy_nfspagecache();
1560 out4:
1561         nfs_fs_proc_exit();
1562 out5:
1563         nfsiod_stop();
1564 out6:
1565         nfs_fscache_unregister();
1566 out7:
1567         return err;
1568 }
1569
1570 static void __exit exit_nfs_fs(void)
1571 {
1572         nfs_destroy_directcache();
1573         nfs_destroy_writepagecache();
1574         nfs_destroy_readpagecache();
1575         nfs_destroy_inodecache();
1576         nfs_destroy_nfspagecache();
1577         nfs_fscache_unregister();
1578 #ifdef CONFIG_PROC_FS
1579         rpc_proc_unregister("nfs");
1580 #endif
1581         unregister_nfs_fs();
1582         nfs_fs_proc_exit();
1583         nfsiod_stop();
1584 }
1585
1586 /* Not quite true; I just maintain it */
1587 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1588 MODULE_LICENSE("GPL");
1589 module_param(enable_ino64, bool, 0644);
1590
1591 module_init(init_nfs_fs)
1592 module_exit(exit_nfs_fs)