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