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