4 * Copyright (C) 1992 Rick Sladkey
6 * nfs inode and superblock handling functions
8 * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
9 * experimental NFS changes. Modularisation taken straight from SYS5 fs.
11 * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
12 * J.S.Peatfield@damtp.cam.ac.uk
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>
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>
43 #include <asm/uaccess.h>
47 #include "delegation.h"
51 #include "dns_resolve.h"
56 #define NFSDBG_FACILITY NFSDBG_VFS
58 #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
60 /* Default is to see 64-bit inode numbers */
61 static bool enable_ino64
= NFS_64_BIT_INODE_NUMBERS_ENABLED
;
63 static void nfs_invalidate_inode(struct inode
*);
64 static int nfs_update_inode(struct inode
*, struct nfs_fattr
*);
66 static struct kmem_cache
* nfs_inode_cachep
;
68 static inline unsigned long
69 nfs_fattr_to_ino_t(struct nfs_fattr
*fattr
)
71 return nfs_fileid_to_ino_t(fattr
->fileid
);
75 * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
76 * @word: long word containing the bit lock
78 int nfs_wait_bit_killable(void *word
)
80 if (fatal_signal_pending(current
))
85 EXPORT_SYMBOL_GPL(nfs_wait_bit_killable
);
88 * nfs_compat_user_ino64 - returns the user-visible inode number
89 * @fileid: 64-bit fileid
91 * This function returns a 32-bit inode number if the boot parameter
92 * nfs.enable_ino64 is zero.
94 u64
nfs_compat_user_ino64(u64 fileid
)
105 if (sizeof(ino
) < sizeof(fileid
))
106 ino
^= fileid
>> (sizeof(fileid
)-sizeof(ino
)) * 8;
110 int nfs_drop_inode(struct inode
*inode
)
112 return NFS_STALE(inode
) || generic_drop_inode(inode
);
114 EXPORT_SYMBOL_GPL(nfs_drop_inode
);
116 void nfs_clear_inode(struct inode
*inode
)
119 * The following should never happen...
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
);
127 EXPORT_SYMBOL_GPL(nfs_clear_inode
);
129 void nfs_evict_inode(struct inode
*inode
)
131 truncate_inode_pages(&inode
->i_data
, 0);
133 nfs_clear_inode(inode
);
137 * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
139 int nfs_sync_mapping(struct address_space
*mapping
)
143 if (mapping
->nrpages
!= 0) {
144 unmap_mapping_range(mapping
, 0, 0, 0);
145 ret
= nfs_wb_all(mapping
->host
);
151 * Invalidate the local caches
153 static void nfs_zap_caches_locked(struct inode
*inode
)
155 struct nfs_inode
*nfsi
= NFS_I(inode
);
156 int mode
= inode
->i_mode
;
158 nfs_inc_stats(inode
, NFSIOS_ATTRINVALIDATE
);
160 nfsi
->attrtimeo
= NFS_MINATTRTIMEO(inode
);
161 nfsi
->attrtimeo_timestamp
= jiffies
;
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
;
167 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ACCESS
|NFS_INO_INVALID_ACL
|NFS_INO_REVAL_PAGECACHE
;
170 void nfs_zap_caches(struct inode
*inode
)
172 spin_lock(&inode
->i_lock
);
173 nfs_zap_caches_locked(inode
);
174 spin_unlock(&inode
->i_lock
);
177 void nfs_zap_mapping(struct inode
*inode
, struct address_space
*mapping
)
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
);
186 void nfs_zap_acl_cache(struct inode
*inode
)
188 void (*clear_acl_cache
)(struct inode
*);
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
);
197 EXPORT_SYMBOL_GPL(nfs_zap_acl_cache
);
199 void nfs_invalidate_atime(struct inode
*inode
)
201 spin_lock(&inode
->i_lock
);
202 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATIME
;
203 spin_unlock(&inode
->i_lock
);
205 EXPORT_SYMBOL_GPL(nfs_invalidate_atime
);
208 * Invalidate, but do not unhash, the inode.
209 * NB: must be called with inode->i_lock held!
211 static void nfs_invalidate_inode(struct inode
*inode
)
213 set_bit(NFS_INO_STALE
, &NFS_I(inode
)->flags
);
214 nfs_zap_caches_locked(inode
);
217 struct nfs_find_desc
{
219 struct nfs_fattr
*fattr
;
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
229 nfs_find_actor(struct inode
*inode
, void *opaque
)
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
;
235 if (NFS_FILEID(inode
) != fattr
->fileid
)
237 if (nfs_compare_fh(NFS_FH(inode
), fh
))
239 if (is_bad_inode(inode
) || NFS_STALE(inode
))
245 nfs_init_locked(struct inode
*inode
, void *opaque
)
247 struct nfs_find_desc
*desc
= (struct nfs_find_desc
*)opaque
;
248 struct nfs_fattr
*fattr
= desc
->fattr
;
250 set_nfs_fileid(inode
, fattr
->fileid
);
251 nfs_copy_fh(NFS_FH(inode
), desc
->fh
);
256 * This is our front-end to iget that looks up inodes by file handle
257 * instead of inode number.
260 nfs_fhget(struct super_block
*sb
, struct nfs_fh
*fh
, struct nfs_fattr
*fattr
)
262 struct nfs_find_desc desc
= {
266 struct inode
*inode
= ERR_PTR(-ENOENT
);
269 nfs_attr_check_mountpoint(sb
, fattr
);
271 if (((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) == 0) &&
272 !nfs_attr_use_mounted_on_fileid(fattr
))
274 if ((fattr
->valid
& NFS_ATTR_FATTR_TYPE
) == 0)
277 hash
= nfs_fattr_to_ino_t(fattr
);
279 inode
= iget5_locked(sb
, hash
, nfs_find_actor
, nfs_init_locked
, &desc
);
281 inode
= ERR_PTR(-ENOMEM
);
285 if (inode
->i_state
& I_NEW
) {
286 struct nfs_inode
*nfsi
= NFS_I(inode
);
287 unsigned long now
= jiffies
;
289 /* We set i_ino for the few things that still rely on it,
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.
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
;
317 inode
->i_op
= &nfs_mountpoint_inode_operations
;
319 inode
->i_flags
|= S_AUTOMOUNT
;
321 } else if (S_ISLNK(inode
->i_mode
))
322 inode
->i_op
= &nfs_symlink_inode_operations
;
324 init_special_inode(inode
, inode
->i_mode
, fattr
->rdev
);
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;
335 memset(nfsi
->cookieverf
, 0, sizeof(nfsi
->cookieverf
));
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
);
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
) {
378 * report the blocks in 512byte units
380 inode
->i_blocks
= nfs_calc_block_size(fattr
->du
.nfs3
.used
);
382 nfsi
->attrtimeo
= NFS_MINATTRTIMEO(inode
);
383 nfsi
->attrtimeo_timestamp
= now
;
384 nfsi
->access_cache
= RB_ROOT
;
386 nfs_fscache_init_inode_cookie(inode
);
388 unlock_new_inode(inode
);
390 nfs_refresh_inode(inode
, fattr
);
391 dprintk("NFS: nfs_fhget(%s/%Ld fh_crc=0x%08x ct=%d)\n",
393 (long long)NFS_FILEID(inode
),
394 nfs_display_fhandle_hash(fh
),
395 atomic_read(&inode
->i_count
));
401 dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode
));
404 EXPORT_SYMBOL_GPL(nfs_fhget
);
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)
409 nfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
411 struct inode
*inode
= dentry
->d_inode
;
412 struct nfs_fattr
*fattr
;
415 nfs_inc_stats(inode
, NFSIOS_VFSSETATTR
);
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
;
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
;
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)
431 /* Write all dirty data */
432 if (S_ISREG(inode
->i_mode
)) {
433 nfs_inode_dio_wait(inode
);
437 fattr
= nfs_alloc_fattr();
441 * Return any delegations if we're going to change ACLs
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
);
447 nfs_refresh_inode(inode
, fattr
);
448 nfs_free_fattr(fattr
);
452 EXPORT_SYMBOL_GPL(nfs_setattr
);
455 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
456 * @inode: inode of the file used
457 * @offset: file offset to start truncating
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.
463 static int nfs_vmtruncate(struct inode
* inode
, loff_t offset
)
468 err
= inode_newsize_ok(inode
, offset
);
472 spin_lock(&inode
->i_lock
);
473 oldsize
= inode
->i_size
;
474 i_size_write(inode
, offset
);
475 spin_unlock(&inode
->i_lock
);
477 truncate_pagecache(inode
, oldsize
, offset
);
483 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
484 * @inode: pointer to struct inode
485 * @attr: pointer to struct iattr
487 * Note: we do this in the *proc.c in order to ensure that
488 * it works for things like exclusive creates too.
490 void nfs_setattr_update_inode(struct inode
*inode
, struct iattr
*attr
)
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
;
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
);
506 if ((attr
->ia_valid
& ATTR_SIZE
) != 0) {
507 nfs_inc_stats(inode
, NFSIOS_SETATTRTRUNC
);
508 nfs_vmtruncate(inode
, attr
->ia_size
);
511 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode
);
513 int nfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
515 struct inode
*inode
= dentry
->d_inode
;
516 int need_atime
= NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ATIME
;
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
);
528 * We may force a getattr if the user cares about atime.
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
533 * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
534 * no point in checking those.
536 if ((mnt
->mnt_flags
& MNT_NOATIME
) ||
537 ((mnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
)))
541 err
= __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
543 err
= nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
545 generic_fillattr(inode
, stat
);
546 stat
->ino
= nfs_compat_user_ino64(NFS_FILEID(inode
));
551 EXPORT_SYMBOL_GPL(nfs_getattr
);
553 static void nfs_init_lock_context(struct nfs_lock_context
*l_ctx
)
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
);
561 static struct nfs_lock_context
*__nfs_find_lock_context(struct nfs_open_context
*ctx
)
563 struct nfs_lock_context
*pos
;
565 list_for_each_entry(pos
, &ctx
->lock_context
.list
, list
) {
566 if (pos
->lockowner
.l_owner
!= current
->files
)
568 if (pos
->lockowner
.l_pid
!= current
->tgid
)
570 atomic_inc(&pos
->count
);
576 struct nfs_lock_context
*nfs_get_lock_context(struct nfs_open_context
*ctx
)
578 struct nfs_lock_context
*res
, *new = NULL
;
579 struct inode
*inode
= ctx
->dentry
->d_inode
;
581 spin_lock(&inode
->i_lock
);
582 res
= __nfs_find_lock_context(ctx
);
584 spin_unlock(&inode
->i_lock
);
585 new = kmalloc(sizeof(*new), GFP_KERNEL
);
587 return ERR_PTR(-ENOMEM
);
588 nfs_init_lock_context(new);
589 spin_lock(&inode
->i_lock
);
590 res
= __nfs_find_lock_context(ctx
);
592 list_add_tail(&new->list
, &ctx
->lock_context
.list
);
593 new->open_context
= ctx
;
598 spin_unlock(&inode
->i_lock
);
603 void nfs_put_lock_context(struct nfs_lock_context
*l_ctx
)
605 struct nfs_open_context
*ctx
= l_ctx
->open_context
;
606 struct inode
*inode
= ctx
->dentry
->d_inode
;
608 if (!atomic_dec_and_lock(&l_ctx
->count
, &inode
->i_lock
))
610 list_del(&l_ctx
->list
);
611 spin_unlock(&inode
->i_lock
);
616 * nfs_close_context - Common close_context() routine NFSv2/v3
617 * @ctx: pointer to context
618 * @is_sync: is this a synchronous close
620 * always ensure that the attributes are up to date if we're mounted
621 * with close-to-open semantics
623 void nfs_close_context(struct nfs_open_context
*ctx
, int is_sync
)
626 struct nfs_server
*server
;
628 if (!(ctx
->mode
& FMODE_WRITE
))
632 inode
= ctx
->dentry
->d_inode
;
633 if (!list_empty(&NFS_I(inode
)->open_files
))
635 server
= NFS_SERVER(inode
);
636 if (server
->flags
& NFS_MOUNT_NOCTO
)
638 nfs_revalidate_inode(server
, inode
);
640 EXPORT_SYMBOL_GPL(nfs_close_context
);
642 struct nfs_open_context
*alloc_nfs_open_context(struct dentry
*dentry
, fmode_t f_mode
)
644 struct nfs_open_context
*ctx
;
645 struct rpc_cred
*cred
= rpc_lookup_cred();
647 return ERR_CAST(cred
);
649 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
652 return ERR_PTR(-ENOMEM
);
654 nfs_sb_active(dentry
->d_sb
);
655 ctx
->dentry
= dget(dentry
);
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
;
667 EXPORT_SYMBOL_GPL(alloc_nfs_open_context
);
669 struct nfs_open_context
*get_nfs_open_context(struct nfs_open_context
*ctx
)
672 atomic_inc(&ctx
->lock_context
.count
);
675 EXPORT_SYMBOL_GPL(get_nfs_open_context
);
677 static void __put_nfs_open_context(struct nfs_open_context
*ctx
, int is_sync
)
679 struct inode
*inode
= ctx
->dentry
->d_inode
;
680 struct super_block
*sb
= ctx
->dentry
->d_sb
;
682 if (!list_empty(&ctx
->list
)) {
683 if (!atomic_dec_and_lock(&ctx
->lock_context
.count
, &inode
->i_lock
))
685 list_del(&ctx
->list
);
686 spin_unlock(&inode
->i_lock
);
687 } else if (!atomic_dec_and_test(&ctx
->lock_context
.count
))
690 NFS_PROTO(inode
)->close_context(ctx
, is_sync
);
691 if (ctx
->cred
!= NULL
)
692 put_rpccred(ctx
->cred
);
697 nfs_sb_deactive_async(sb
);
698 kfree(ctx
->mdsthreshold
);
702 void put_nfs_open_context(struct nfs_open_context
*ctx
)
704 __put_nfs_open_context(ctx
, 0);
706 EXPORT_SYMBOL_GPL(put_nfs_open_context
);
709 * Ensure that mmap has a recent RPC credential for use when writing out
712 void nfs_file_set_open_context(struct file
*filp
, struct nfs_open_context
*ctx
)
714 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
715 struct nfs_inode
*nfsi
= NFS_I(inode
);
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
);
722 EXPORT_SYMBOL_GPL(nfs_file_set_open_context
);
725 * Given an inode, search for an open context with the desired characteristics
727 struct nfs_open_context
*nfs_find_open_context(struct inode
*inode
, struct rpc_cred
*cred
, fmode_t mode
)
729 struct nfs_inode
*nfsi
= NFS_I(inode
);
730 struct nfs_open_context
*pos
, *ctx
= NULL
;
732 spin_lock(&inode
->i_lock
);
733 list_for_each_entry(pos
, &nfsi
->open_files
, list
) {
734 if (cred
!= NULL
&& pos
->cred
!= cred
)
736 if ((pos
->mode
& (FMODE_READ
|FMODE_WRITE
)) != mode
)
738 ctx
= get_nfs_open_context(pos
);
741 spin_unlock(&inode
->i_lock
);
745 static void nfs_file_clear_open_context(struct file
*filp
)
747 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
748 struct nfs_open_context
*ctx
= nfs_file_open_context(filp
);
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);
760 * These allocate and release file read/write context information.
762 int nfs_open(struct inode
*inode
, struct file
*filp
)
764 struct nfs_open_context
*ctx
;
766 ctx
= alloc_nfs_open_context(filp
->f_path
.dentry
, filp
->f_mode
);
769 nfs_file_set_open_context(filp
, ctx
);
770 put_nfs_open_context(ctx
);
771 nfs_fscache_set_inode_cookie(inode
, filp
);
775 int nfs_release(struct inode
*inode
, struct file
*filp
)
777 nfs_file_clear_open_context(filp
);
782 * This function is called whenever some part of NFS notices that
783 * the cached attributes have to be refreshed.
786 __nfs_revalidate_inode(struct nfs_server
*server
, struct inode
*inode
)
788 int status
= -ESTALE
;
789 struct nfs_fattr
*fattr
= NULL
;
790 struct nfs_inode
*nfsi
= NFS_I(inode
);
792 dfprintk(PAGECACHE
, "NFS: revalidating (%s/%Ld)\n",
793 inode
->i_sb
->s_id
, (long long)NFS_FILEID(inode
));
795 if (is_bad_inode(inode
))
797 if (NFS_STALE(inode
))
801 fattr
= nfs_alloc_fattr();
805 nfs_inc_stats(inode
, NFSIOS_INODEREVALIDATE
);
806 status
= NFS_PROTO(inode
)->getattr(server
, NFS_FH(inode
), fattr
);
808 dfprintk(PAGECACHE
, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
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
);
819 status
= nfs_refresh_inode(inode
, fattr
);
821 dfprintk(PAGECACHE
, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
823 (long long)NFS_FILEID(inode
), status
);
827 if (nfsi
->cache_validity
& NFS_INO_INVALID_ACL
)
828 nfs_zap_acl_cache(inode
);
830 dfprintk(PAGECACHE
, "NFS: (%s/%Ld) revalidation complete\n",
832 (long long)NFS_FILEID(inode
));
835 nfs_free_fattr(fattr
);
839 int nfs_attribute_timeout(struct inode
*inode
)
841 struct nfs_inode
*nfsi
= NFS_I(inode
);
843 return !time_in_range_open(jiffies
, nfsi
->read_cache_jiffies
, nfsi
->read_cache_jiffies
+ nfsi
->attrtimeo
);
846 static int nfs_attribute_cache_expired(struct inode
*inode
)
848 if (nfs_have_delegated_attributes(inode
))
850 return nfs_attribute_timeout(inode
);
854 * nfs_revalidate_inode - Revalidate the inode attributes
855 * @server - pointer to nfs_server struct
856 * @inode - pointer to inode struct
858 * Updates inode attribute information by retrieving the data from the server.
860 int nfs_revalidate_inode(struct nfs_server
*server
, struct inode
*inode
)
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
);
867 EXPORT_SYMBOL_GPL(nfs_revalidate_inode
);
869 static int nfs_invalidate_mapping(struct inode
*inode
, struct address_space
*mapping
)
871 struct nfs_inode
*nfsi
= NFS_I(inode
);
873 if (mapping
->nrpages
!= 0) {
874 int ret
= invalidate_inode_pages2(mapping
);
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
));
890 static bool nfs_mapping_need_revalidate_inode(struct inode
*inode
)
892 if (nfs_have_delegated_attributes(inode
))
894 return (NFS_I(inode
)->cache_validity
& NFS_INO_REVAL_PAGECACHE
)
895 || nfs_attribute_timeout(inode
)
900 * nfs_revalidate_mapping - Revalidate the pagecache
901 * @inode - pointer to host inode
902 * @mapping - pointer to mapping
904 int nfs_revalidate_mapping(struct inode
*inode
, struct address_space
*mapping
)
906 struct nfs_inode
*nfsi
= NFS_I(inode
);
909 /* swapfiles are not supposed to be shared. */
910 if (IS_SWAPFILE(inode
))
913 if (nfs_mapping_need_revalidate_inode(inode
)) {
914 ret
= __nfs_revalidate_inode(NFS_SERVER(inode
), inode
);
918 if (nfsi
->cache_validity
& NFS_INO_INVALID_DATA
)
919 ret
= nfs_invalidate_mapping(inode
, mapping
);
924 static unsigned long nfs_wcc_update_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
926 struct nfs_inode
*nfsi
= NFS_I(inode
);
927 unsigned long ret
= 0;
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
;
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
;
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
;
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
;
964 * nfs_check_inode_attributes - verify consistency of the inode attribute cache
965 * @inode - pointer to inode
966 * @fattr - updated attributes
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.
972 static int nfs_check_inode_attributes(struct inode
*inode
, struct nfs_fattr
*fattr
)
974 struct nfs_inode
*nfsi
= NFS_I(inode
);
975 loff_t cur_size
, new_isize
;
976 unsigned long invalid
= 0;
979 if (nfs_have_delegated_attributes(inode
))
981 /* Has the inode gone and changed behind our back? */
982 if ((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) && nfsi
->fileid
!= fattr
->fileid
)
984 if ((fattr
->valid
& NFS_ATTR_FATTR_TYPE
) && (inode
->i_mode
& S_IFMT
) != (fattr
->mode
& S_IFMT
))
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
;
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
;
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
;
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
;
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
;
1014 if ((fattr
->valid
& NFS_ATTR_FATTR_ATIME
) && !timespec_equal(&inode
->i_atime
, &fattr
->atime
))
1015 invalid
|= NFS_INO_INVALID_ATIME
;
1018 nfsi
->cache_validity
|= invalid
;
1020 nfsi
->read_cache_jiffies
= fattr
->time_start
;
1024 static int nfs_ctime_need_update(const struct inode
*inode
, const struct nfs_fattr
*fattr
)
1026 if (!(fattr
->valid
& NFS_ATTR_FATTR_CTIME
))
1028 return timespec_compare(&fattr
->ctime
, &inode
->i_ctime
) > 0;
1031 static int nfs_size_need_update(const struct inode
*inode
, const struct nfs_fattr
*fattr
)
1033 if (!(fattr
->valid
& NFS_ATTR_FATTR_SIZE
))
1035 return nfs_size_to_loff_t(fattr
->size
) > i_size_read(inode
);
1038 static atomic_long_t nfs_attr_generation_counter
;
1040 static unsigned long nfs_read_attr_generation_counter(void)
1042 return atomic_long_read(&nfs_attr_generation_counter
);
1045 unsigned long nfs_inc_attr_generation_counter(void)
1047 return atomic_long_inc_return(&nfs_attr_generation_counter
);
1050 void nfs_fattr_init(struct nfs_fattr
*fattr
)
1053 fattr
->time_start
= jiffies
;
1054 fattr
->gencount
= nfs_inc_attr_generation_counter();
1055 fattr
->owner_name
= NULL
;
1056 fattr
->group_name
= NULL
;
1058 EXPORT_SYMBOL_GPL(nfs_fattr_init
);
1060 struct nfs_fattr
*nfs_alloc_fattr(void)
1062 struct nfs_fattr
*fattr
;
1064 fattr
= kmalloc(sizeof(*fattr
), GFP_NOFS
);
1066 nfs_fattr_init(fattr
);
1069 EXPORT_SYMBOL_GPL(nfs_alloc_fattr
);
1071 struct nfs_fh
*nfs_alloc_fhandle(void)
1075 fh
= kmalloc(sizeof(struct nfs_fh
), GFP_NOFS
);
1080 EXPORT_SYMBOL_GPL(nfs_alloc_fhandle
);
1084 * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
1085 * in the same way that wireshark does
1089 * For debugging only.
1091 u32
_nfs_display_fhandle_hash(const struct nfs_fh
*fh
)
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
);
1099 * _nfs_display_fhandle - display an NFS file handle on the console
1101 * @fh: file handle to display
1102 * @caption: display caption
1104 * For debugging only.
1106 void _nfs_display_fhandle(const struct nfs_fh
*fh
, const char *caption
)
1110 if (fh
== NULL
|| fh
->size
== 0) {
1111 printk(KERN_DEFAULT
"%s at %p is empty\n", caption
, fh
);
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
];
1120 switch ((fh
->size
- i
- 1) >> 2) {
1122 printk(KERN_DEFAULT
" %08x\n",
1126 printk(KERN_DEFAULT
" %08x %08x\n",
1127 be32_to_cpup(pos
), be32_to_cpup(pos
+ 1));
1130 printk(KERN_DEFAULT
" %08x %08x %08x\n",
1131 be32_to_cpup(pos
), be32_to_cpup(pos
+ 1),
1132 be32_to_cpup(pos
+ 2));
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));
1144 * nfs_inode_attrs_need_update - check if the inode attributes need updating
1145 * @inode - pointer to inode
1146 * @fattr - attributes
1148 * Attempt to divine whether or not an RPC call reply carrying stale
1149 * attributes got scheduled after another call carrying updated ones.
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'
1158 * The function returns 'true' if it thinks the attributes in 'fattr' are
1159 * more recent than the ones cached in the inode.
1162 static int nfs_inode_attrs_need_update(const struct inode
*inode
, const struct nfs_fattr
*fattr
)
1164 const struct nfs_inode
*nfsi
= NFS_I(inode
);
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);
1172 static int nfs_refresh_inode_locked(struct inode
*inode
, struct nfs_fattr
*fattr
)
1174 if (nfs_inode_attrs_need_update(inode
, fattr
))
1175 return nfs_update_inode(inode
, fattr
);
1176 return nfs_check_inode_attributes(inode
, fattr
);
1180 * nfs_refresh_inode - try to update the inode attribute cache
1181 * @inode - pointer to inode
1182 * @fattr - updated attributes
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.
1189 int nfs_refresh_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
1193 if ((fattr
->valid
& NFS_ATTR_FATTR
) == 0)
1195 spin_lock(&inode
->i_lock
);
1196 status
= nfs_refresh_inode_locked(inode
, fattr
);
1197 spin_unlock(&inode
->i_lock
);
1201 EXPORT_SYMBOL_GPL(nfs_refresh_inode
);
1203 static int nfs_post_op_update_inode_locked(struct inode
*inode
, struct nfs_fattr
*fattr
)
1205 struct nfs_inode
*nfsi
= NFS_I(inode
);
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)
1212 return nfs_refresh_inode_locked(inode
, fattr
);
1216 * nfs_post_op_update_inode - try to update the inode attribute cache
1217 * @inode - pointer to inode
1218 * @fattr - updated attributes
1220 * After an operation that has changed the inode metadata, mark the
1221 * attribute cache as being invalid, then try to update it.
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().
1229 int nfs_post_op_update_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
1233 spin_lock(&inode
->i_lock
);
1234 status
= nfs_post_op_update_inode_locked(inode
, fattr
);
1235 spin_unlock(&inode
->i_lock
);
1238 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode
);
1241 * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
1242 * @inode - pointer to inode
1243 * @fattr - updated attributes
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.
1249 * This function is mainly designed to be used by the ->write_done() functions.
1251 int nfs_post_op_update_inode_force_wcc(struct inode
*inode
, struct nfs_fattr
*fattr
)
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
);
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
;
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
;
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
;
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
;
1286 status
= nfs_post_op_update_inode_locked(inode
, fattr
);
1287 spin_unlock(&inode
->i_lock
);
1290 EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc
);
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.
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.
1302 * A very similar scenario holds for the dir cache.
1304 static int nfs_update_inode(struct inode
*inode
, struct nfs_fattr
*fattr
)
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
;
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
);
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
);
1328 * Make sure the inode's type hasn't changed.
1330 if ((fattr
->valid
& NFS_ATTR_FATTR_TYPE
) && (inode
->i_mode
& S_IFMT
) != (fattr
->mode
& S_IFMT
)) {
1332 * Big trouble! The inode has become a different object.
1334 printk(KERN_DEBUG
"NFS: %s: inode %ld mode changed, %07o to %07o\n",
1335 __func__
, inode
->i_ino
, inode
->i_mode
, fattr
->mode
);
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
;
1347 * Update the read time so we don't revalidate too often.
1349 nfsi
->read_cache_jiffies
= fattr
->time_start
;
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
);
1357 /* Do atomic weak cache consistency updates */
1358 invalid
|= nfs_wcc_update_inode(inode
, fattr
);
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
;
1374 } else if (server
->caps
& NFS_CAP_CHANGE_ATTR
)
1375 invalid
|= save_cache_validity
;
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
);
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
);
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
;
1401 dprintk("NFS: isize change on server for file %s/%ld "
1405 (long long)cur_isize
,
1406 (long long)new_isize
);
1409 invalid
|= save_cache_validity
& (NFS_INO_INVALID_ATTR
1410 | NFS_INO_REVAL_PAGECACHE
1411 | NFS_INO_REVAL_FORCED
);
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
);
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
;
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
);
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
;
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
);
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
;
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
);
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
);
1462 } else if (server
->caps
& NFS_CAP_NLINK
)
1463 invalid
|= save_cache_validity
& (NFS_INO_INVALID_ATTR
1464 | NFS_INO_REVAL_FORCED
);
1466 if (fattr
->valid
& NFS_ATTR_FATTR_SPACE_USED
) {
1468 * report the blocks in 512byte units
1470 inode
->i_blocks
= nfs_calc_block_size(fattr
->du
.nfs3
.used
);
1472 if (fattr
->valid
& NFS_ATTR_FATTR_BLOCKS_USED
)
1473 inode
->i_blocks
= fattr
->du
.nfs2
.blocks
;
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();
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
;
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
;
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.)
1504 nfs_invalidate_inode(inode
);
1508 struct inode
*nfs_alloc_inode(struct super_block
*sb
)
1510 struct nfs_inode
*nfsi
;
1511 nfsi
= (struct nfs_inode
*)kmem_cache_alloc(nfs_inode_cachep
, GFP_KERNEL
);
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
);
1520 #if IS_ENABLED(CONFIG_NFS_V4)
1521 nfsi
->nfs4_acl
= NULL
;
1522 #endif /* CONFIG_NFS_V4 */
1523 return &nfsi
->vfs_inode
;
1525 EXPORT_SYMBOL_GPL(nfs_alloc_inode
);
1527 static void nfs_i_callback(struct rcu_head
*head
)
1529 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
1530 kmem_cache_free(nfs_inode_cachep
, NFS_I(inode
));
1533 void nfs_destroy_inode(struct inode
*inode
)
1535 call_rcu(&inode
->i_rcu
, nfs_i_callback
);
1537 EXPORT_SYMBOL_GPL(nfs_destroy_inode
);
1539 static inline void nfs4_init_once(struct nfs_inode
*nfsi
)
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
;
1550 static void init_once(void *foo
)
1552 struct nfs_inode
*nfsi
= (struct nfs_inode
*) foo
;
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
);
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
);
1568 static int __init
nfs_init_inodecache(void)
1570 nfs_inode_cachep
= kmem_cache_create("nfs_inode_cache",
1571 sizeof(struct nfs_inode
),
1572 0, (SLAB_RECLAIM_ACCOUNT
|
1575 if (nfs_inode_cachep
== NULL
)
1581 static void nfs_destroy_inodecache(void)
1584 * Make sure all delayed rcu free inodes are flushed before we
1588 kmem_cache_destroy(nfs_inode_cachep
);
1591 struct workqueue_struct
*nfsiod_workqueue
;
1592 EXPORT_SYMBOL_GPL(nfsiod_workqueue
);
1595 * start up the nfsiod workqueue
1597 static int nfsiod_start(void)
1599 struct workqueue_struct
*wq
;
1600 dprintk("RPC: creating workqueue nfsiod\n");
1601 wq
= alloc_workqueue("nfsiod", WQ_MEM_RECLAIM
, 0);
1604 nfsiod_workqueue
= wq
;
1609 * Destroy the nfsiod workqueue
1611 static void nfsiod_stop(void)
1613 struct workqueue_struct
*wq
;
1615 wq
= nfsiod_workqueue
;
1618 nfsiod_workqueue
= NULL
;
1619 destroy_workqueue(wq
);
1623 EXPORT_SYMBOL_GPL(nfs_net_id
);
1625 static int nfs_net_init(struct net
*net
)
1627 nfs_clients_init(net
);
1628 return nfs_dns_resolver_cache_init(net
);
1631 static void nfs_net_exit(struct net
*net
)
1633 nfs_dns_resolver_cache_destroy(net
);
1634 nfs_cleanup_cb_ident_idr(net
);
1637 static struct pernet_operations nfs_net_ops
= {
1638 .init
= nfs_net_init
,
1639 .exit
= nfs_net_exit
,
1641 .size
= sizeof(struct nfs_net
),
1647 static int __init
init_nfs_fs(void)
1651 err
= nfs_dns_resolver_init();
1655 err
= register_pernet_subsys(&nfs_net_ops
);
1659 err
= nfs_fscache_register();
1663 err
= nfsiod_start();
1667 err
= nfs_fs_proc_init();
1671 err
= nfs_init_nfspagecache();
1675 err
= nfs_init_inodecache();
1679 err
= nfs_init_readpagecache();
1683 err
= nfs_init_writepagecache();
1687 err
= nfs_init_directcache();
1691 #ifdef CONFIG_PROC_FS
1692 rpc_proc_register(&init_net
, &nfs_rpcstat
);
1694 if ((err
= register_nfs_fs()) != 0)
1699 #ifdef CONFIG_PROC_FS
1700 rpc_proc_unregister(&init_net
, "nfs");
1702 nfs_destroy_directcache();
1704 nfs_destroy_writepagecache();
1706 nfs_destroy_readpagecache();
1708 nfs_destroy_inodecache();
1710 nfs_destroy_nfspagecache();
1716 nfs_fscache_unregister();
1718 unregister_pernet_subsys(&nfs_net_ops
);
1720 nfs_dns_resolver_destroy();
1725 static void __exit
exit_nfs_fs(void)
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");
1738 unregister_nfs_fs();
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);
1748 module_init(init_nfs_fs
)
1749 module_exit(exit_nfs_fs
)