Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/fs/nfs/inode.c | |
4 | * | |
5 | * Copyright (C) 1992 Rick Sladkey | |
6 | * | |
7 | * nfs inode and superblock handling functions | |
8 | * | |
526719ba | 9 | * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some |
1da177e4 LT |
10 | * experimental NFS changes. Modularisation taken straight from SYS5 fs. |
11 | * | |
12 | * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts. | |
13 | * J.S.Peatfield@damtp.cam.ac.uk | |
14 | * | |
15 | */ | |
16 | ||
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/init.h> | |
174cd4b1 | 19 | #include <linux/sched/signal.h> |
1da177e4 LT |
20 | #include <linux/time.h> |
21 | #include <linux/kernel.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/unistd.h> | |
27 | #include <linux/sunrpc/clnt.h> | |
28 | #include <linux/sunrpc/stats.h> | |
4ece3a2d | 29 | #include <linux/sunrpc/metrics.h> |
1da177e4 LT |
30 | #include <linux/nfs_fs.h> |
31 | #include <linux/nfs_mount.h> | |
32 | #include <linux/nfs4_mount.h> | |
33 | #include <linux/lockd/bind.h> | |
1da177e4 LT |
34 | #include <linux/seq_file.h> |
35 | #include <linux/mount.h> | |
1da177e4 | 36 | #include <linux/vfs.h> |
9cdb3883 MN |
37 | #include <linux/inet.h> |
38 | #include <linux/nfs_xdr.h> | |
5a0e3ad6 | 39 | #include <linux/slab.h> |
3fa0b4e2 | 40 | #include <linux/compat.h> |
d310310c | 41 | #include <linux/freezer.h> |
7c0f6ba6 | 42 | #include <linux/uaccess.h> |
1eb5d98f | 43 | #include <linux/iversion.h> |
1da177e4 | 44 | |
4ce79717 | 45 | #include "nfs4_fs.h" |
a72b4422 | 46 | #include "callback.h" |
1da177e4 | 47 | #include "delegation.h" |
d9ef5a8c | 48 | #include "iostat.h" |
f7b422b1 | 49 | #include "internal.h" |
8ec442ae | 50 | #include "fscache.h" |
e5e94017 | 51 | #include "pnfs.h" |
ab7017a3 | 52 | #include "nfs.h" |
1b340d01 | 53 | #include "netns.h" |
996bc4f4 | 54 | #include "sysfs.h" |
1da177e4 | 55 | |
f4ce1299 TM |
56 | #include "nfstrace.h" |
57 | ||
1da177e4 | 58 | #define NFSDBG_FACILITY NFSDBG_VFS |
1da177e4 | 59 | |
f43bf0be TM |
60 | #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1 |
61 | ||
62 | /* Default is to see 64-bit inode numbers */ | |
90ab5ee9 | 63 | static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED; |
f43bf0be | 64 | |
24aa1fe6 | 65 | static int nfs_update_inode(struct inode *, struct nfs_fattr *); |
1da177e4 | 66 | |
e18b890b | 67 | static struct kmem_cache * nfs_inode_cachep; |
b7fa0554 | 68 | |
1da177e4 LT |
69 | static inline unsigned long |
70 | nfs_fattr_to_ino_t(struct nfs_fattr *fattr) | |
71 | { | |
72 | return nfs_fileid_to_ino_t(fattr->fileid); | |
73 | } | |
74 | ||
210c7c17 | 75 | static int nfs_wait_killable(int mode) |
72cb77f4 | 76 | { |
416ad3c9 | 77 | freezable_schedule_unsafe(); |
dfd01f02 PZ |
78 | if (signal_pending_state(mode, current)) |
79 | return -ERESTARTSYS; | |
72cb77f4 TM |
80 | return 0; |
81 | } | |
210c7c17 BC |
82 | |
83 | int nfs_wait_bit_killable(struct wait_bit_key *key, int mode) | |
84 | { | |
85 | return nfs_wait_killable(mode); | |
86 | } | |
89d77c8f | 87 | EXPORT_SYMBOL_GPL(nfs_wait_bit_killable); |
72cb77f4 | 88 | |
f43bf0be TM |
89 | /** |
90 | * nfs_compat_user_ino64 - returns the user-visible inode number | |
91 | * @fileid: 64-bit fileid | |
92 | * | |
93 | * This function returns a 32-bit inode number if the boot parameter | |
94 | * nfs.enable_ino64 is zero. | |
95 | */ | |
96 | u64 nfs_compat_user_ino64(u64 fileid) | |
97 | { | |
3fa0b4e2 FF |
98 | #ifdef CONFIG_COMPAT |
99 | compat_ulong_t ino; | |
100 | #else | |
101 | unsigned long ino; | |
102 | #endif | |
f43bf0be TM |
103 | |
104 | if (enable_ino64) | |
105 | return fileid; | |
106 | ino = fileid; | |
107 | if (sizeof(ino) < sizeof(fileid)) | |
108 | ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8; | |
109 | return ino; | |
110 | } | |
111 | ||
eed99357 TM |
112 | int nfs_drop_inode(struct inode *inode) |
113 | { | |
114 | return NFS_STALE(inode) || generic_drop_inode(inode); | |
115 | } | |
116 | EXPORT_SYMBOL_GPL(nfs_drop_inode); | |
117 | ||
19d87ca3 | 118 | void nfs_clear_inode(struct inode *inode) |
1da177e4 | 119 | { |
da6d503a TM |
120 | /* |
121 | * The following should never happen... | |
122 | */ | |
f48407dd TM |
123 | WARN_ON_ONCE(nfs_have_writebacks(inode)); |
124 | WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files)); | |
ada70d94 | 125 | nfs_zap_acl_cache(inode); |
1c3c07e9 | 126 | nfs_access_zap_cache(inode); |
f1fe29b4 | 127 | nfs_fscache_clear_inode(inode); |
1da177e4 | 128 | } |
89d77c8f | 129 | EXPORT_SYMBOL_GPL(nfs_clear_inode); |
1da177e4 | 130 | |
b57922d9 AV |
131 | void nfs_evict_inode(struct inode *inode) |
132 | { | |
91b0abe3 | 133 | truncate_inode_pages_final(&inode->i_data); |
dbd5768f | 134 | clear_inode(inode); |
b57922d9 AV |
135 | nfs_clear_inode(inode); |
136 | } | |
137 | ||
9e1681c2 | 138 | int nfs_sync_inode(struct inode *inode) |
4d346bea | 139 | { |
95d9f6c3 | 140 | inode_dio_wait(inode); |
4d346bea TM |
141 | return nfs_wb_all(inode); |
142 | } | |
9e1681c2 | 143 | EXPORT_SYMBOL_GPL(nfs_sync_inode); |
4d346bea | 144 | |
29884df0 TM |
145 | /** |
146 | * nfs_sync_mapping - helper to flush all mmapped dirty data to disk | |
302fad7b | 147 | * @mapping: pointer to struct address_space |
29884df0 TM |
148 | */ |
149 | int nfs_sync_mapping(struct address_space *mapping) | |
150 | { | |
5cf95214 | 151 | int ret = 0; |
29884df0 | 152 | |
5cf95214 TM |
153 | if (mapping->nrpages != 0) { |
154 | unmap_mapping_range(mapping, 0, 0, 0); | |
155 | ret = nfs_wb_all(mapping->host); | |
156 | } | |
29884df0 TM |
157 | return ret; |
158 | } | |
159 | ||
187e593d TM |
160 | static int nfs_attribute_timeout(struct inode *inode) |
161 | { | |
162 | struct nfs_inode *nfsi = NFS_I(inode); | |
163 | ||
164 | return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo); | |
165 | } | |
166 | ||
13c0b082 TM |
167 | static bool nfs_check_cache_flags_invalid(struct inode *inode, |
168 | unsigned long flags) | |
61540bf6 TM |
169 | { |
170 | unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); | |
171 | ||
61540bf6 TM |
172 | return (cache_validity & flags) != 0; |
173 | } | |
174 | ||
61540bf6 TM |
175 | bool nfs_check_cache_invalid(struct inode *inode, unsigned long flags) |
176 | { | |
13c0b082 TM |
177 | if (nfs_check_cache_flags_invalid(inode, flags)) |
178 | return true; | |
179 | return nfs_attribute_cache_expired(inode); | |
61540bf6 | 180 | } |
95ad37f9 | 181 | EXPORT_SYMBOL_GPL(nfs_check_cache_invalid); |
61540bf6 | 182 | |
848fdd62 TM |
183 | #ifdef CONFIG_NFS_V4_2 |
184 | static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) | |
185 | { | |
186 | return nfsi->xattr_cache != NULL; | |
187 | } | |
188 | #else | |
189 | static bool nfs_has_xattr_cache(const struct nfs_inode *nfsi) | |
190 | { | |
191 | return false; | |
192 | } | |
193 | #endif | |
194 | ||
fd6d3fee | 195 | void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) |
6edf9609 TM |
196 | { |
197 | struct nfs_inode *nfsi = NFS_I(inode); | |
3f0b3cf4 TM |
198 | bool have_delegation = NFS_PROTO(inode)->have_delegation(inode, FMODE_READ); |
199 | ||
200 | if (have_delegation) { | |
201 | if (!(flags & NFS_INO_REVAL_FORCED)) | |
720869eb | 202 | flags &= ~(NFS_INO_INVALID_MODE | |
cc7f2dae TM |
203 | NFS_INO_INVALID_OTHER | |
204 | NFS_INO_INVALID_XATTR); | |
205 | flags &= ~(NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); | |
50c7a799 TM |
206 | } else if (flags & NFS_INO_REVAL_PAGECACHE) |
207 | flags |= NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE; | |
6edf9609 | 208 | |
848fdd62 TM |
209 | if (!nfs_has_xattr_cache(nfsi)) |
210 | flags &= ~NFS_INO_INVALID_XATTR; | |
beab450d | 211 | if (flags & NFS_INO_INVALID_DATA) |
a6b5a28e | 212 | nfs_fscache_invalidate(inode, 0); |
cc7f2dae | 213 | flags &= ~(NFS_INO_REVAL_PAGECACHE | NFS_INO_REVAL_FORCED); |
488796ec | 214 | |
6edf9609 | 215 | nfsi->cache_validity |= flags; |
488796ec TM |
216 | |
217 | if (inode->i_mapping->nrpages == 0) | |
218 | nfsi->cache_validity &= ~(NFS_INO_INVALID_DATA | | |
219 | NFS_INO_DATA_INVAL_DEFER); | |
220 | else if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
221 | nfsi->cache_validity &= ~NFS_INO_DATA_INVAL_DEFER; | |
93c2e5e0 | 222 | trace_nfs_set_cache_invalid(inode, 0); |
6edf9609 | 223 | } |
b6f80a2e | 224 | EXPORT_SYMBOL_GPL(nfs_set_cache_invalid); |
6edf9609 | 225 | |
1da177e4 LT |
226 | /* |
227 | * Invalidate the local caches | |
228 | */ | |
b37b03b7 | 229 | static void nfs_zap_caches_locked(struct inode *inode) |
1da177e4 LT |
230 | { |
231 | struct nfs_inode *nfsi = NFS_I(inode); | |
232 | int mode = inode->i_mode; | |
233 | ||
91d5b470 CL |
234 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
235 | ||
c7c20973 TM |
236 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
237 | nfsi->attrtimeo_timestamp = jiffies; | |
1da177e4 | 238 | |
de242c0b | 239 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { |
6edf9609 | 240 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
aa9c2669 DQ |
241 | | NFS_INO_INVALID_DATA |
242 | | NFS_INO_INVALID_ACCESS | |
243 | | NFS_INO_INVALID_ACL | |
95ad37f9 | 244 | | NFS_INO_INVALID_XATTR |
6edf9609 | 245 | | NFS_INO_REVAL_PAGECACHE); |
aa9c2669 | 246 | } else |
6edf9609 | 247 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
aa9c2669 DQ |
248 | | NFS_INO_INVALID_ACCESS |
249 | | NFS_INO_INVALID_ACL | |
95ad37f9 | 250 | | NFS_INO_INVALID_XATTR |
6edf9609 | 251 | | NFS_INO_REVAL_PAGECACHE); |
fd1defc2 | 252 | nfs_zap_label_cache_locked(nfsi); |
b37b03b7 | 253 | } |
dc59250c | 254 | |
b37b03b7 TM |
255 | void nfs_zap_caches(struct inode *inode) |
256 | { | |
257 | spin_lock(&inode->i_lock); | |
258 | nfs_zap_caches_locked(inode); | |
dc59250c | 259 | spin_unlock(&inode->i_lock); |
ada70d94 TM |
260 | } |
261 | ||
cd9ae2b6 TM |
262 | void nfs_zap_mapping(struct inode *inode, struct address_space *mapping) |
263 | { | |
264 | if (mapping->nrpages != 0) { | |
265 | spin_lock(&inode->i_lock); | |
6edf9609 | 266 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
cd9ae2b6 TM |
267 | spin_unlock(&inode->i_lock); |
268 | } | |
269 | } | |
270 | ||
f41f7418 | 271 | void nfs_zap_acl_cache(struct inode *inode) |
ada70d94 TM |
272 | { |
273 | void (*clear_acl_cache)(struct inode *); | |
274 | ||
275 | clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache; | |
276 | if (clear_acl_cache != NULL) | |
277 | clear_acl_cache(inode); | |
dc59250c | 278 | spin_lock(&inode->i_lock); |
55296809 | 279 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL; |
dc59250c | 280 | spin_unlock(&inode->i_lock); |
1da177e4 | 281 | } |
1c606fb7 | 282 | EXPORT_SYMBOL_GPL(nfs_zap_acl_cache); |
1da177e4 | 283 | |
c4812998 TM |
284 | void nfs_invalidate_atime(struct inode *inode) |
285 | { | |
286 | spin_lock(&inode->i_lock); | |
6edf9609 | 287 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); |
c4812998 TM |
288 | spin_unlock(&inode->i_lock); |
289 | } | |
ddda8e0a | 290 | EXPORT_SYMBOL_GPL(nfs_invalidate_atime); |
c4812998 | 291 | |
1da177e4 | 292 | /* |
b37b03b7 TM |
293 | * Invalidate, but do not unhash, the inode. |
294 | * NB: must be called with inode->i_lock held! | |
1da177e4 | 295 | */ |
93ce4af7 | 296 | static void nfs_set_inode_stale_locked(struct inode *inode) |
1da177e4 | 297 | { |
3a10c30a | 298 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
b37b03b7 | 299 | nfs_zap_caches_locked(inode); |
93ce4af7 TM |
300 | trace_nfs_set_inode_stale(inode); |
301 | } | |
302 | ||
303 | void nfs_set_inode_stale(struct inode *inode) | |
304 | { | |
305 | spin_lock(&inode->i_lock); | |
306 | nfs_set_inode_stale_locked(inode); | |
307 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
308 | } |
309 | ||
310 | struct nfs_find_desc { | |
311 | struct nfs_fh *fh; | |
312 | struct nfs_fattr *fattr; | |
313 | }; | |
314 | ||
315 | /* | |
316 | * In NFSv3 we can have 64bit inode numbers. In order to support | |
317 | * this, and re-exported directories (also seen in NFSv2) | |
318 | * we are forced to allow 2 different inodes to have the same | |
319 | * i_ino. | |
320 | */ | |
321 | static int | |
322 | nfs_find_actor(struct inode *inode, void *opaque) | |
323 | { | |
324 | struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque; | |
325 | struct nfs_fh *fh = desc->fh; | |
326 | struct nfs_fattr *fattr = desc->fattr; | |
327 | ||
328 | if (NFS_FILEID(inode) != fattr->fileid) | |
329 | return 0; | |
6e3e2c43 | 330 | if (inode_wrong_type(inode, fattr->mode)) |
f6488c9b | 331 | return 0; |
1da177e4 LT |
332 | if (nfs_compare_fh(NFS_FH(inode), fh)) |
333 | return 0; | |
334 | if (is_bad_inode(inode) || NFS_STALE(inode)) | |
335 | return 0; | |
336 | return 1; | |
337 | } | |
338 | ||
339 | static int | |
340 | nfs_init_locked(struct inode *inode, void *opaque) | |
341 | { | |
342 | struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque; | |
343 | struct nfs_fattr *fattr = desc->fattr; | |
344 | ||
99fadcd7 | 345 | set_nfs_fileid(inode, fattr->fileid); |
916ec34d | 346 | inode->i_mode = fattr->mode; |
1da177e4 LT |
347 | nfs_copy_fh(NFS_FH(inode), desc->fh); |
348 | return 0; | |
349 | } | |
350 | ||
e058f70b | 351 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
fd1defc2 TM |
352 | static void nfs_clear_label_invalid(struct inode *inode) |
353 | { | |
354 | spin_lock(&inode->i_lock); | |
355 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL; | |
356 | spin_unlock(&inode->i_lock); | |
357 | } | |
358 | ||
dd225cb3 | 359 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) |
aa9c2669 DQ |
360 | { |
361 | int error; | |
362 | ||
dd225cb3 | 363 | if (fattr->label == NULL) |
aa9c2669 DQ |
364 | return; |
365 | ||
aa9c2669 | 366 | if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) { |
dd225cb3 AS |
367 | error = security_inode_notifysecctx(inode, fattr->label->label, |
368 | fattr->label->len); | |
aa9c2669 DQ |
369 | if (error) |
370 | printk(KERN_ERR "%s() %s %d " | |
371 | "security_inode_notifysecctx() %d\n", | |
372 | __func__, | |
dd225cb3 AS |
373 | (char *)fattr->label->label, |
374 | fattr->label->len, error); | |
fd1defc2 | 375 | nfs_clear_label_invalid(inode); |
aa9c2669 DQ |
376 | } |
377 | } | |
378 | ||
e058f70b SD |
379 | struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) |
380 | { | |
e48c81bb | 381 | struct nfs4_label *label; |
e058f70b SD |
382 | |
383 | if (!(server->caps & NFS_CAP_SECURITY_LABEL)) | |
e48c81bb | 384 | return NULL; |
e058f70b SD |
385 | |
386 | label = kzalloc(sizeof(struct nfs4_label), flags); | |
387 | if (label == NULL) | |
388 | return ERR_PTR(-ENOMEM); | |
389 | ||
390 | label->label = kzalloc(NFS4_MAXLABELLEN, flags); | |
391 | if (label->label == NULL) { | |
392 | kfree(label); | |
393 | return ERR_PTR(-ENOMEM); | |
394 | } | |
395 | label->len = NFS4_MAXLABELLEN; | |
396 | ||
397 | return label; | |
398 | } | |
399 | EXPORT_SYMBOL_GPL(nfs4_label_alloc); | |
aa9c2669 | 400 | #else |
dd225cb3 | 401 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr) |
aa9c2669 DQ |
402 | { |
403 | } | |
e058f70b | 404 | #endif |
aa9c2669 | 405 | EXPORT_SYMBOL_GPL(nfs_setsecurity); |
e058f70b | 406 | |
f174ff7a PT |
407 | /* Search for inode identified by fh, fileid and i_mode in inode cache. */ |
408 | struct inode * | |
409 | nfs_ilookup(struct super_block *sb, struct nfs_fattr *fattr, struct nfs_fh *fh) | |
410 | { | |
411 | struct nfs_find_desc desc = { | |
412 | .fh = fh, | |
413 | .fattr = fattr, | |
414 | }; | |
415 | struct inode *inode; | |
416 | unsigned long hash; | |
417 | ||
418 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID) || | |
419 | !(fattr->valid & NFS_ATTR_FATTR_TYPE)) | |
420 | return NULL; | |
421 | ||
422 | hash = nfs_fattr_to_ino_t(fattr); | |
423 | inode = ilookup5(sb, hash, nfs_find_actor, &desc); | |
424 | ||
425 | dprintk("%s: returning %p\n", __func__, inode); | |
426 | return inode; | |
427 | } | |
428 | ||
e591b298 TM |
429 | static void nfs_inode_init_regular(struct nfs_inode *nfsi) |
430 | { | |
431 | atomic_long_set(&nfsi->nrequests, 0); | |
432 | INIT_LIST_HEAD(&nfsi->commit_info.list); | |
433 | atomic_long_set(&nfsi->commit_info.ncommit, 0); | |
434 | atomic_set(&nfsi->commit_info.rpcs_out, 0); | |
435 | mutex_init(&nfsi->commit_mutex); | |
436 | } | |
437 | ||
438 | static void nfs_inode_init_dir(struct nfs_inode *nfsi) | |
439 | { | |
440 | nfsi->cache_change_attribute = 0; | |
441 | memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf)); | |
442 | init_rwsem(&nfsi->rmdir_sem); | |
443 | } | |
444 | ||
1da177e4 LT |
445 | /* |
446 | * This is our front-end to iget that looks up inodes by file handle | |
447 | * instead of inode number. | |
448 | */ | |
449 | struct inode * | |
cf7ab00a | 450 | nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr) |
1da177e4 LT |
451 | { |
452 | struct nfs_find_desc desc = { | |
453 | .fh = fh, | |
454 | .fattr = fattr | |
455 | }; | |
03f28e3a | 456 | struct inode *inode = ERR_PTR(-ENOENT); |
ce62b114 | 457 | u64 fattr_supported = NFS_SB(sb)->fattr_valid; |
1da177e4 LT |
458 | unsigned long hash; |
459 | ||
7ebb9315 BS |
460 | nfs_attr_check_mountpoint(sb, fattr); |
461 | ||
2ef47eb1 AS |
462 | if (nfs_attr_use_mounted_on_fileid(fattr)) |
463 | fattr->fileid = fattr->mounted_on_fileid; | |
464 | else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) | |
1da177e4 | 465 | goto out_no_inode; |
9e6e70f8 | 466 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0) |
1da177e4 | 467 | goto out_no_inode; |
1da177e4 LT |
468 | |
469 | hash = nfs_fattr_to_ino_t(fattr); | |
470 | ||
03f28e3a TM |
471 | inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc); |
472 | if (inode == NULL) { | |
473 | inode = ERR_PTR(-ENOMEM); | |
1da177e4 | 474 | goto out_no_inode; |
03f28e3a | 475 | } |
1da177e4 LT |
476 | |
477 | if (inode->i_state & I_NEW) { | |
478 | struct nfs_inode *nfsi = NFS_I(inode); | |
b0c4fddc | 479 | unsigned long now = jiffies; |
1da177e4 LT |
480 | |
481 | /* We set i_ino for the few things that still rely on it, | |
482 | * such as stat(2) */ | |
483 | inode->i_ino = hash; | |
484 | ||
485 | /* We can't support update_atime(), since the server will reset it */ | |
486 | inode->i_flags |= S_NOATIME|S_NOCMTIME; | |
487 | inode->i_mode = fattr->mode; | |
821a868a | 488 | nfsi->cache_validity = 0; |
62ab460c | 489 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0 |
ce62b114 | 490 | && (fattr_supported & NFS_ATTR_FATTR_MODE)) |
720869eb | 491 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE); |
1da177e4 LT |
492 | /* Why so? Because we want revalidate for devices/FIFOs, and |
493 | * that's precisely what we have in nfs_file_inode_operations. | |
494 | */ | |
8fa5c000 | 495 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops; |
1da177e4 | 496 | if (S_ISREG(inode->i_mode)) { |
1788ea6e | 497 | inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops; |
1da177e4 | 498 | inode->i_data.a_ops = &nfs_file_aops; |
e591b298 | 499 | nfs_inode_init_regular(nfsi); |
1da177e4 | 500 | } else if (S_ISDIR(inode->i_mode)) { |
8fa5c000 | 501 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops; |
1da177e4 | 502 | inode->i_fop = &nfs_dir_operations; |
11de3b11 | 503 | inode->i_data.a_ops = &nfs_dir_aops; |
e591b298 | 504 | nfs_inode_init_dir(nfsi); |
55a97593 | 505 | /* Deal with crossing mountpoints */ |
7ebb9315 BS |
506 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT || |
507 | fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) { | |
6b97fd3d MN |
508 | if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) |
509 | inode->i_op = &nfs_referral_inode_operations; | |
510 | else | |
511 | inode->i_op = &nfs_mountpoint_inode_operations; | |
55a97593 | 512 | inode->i_fop = NULL; |
36d43a43 | 513 | inode->i_flags |= S_AUTOMOUNT; |
55a97593 | 514 | } |
21fc61c7 | 515 | } else if (S_ISLNK(inode->i_mode)) { |
1da177e4 | 516 | inode->i_op = &nfs_symlink_inode_operations; |
21fc61c7 AV |
517 | inode_nohighmem(inode); |
518 | } else | |
1da177e4 LT |
519 | init_special_inode(inode, inode->i_mode, fattr->rdev); |
520 | ||
9e6e70f8 TM |
521 | memset(&inode->i_atime, 0, sizeof(inode->i_atime)); |
522 | memset(&inode->i_mtime, 0, sizeof(inode->i_mtime)); | |
523 | memset(&inode->i_ctime, 0, sizeof(inode->i_ctime)); | |
1eb5d98f | 524 | inode_set_iversion_raw(inode, 0); |
9e6e70f8 | 525 | inode->i_size = 0; |
6d6b77f1 | 526 | clear_nlink(inode); |
9ff593c4 EB |
527 | inode->i_uid = make_kuid(&init_user_ns, -2); |
528 | inode->i_gid = make_kgid(&init_user_ns, -2); | |
9e6e70f8 | 529 | inode->i_blocks = 0; |
2701d086 AA |
530 | nfsi->write_io = 0; |
531 | nfsi->read_io = 0; | |
9e6e70f8 | 532 | |
33801147 | 533 | nfsi->read_cache_jiffies = fattr->time_start; |
4704f0e2 | 534 | nfsi->attr_gencount = fattr->gencount; |
9e6e70f8 | 535 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
e86d5a02 | 536 | inode->i_atime = fattr->atime; |
ce62b114 | 537 | else if (fattr_supported & NFS_ATTR_FATTR_ATIME) |
16e14375 | 538 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); |
9e6e70f8 | 539 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) |
e86d5a02 | 540 | inode->i_mtime = fattr->mtime; |
ce62b114 | 541 | else if (fattr_supported & NFS_ATTR_FATTR_MTIME) |
16e14375 | 542 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); |
9e6e70f8 | 543 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
e86d5a02 | 544 | inode->i_ctime = fattr->ctime; |
ce62b114 | 545 | else if (fattr_supported & NFS_ATTR_FATTR_CTIME) |
16e14375 | 546 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CTIME); |
9e6e70f8 | 547 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) |
1eb5d98f | 548 | inode_set_iversion_raw(inode, fattr->change_attr); |
cd812599 | 549 | else |
16e14375 | 550 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE); |
9e6e70f8 TM |
551 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) |
552 | inode->i_size = nfs_size_to_loff_t(fattr->size); | |
62ab460c | 553 | else |
16e14375 | 554 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_SIZE); |
9e6e70f8 | 555 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) |
bfe86848 | 556 | set_nlink(inode, fattr->nlink); |
ce62b114 | 557 | else if (fattr_supported & NFS_ATTR_FATTR_NLINK) |
fabf2b34 | 558 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK); |
9e6e70f8 TM |
559 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) |
560 | inode->i_uid = fattr->uid; | |
ce62b114 | 561 | else if (fattr_supported & NFS_ATTR_FATTR_OWNER) |
16e14375 | 562 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); |
9e6e70f8 TM |
563 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) |
564 | inode->i_gid = fattr->gid; | |
ce62b114 | 565 | else if (fattr_supported & NFS_ATTR_FATTR_GROUP) |
16e14375 | 566 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER); |
0f44da51 FL |
567 | if (nfs_server_capable(inode, NFS_CAP_XATTR)) |
568 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR); | |
9e6e70f8 TM |
569 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
570 | inode->i_blocks = fattr->du.nfs2.blocks; | |
ce62b114 TM |
571 | else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED && |
572 | fattr->size != 0) | |
573 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); | |
9e6e70f8 | 574 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { |
1da177e4 LT |
575 | /* |
576 | * report the blocks in 512byte units | |
577 | */ | |
578 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
ce62b114 TM |
579 | } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED && |
580 | fattr->size != 0) | |
4cdfeb64 | 581 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); |
821a868a | 582 | |
dd225cb3 | 583 | nfs_setsecurity(inode, fattr); |
aa9c2669 | 584 | |
1da177e4 | 585 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
b0c4fddc | 586 | nfsi->attrtimeo_timestamp = now; |
1c3c07e9 | 587 | nfsi->access_cache = RB_ROOT; |
1da177e4 | 588 | |
f1fe29b4 | 589 | nfs_fscache_init_inode(inode); |
ef79c097 | 590 | |
1da177e4 | 591 | unlock_new_inode(inode); |
26fde4df N |
592 | } else { |
593 | int err = nfs_refresh_inode(inode, fattr); | |
594 | if (err < 0) { | |
595 | iput(inode); | |
596 | inode = ERR_PTR(err); | |
597 | goto out_no_inode; | |
598 | } | |
599 | } | |
1e8968c5 | 600 | dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n", |
1da177e4 | 601 | inode->i_sb->s_id, |
1e8968c5 | 602 | (unsigned long long)NFS_FILEID(inode), |
4f1abd22 | 603 | nfs_display_fhandle_hash(fh), |
1da177e4 LT |
604 | atomic_read(&inode->i_count)); |
605 | ||
606 | out: | |
607 | return inode; | |
608 | ||
609 | out_no_inode: | |
03f28e3a | 610 | dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode)); |
1da177e4 LT |
611 | goto out; |
612 | } | |
89d77c8f | 613 | EXPORT_SYMBOL_GPL(nfs_fhget); |
1da177e4 | 614 | |
536e43d1 | 615 | #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) |
1da177e4 LT |
616 | |
617 | int | |
549c7297 CB |
618 | nfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
619 | struct iattr *attr) | |
1da177e4 | 620 | { |
2b0143b5 | 621 | struct inode *inode = d_inode(dentry); |
987f8dfc | 622 | struct nfs_fattr *fattr; |
ae57ca0f | 623 | int error = 0; |
1da177e4 | 624 | |
91d5b470 CL |
625 | nfs_inc_stats(inode, NFSIOS_VFSSETATTR); |
626 | ||
188b95dd JL |
627 | /* skip mode change if it's just for clearing setuid/setgid */ |
628 | if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) | |
629 | attr->ia_valid &= ~ATTR_MODE; | |
630 | ||
1da177e4 | 631 | if (attr->ia_valid & ATTR_SIZE) { |
08a899d5 CH |
632 | BUG_ON(!S_ISREG(inode->i_mode)); |
633 | ||
ae57ca0f KM |
634 | error = inode_newsize_ok(inode, attr->ia_size); |
635 | if (error) | |
636 | return error; | |
637 | ||
638 | if (attr->ia_size == i_size_read(inode)) | |
1da177e4 LT |
639 | attr->ia_valid &= ~ATTR_SIZE; |
640 | } | |
641 | ||
642 | /* Optimization: if the end result is no change, don't RPC */ | |
1f9f4328 | 643 | if (((attr->ia_valid & NFS_VALID_ATTRS) & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
1da177e4 LT |
644 | return 0; |
645 | ||
f4ce1299 TM |
646 | trace_nfs_setattr_enter(inode); |
647 | ||
755c1e20 | 648 | /* Write all dirty data */ |
4d346bea TM |
649 | if (S_ISREG(inode->i_mode)) |
650 | nfs_sync_inode(inode); | |
987f8dfc | 651 | |
1b00ad65 | 652 | fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); |
ae57ca0f KM |
653 | if (fattr == NULL) { |
654 | error = -ENOMEM; | |
987f8dfc | 655 | goto out; |
ae57ca0f KM |
656 | } |
657 | ||
987f8dfc | 658 | error = NFS_PROTO(inode)->setattr(dentry, fattr, attr); |
65e4308d | 659 | if (error == 0) |
aa9c2669 | 660 | error = nfs_refresh_inode(inode, fattr); |
987f8dfc TM |
661 | nfs_free_fattr(fattr); |
662 | out: | |
f4ce1299 | 663 | trace_nfs_setattr_exit(inode, error); |
65e4308d TM |
664 | return error; |
665 | } | |
ddda8e0a | 666 | EXPORT_SYMBOL_GPL(nfs_setattr); |
65e4308d | 667 | |
a3d01454 TM |
668 | /** |
669 | * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall | |
670 | * @inode: inode of the file used | |
671 | * @offset: file offset to start truncating | |
672 | * | |
673 | * This is a copy of the common vmtruncate, but with the locking | |
674 | * corrected to take into account the fact that NFS requires | |
675 | * inode->i_size to be updated under the inode->i_lock. | |
f044636d | 676 | * Note: must be called with inode->i_lock held! |
a3d01454 TM |
677 | */ |
678 | static int nfs_vmtruncate(struct inode * inode, loff_t offset) | |
679 | { | |
c08d3b0e | 680 | int err; |
a3d01454 | 681 | |
c08d3b0e | 682 | err = inode_newsize_ok(inode, offset); |
683 | if (err) | |
684 | goto out; | |
a3d01454 | 685 | |
110cb2d2 | 686 | trace_nfs_size_truncate(inode, offset); |
c08d3b0e | 687 | i_size_write(inode, offset); |
6edf9609 TM |
688 | /* Optimisation */ |
689 | if (offset == 0) | |
1c341b77 TM |
690 | NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_DATA | |
691 | NFS_INO_DATA_INVAL_DEFER); | |
f6cdfa6d | 692 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; |
c08d3b0e | 693 | |
f044636d | 694 | spin_unlock(&inode->i_lock); |
7caef267 | 695 | truncate_pagecache(inode, offset); |
f044636d | 696 | spin_lock(&inode->i_lock); |
c08d3b0e | 697 | out: |
698 | return err; | |
a3d01454 TM |
699 | } |
700 | ||
65e4308d TM |
701 | /** |
702 | * nfs_setattr_update_inode - Update inode metadata after a setattr call. | |
703 | * @inode: pointer to struct inode | |
704 | * @attr: pointer to struct iattr | |
16e14375 | 705 | * @fattr: pointer to struct nfs_fattr |
65e4308d TM |
706 | * |
707 | * Note: we do this in the *proc.c in order to ensure that | |
708 | * it works for things like exclusive creates too. | |
709 | */ | |
f044636d TM |
710 | void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr, |
711 | struct nfs_fattr *fattr) | |
65e4308d | 712 | { |
f044636d TM |
713 | /* Barrier: bump the attribute generation count. */ |
714 | nfs_fattr_set_barrier(fattr); | |
715 | ||
716 | spin_lock(&inode->i_lock); | |
717 | NFS_I(inode)->attr_gencount = fattr->gencount; | |
6a97d02d | 718 | if ((attr->ia_valid & ATTR_SIZE) != 0) { |
4cdfeb64 TM |
719 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME | |
720 | NFS_INO_INVALID_BLOCKS); | |
6a97d02d TM |
721 | nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC); |
722 | nfs_vmtruncate(inode, attr->ia_size); | |
723 | } | |
65e4308d | 724 | if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) { |
6a97d02d | 725 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_CTIME; |
1f9f4328 TM |
726 | if ((attr->ia_valid & ATTR_KILL_SUID) != 0 && |
727 | inode->i_mode & S_ISUID) | |
728 | inode->i_mode &= ~S_ISUID; | |
729 | if ((attr->ia_valid & ATTR_KILL_SGID) != 0 && | |
730 | (inode->i_mode & (S_ISGID | S_IXGRP)) == | |
731 | (S_ISGID | S_IXGRP)) | |
732 | inode->i_mode &= ~S_ISGID; | |
1da177e4 | 733 | if ((attr->ia_valid & ATTR_MODE) != 0) { |
65e4308d TM |
734 | int mode = attr->ia_mode & S_IALLUGO; |
735 | mode |= inode->i_mode & ~S_IALLUGO; | |
1da177e4 LT |
736 | inode->i_mode = mode; |
737 | } | |
738 | if ((attr->ia_valid & ATTR_UID) != 0) | |
739 | inode->i_uid = attr->ia_uid; | |
740 | if ((attr->ia_valid & ATTR_GID) != 0) | |
741 | inode->i_gid = attr->ia_gid; | |
6a97d02d | 742 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
e86d5a02 | 743 | inode->i_ctime = fattr->ctime; |
6a97d02d TM |
744 | else |
745 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
746 | | NFS_INO_INVALID_CTIME); | |
6edf9609 TM |
747 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS |
748 | | NFS_INO_INVALID_ACL); | |
65e4308d | 749 | } |
6a97d02d TM |
750 | if (attr->ia_valid & (ATTR_ATIME_SET|ATTR_ATIME)) { |
751 | NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_ATIME | |
752 | | NFS_INO_INVALID_CTIME); | |
753 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) | |
e86d5a02 | 754 | inode->i_atime = fattr->atime; |
6a97d02d TM |
755 | else if (attr->ia_valid & ATTR_ATIME_SET) |
756 | inode->i_atime = attr->ia_atime; | |
757 | else | |
758 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); | |
759 | ||
760 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
e86d5a02 | 761 | inode->i_ctime = fattr->ctime; |
6a97d02d TM |
762 | else |
763 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
764 | | NFS_INO_INVALID_CTIME); | |
765 | } | |
766 | if (attr->ia_valid & (ATTR_MTIME_SET|ATTR_MTIME)) { | |
767 | NFS_I(inode)->cache_validity &= ~(NFS_INO_INVALID_MTIME | |
768 | | NFS_INO_INVALID_CTIME); | |
769 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) | |
e86d5a02 | 770 | inode->i_mtime = fattr->mtime; |
6a97d02d TM |
771 | else if (attr->ia_valid & ATTR_MTIME_SET) |
772 | inode->i_mtime = attr->ia_mtime; | |
773 | else | |
774 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MTIME); | |
775 | ||
776 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
e86d5a02 | 777 | inode->i_ctime = fattr->ctime; |
6a97d02d TM |
778 | else |
779 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
780 | | NFS_INO_INVALID_CTIME); | |
65e4308d | 781 | } |
616c3196 JL |
782 | if (fattr->valid) |
783 | nfs_update_inode(inode, fattr); | |
f044636d | 784 | spin_unlock(&inode->i_lock); |
1da177e4 | 785 | } |
ddda8e0a | 786 | EXPORT_SYMBOL_GPL(nfs_setattr_update_inode); |
1da177e4 | 787 | |
1bcf4c5c | 788 | static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry) |
311324ad TM |
789 | { |
790 | struct dentry *parent; | |
791 | ||
1bcf4c5c TM |
792 | if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS)) |
793 | return; | |
311324ad | 794 | parent = dget_parent(dentry); |
2b0143b5 | 795 | nfs_force_use_readdirplus(d_inode(parent)); |
311324ad TM |
796 | dput(parent); |
797 | } | |
798 | ||
1bcf4c5c TM |
799 | static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry) |
800 | { | |
801 | struct dentry *parent; | |
802 | ||
803 | if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS)) | |
804 | return; | |
805 | parent = dget_parent(dentry); | |
806 | nfs_advise_use_readdirplus(d_inode(parent)); | |
807 | dput(parent); | |
808 | } | |
809 | ||
63cdd7ed | 810 | static u32 nfs_get_valid_attrmask(struct inode *inode) |
311324ad | 811 | { |
63cdd7ed TM |
812 | unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); |
813 | u32 reply_mask = STATX_INO | STATX_TYPE; | |
814 | ||
815 | if (!(cache_validity & NFS_INO_INVALID_ATIME)) | |
816 | reply_mask |= STATX_ATIME; | |
13c0b082 TM |
817 | if (!(cache_validity & NFS_INO_INVALID_CTIME)) |
818 | reply_mask |= STATX_CTIME; | |
819 | if (!(cache_validity & NFS_INO_INVALID_MTIME)) | |
820 | reply_mask |= STATX_MTIME; | |
821 | if (!(cache_validity & NFS_INO_INVALID_SIZE)) | |
822 | reply_mask |= STATX_SIZE; | |
fabf2b34 TM |
823 | if (!(cache_validity & NFS_INO_INVALID_NLINK)) |
824 | reply_mask |= STATX_NLINK; | |
720869eb TM |
825 | if (!(cache_validity & NFS_INO_INVALID_MODE)) |
826 | reply_mask |= STATX_MODE; | |
63cdd7ed | 827 | if (!(cache_validity & NFS_INO_INVALID_OTHER)) |
720869eb | 828 | reply_mask |= STATX_UID | STATX_GID; |
63cdd7ed TM |
829 | if (!(cache_validity & NFS_INO_INVALID_BLOCKS)) |
830 | reply_mask |= STATX_BLOCKS; | |
831 | return reply_mask; | |
311324ad TM |
832 | } |
833 | ||
549c7297 CB |
834 | int nfs_getattr(struct user_namespace *mnt_userns, const struct path *path, |
835 | struct kstat *stat, u32 request_mask, unsigned int query_flags) | |
1da177e4 | 836 | { |
a528d35e | 837 | struct inode *inode = d_inode(path->dentry); |
9ccee940 TM |
838 | struct nfs_server *server = NFS_SERVER(inode); |
839 | unsigned long cache_validity; | |
16caf5b6 | 840 | int err = 0; |
9ccee940 TM |
841 | bool force_sync = query_flags & AT_STATX_FORCE_SYNC; |
842 | bool do_update = false; | |
1da177e4 | 843 | |
f4ce1299 | 844 | trace_nfs_getattr_enter(inode); |
9ccee940 | 845 | |
4eb6a823 TM |
846 | request_mask &= STATX_TYPE | STATX_MODE | STATX_NLINK | STATX_UID | |
847 | STATX_GID | STATX_ATIME | STATX_MTIME | STATX_CTIME | | |
848 | STATX_INO | STATX_SIZE | STATX_BLOCKS; | |
849 | ||
ac7cbb22 TM |
850 | if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) { |
851 | nfs_readdirplus_parent_cache_hit(path->dentry); | |
63cdd7ed | 852 | goto out_no_revalidate; |
ac7cbb22 | 853 | } |
9ccee940 | 854 | |
acdc53b2 | 855 | /* Flush out writes to the server in order to update c/mtime. */ |
d19e0183 TM |
856 | if ((request_mask & (STATX_CTIME | STATX_MTIME)) && |
857 | S_ISREG(inode->i_mode)) | |
858 | filemap_write_and_wait(inode->i_mapping); | |
fc33a7bb CH |
859 | |
860 | /* | |
861 | * We may force a getattr if the user cares about atime. | |
862 | * | |
863 | * Note that we only have to check the vfsmount flags here: | |
864 | * - NFS always sets S_NOATIME by so checking it would give a | |
865 | * bogus result | |
1751e8a6 | 866 | * - NFS never sets SB_NOATIME or SB_NODIRATIME so there is |
fc33a7bb CH |
867 | * no point in checking those. |
868 | */ | |
a528d35e DH |
869 | if ((path->mnt->mnt_flags & MNT_NOATIME) || |
870 | ((path->mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
9ccee940 TM |
871 | request_mask &= ~STATX_ATIME; |
872 | ||
873 | /* Is the user requesting attributes that might need revalidation? */ | |
874 | if (!(request_mask & (STATX_MODE|STATX_NLINK|STATX_ATIME|STATX_CTIME| | |
875 | STATX_MTIME|STATX_UID|STATX_GID| | |
876 | STATX_SIZE|STATX_BLOCKS))) | |
877 | goto out_no_revalidate; | |
878 | ||
879 | /* Check whether the cached attributes are stale */ | |
880 | do_update |= force_sync || nfs_attribute_cache_expired(inode); | |
881 | cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); | |
e8764a6f | 882 | do_update |= cache_validity & NFS_INO_INVALID_CHANGE; |
9ccee940 TM |
883 | if (request_mask & STATX_ATIME) |
884 | do_update |= cache_validity & NFS_INO_INVALID_ATIME; | |
e8764a6f TM |
885 | if (request_mask & STATX_CTIME) |
886 | do_update |= cache_validity & NFS_INO_INVALID_CTIME; | |
887 | if (request_mask & STATX_MTIME) | |
888 | do_update |= cache_validity & NFS_INO_INVALID_MTIME; | |
889 | if (request_mask & STATX_SIZE) | |
890 | do_update |= cache_validity & NFS_INO_INVALID_SIZE; | |
fabf2b34 TM |
891 | if (request_mask & STATX_NLINK) |
892 | do_update |= cache_validity & NFS_INO_INVALID_NLINK; | |
720869eb TM |
893 | if (request_mask & STATX_MODE) |
894 | do_update |= cache_validity & NFS_INO_INVALID_MODE; | |
895 | if (request_mask & (STATX_UID | STATX_GID)) | |
e8764a6f | 896 | do_update |= cache_validity & NFS_INO_INVALID_OTHER; |
3a39e778 ZB |
897 | if (request_mask & STATX_BLOCKS) |
898 | do_update |= cache_validity & NFS_INO_INVALID_BLOCKS; | |
63cdd7ed | 899 | |
9ccee940 TM |
900 | if (do_update) { |
901 | /* Update the attribute cache */ | |
59b86d85 HT |
902 | if (!(server->flags & NFS_MOUNT_NOAC)) |
903 | nfs_readdirplus_parent_cache_miss(path->dentry); | |
904 | else | |
905 | nfs_readdirplus_parent_cache_hit(path->dentry); | |
311324ad | 906 | err = __nfs_revalidate_inode(server, inode); |
9ccee940 TM |
907 | if (err) |
908 | goto out; | |
1bcf4c5c | 909 | } else |
a528d35e | 910 | nfs_readdirplus_parent_cache_hit(path->dentry); |
9ccee940 TM |
911 | out_no_revalidate: |
912 | /* Only return attributes that were revalidated. */ | |
63cdd7ed TM |
913 | stat->result_mask = nfs_get_valid_attrmask(inode) | request_mask; |
914 | ||
0d56a451 | 915 | generic_fillattr(&init_user_ns, inode, stat); |
9ccee940 TM |
916 | stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode)); |
917 | if (S_ISDIR(inode->i_mode)) | |
918 | stat->blksize = NFS_SERVER(inode)->dtsize; | |
acdc53b2 | 919 | out: |
f4ce1299 | 920 | trace_nfs_getattr_exit(inode, err); |
1da177e4 LT |
921 | return err; |
922 | } | |
ddda8e0a | 923 | EXPORT_SYMBOL_GPL(nfs_getattr); |
1da177e4 | 924 | |
f11ac8db TM |
925 | static void nfs_init_lock_context(struct nfs_lock_context *l_ctx) |
926 | { | |
2f62b5aa | 927 | refcount_set(&l_ctx->count, 1); |
d51fdb87 | 928 | l_ctx->lockowner = current->files; |
f11ac8db | 929 | INIT_LIST_HEAD(&l_ctx->list); |
210c7c17 | 930 | atomic_set(&l_ctx->io_count, 0); |
f11ac8db TM |
931 | } |
932 | ||
933 | static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx) | |
934 | { | |
1db97eaa | 935 | struct nfs_lock_context *pos; |
f11ac8db | 936 | |
1db97eaa | 937 | list_for_each_entry_rcu(pos, &ctx->lock_context.list, list) { |
d51fdb87 | 938 | if (pos->lockowner != current->files) |
f11ac8db | 939 | continue; |
1db97eaa TM |
940 | if (refcount_inc_not_zero(&pos->count)) |
941 | return pos; | |
942 | } | |
f11ac8db TM |
943 | return NULL; |
944 | } | |
945 | ||
946 | struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx) | |
947 | { | |
948 | struct nfs_lock_context *res, *new = NULL; | |
2b0143b5 | 949 | struct inode *inode = d_inode(ctx->dentry); |
f11ac8db | 950 | |
1db97eaa | 951 | rcu_read_lock(); |
f11ac8db | 952 | res = __nfs_find_lock_context(ctx); |
1db97eaa | 953 | rcu_read_unlock(); |
f11ac8db | 954 | if (res == NULL) { |
d7867712 | 955 | new = kmalloc(sizeof(*new), GFP_KERNEL_ACCOUNT); |
f11ac8db | 956 | if (new == NULL) |
b3c54de6 | 957 | return ERR_PTR(-ENOMEM); |
f11ac8db TM |
958 | nfs_init_lock_context(new); |
959 | spin_lock(&inode->i_lock); | |
960 | res = __nfs_find_lock_context(ctx); | |
961 | if (res == NULL) { | |
15494511 TM |
962 | new->open_context = get_nfs_open_context(ctx); |
963 | if (new->open_context) { | |
964 | list_add_tail_rcu(&new->list, | |
965 | &ctx->lock_context.list); | |
966 | res = new; | |
967 | new = NULL; | |
968 | } else | |
969 | res = ERR_PTR(-EBADF); | |
f11ac8db | 970 | } |
1db97eaa TM |
971 | spin_unlock(&inode->i_lock); |
972 | kfree(new); | |
f11ac8db | 973 | } |
f11ac8db TM |
974 | return res; |
975 | } | |
1c6dcbe5 | 976 | EXPORT_SYMBOL_GPL(nfs_get_lock_context); |
f11ac8db TM |
977 | |
978 | void nfs_put_lock_context(struct nfs_lock_context *l_ctx) | |
979 | { | |
980 | struct nfs_open_context *ctx = l_ctx->open_context; | |
2b0143b5 | 981 | struct inode *inode = d_inode(ctx->dentry); |
f11ac8db | 982 | |
2f62b5aa | 983 | if (!refcount_dec_and_lock(&l_ctx->count, &inode->i_lock)) |
f11ac8db | 984 | return; |
1db97eaa | 985 | list_del_rcu(&l_ctx->list); |
f11ac8db | 986 | spin_unlock(&inode->i_lock); |
15494511 | 987 | put_nfs_open_context(ctx); |
1db97eaa | 988 | kfree_rcu(l_ctx, rcu_head); |
f11ac8db | 989 | } |
1c6dcbe5 | 990 | EXPORT_SYMBOL_GPL(nfs_put_lock_context); |
f11ac8db | 991 | |
7fe5c398 TM |
992 | /** |
993 | * nfs_close_context - Common close_context() routine NFSv2/v3 | |
994 | * @ctx: pointer to context | |
995 | * @is_sync: is this a synchronous close | |
996 | * | |
5cf9d706 TM |
997 | * Ensure that the attributes are up to date if we're mounted |
998 | * with close-to-open semantics and we have cached data that will | |
999 | * need to be revalidated on open. | |
7fe5c398 TM |
1000 | */ |
1001 | void nfs_close_context(struct nfs_open_context *ctx, int is_sync) | |
1002 | { | |
5cf9d706 | 1003 | struct nfs_inode *nfsi; |
7fe5c398 | 1004 | struct inode *inode; |
7fe5c398 TM |
1005 | |
1006 | if (!(ctx->mode & FMODE_WRITE)) | |
1007 | return; | |
1008 | if (!is_sync) | |
1009 | return; | |
2b0143b5 | 1010 | inode = d_inode(ctx->dentry); |
58ff4184 TM |
1011 | if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) |
1012 | return; | |
5cf9d706 TM |
1013 | nfsi = NFS_I(inode); |
1014 | if (inode->i_mapping->nrpages == 0) | |
1015 | return; | |
1016 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
1017 | return; | |
1018 | if (!list_empty(&nfsi->open_files)) | |
7fe5c398 | 1019 | return; |
1f3208b2 | 1020 | if (NFS_SERVER(inode)->flags & NFS_MOUNT_NOCTO) |
7fe5c398 | 1021 | return; |
1f3208b2 TM |
1022 | nfs_revalidate_inode(inode, |
1023 | NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE); | |
7fe5c398 | 1024 | } |
ddda8e0a | 1025 | EXPORT_SYMBOL_GPL(nfs_close_context); |
7fe5c398 | 1026 | |
532d4def N |
1027 | struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, |
1028 | fmode_t f_mode, | |
1029 | struct file *filp) | |
1da177e4 LT |
1030 | { |
1031 | struct nfs_open_context *ctx; | |
1032 | ||
d7867712 | 1033 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT); |
1d179d6b | 1034 | if (!ctx) |
5ede7b1c | 1035 | return ERR_PTR(-ENOMEM); |
5ede7b1c AV |
1036 | nfs_sb_active(dentry->d_sb); |
1037 | ctx->dentry = dget(dentry); | |
1d179d6b TM |
1038 | if (filp) |
1039 | ctx->cred = get_cred(filp->f_cred); | |
1040 | else | |
1041 | ctx->cred = get_current_cred(); | |
ca05cbae | 1042 | rcu_assign_pointer(ctx->ll_cred, NULL); |
5ede7b1c AV |
1043 | ctx->state = NULL; |
1044 | ctx->mode = f_mode; | |
1045 | ctx->flags = 0; | |
1046 | ctx->error = 0; | |
532d4def | 1047 | ctx->flock_owner = (fl_owner_t)filp; |
5ede7b1c AV |
1048 | nfs_init_lock_context(&ctx->lock_context); |
1049 | ctx->lock_context.open_context = ctx; | |
1050 | INIT_LIST_HEAD(&ctx->list); | |
82be417a | 1051 | ctx->mdsthreshold = NULL; |
1da177e4 LT |
1052 | return ctx; |
1053 | } | |
89d77c8f | 1054 | EXPORT_SYMBOL_GPL(alloc_nfs_open_context); |
1da177e4 LT |
1055 | |
1056 | struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) | |
1057 | { | |
0de43976 TM |
1058 | if (ctx != NULL && refcount_inc_not_zero(&ctx->lock_context.count)) |
1059 | return ctx; | |
1060 | return NULL; | |
1da177e4 | 1061 | } |
89d77c8f | 1062 | EXPORT_SYMBOL_GPL(get_nfs_open_context); |
1da177e4 | 1063 | |
7fe5c398 | 1064 | static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) |
1da177e4 | 1065 | { |
2b0143b5 | 1066 | struct inode *inode = d_inode(ctx->dentry); |
3d4ff43d | 1067 | struct super_block *sb = ctx->dentry->d_sb; |
3bec63db | 1068 | |
0de43976 TM |
1069 | if (!refcount_dec_and_test(&ctx->lock_context.count)) |
1070 | return; | |
5c78f58e | 1071 | if (!list_empty(&ctx->list)) { |
0de43976 TM |
1072 | spin_lock(&inode->i_lock); |
1073 | list_del_rcu(&ctx->list); | |
ef84303e | 1074 | spin_unlock(&inode->i_lock); |
0de43976 | 1075 | } |
5c78f58e TM |
1076 | if (inode != NULL) |
1077 | NFS_PROTO(inode)->close_context(ctx, is_sync); | |
a52458b4 | 1078 | put_cred(ctx->cred); |
3d4ff43d | 1079 | dput(ctx->dentry); |
322b2b90 | 1080 | nfs_sb_deactive(sb); |
ca05cbae | 1081 | put_rpccred(rcu_dereference_protected(ctx->ll_cred, 1)); |
82be417a | 1082 | kfree(ctx->mdsthreshold); |
0de43976 | 1083 | kfree_rcu(ctx, rcu_head); |
3bec63db TM |
1084 | } |
1085 | ||
a49c3c77 TM |
1086 | void put_nfs_open_context(struct nfs_open_context *ctx) |
1087 | { | |
1088 | __put_nfs_open_context(ctx, 0); | |
1089 | } | |
89d77c8f | 1090 | EXPORT_SYMBOL_GPL(put_nfs_open_context); |
a49c3c77 | 1091 | |
4eae5014 TM |
1092 | static void put_nfs_open_context_sync(struct nfs_open_context *ctx) |
1093 | { | |
1094 | __put_nfs_open_context(ctx, 1); | |
1095 | } | |
1096 | ||
1da177e4 LT |
1097 | /* |
1098 | * Ensure that mmap has a recent RPC credential for use when writing out | |
1099 | * shared pages | |
1100 | */ | |
c45ffdd2 | 1101 | void nfs_inode_attach_open_context(struct nfs_open_context *ctx) |
1da177e4 | 1102 | { |
2b0143b5 | 1103 | struct inode *inode = d_inode(ctx->dentry); |
1da177e4 LT |
1104 | struct nfs_inode *nfsi = NFS_I(inode); |
1105 | ||
1da177e4 | 1106 | spin_lock(&inode->i_lock); |
1c341b77 TM |
1107 | if (list_empty(&nfsi->open_files) && |
1108 | (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) | |
ac46b3d7 TM |
1109 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA | |
1110 | NFS_INO_REVAL_FORCED); | |
0de43976 | 1111 | list_add_tail_rcu(&ctx->list, &nfsi->open_files); |
1da177e4 LT |
1112 | spin_unlock(&inode->i_lock); |
1113 | } | |
c45ffdd2 TM |
1114 | EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context); |
1115 | ||
1116 | void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx) | |
1117 | { | |
1118 | filp->private_data = get_nfs_open_context(ctx); | |
e97bc663 | 1119 | set_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); |
c45ffdd2 TM |
1120 | if (list_empty(&ctx->list)) |
1121 | nfs_inode_attach_open_context(ctx); | |
1122 | } | |
89d77c8f | 1123 | EXPORT_SYMBOL_GPL(nfs_file_set_open_context); |
1da177e4 | 1124 | |
d530838b TM |
1125 | /* |
1126 | * Given an inode, search for an open context with the desired characteristics | |
1127 | */ | |
a52458b4 | 1128 | struct nfs_open_context *nfs_find_open_context(struct inode *inode, const struct cred *cred, fmode_t mode) |
1da177e4 LT |
1129 | { |
1130 | struct nfs_inode *nfsi = NFS_I(inode); | |
1131 | struct nfs_open_context *pos, *ctx = NULL; | |
1132 | ||
0de43976 TM |
1133 | rcu_read_lock(); |
1134 | list_for_each_entry_rcu(pos, &nfsi->open_files, list) { | |
65f51603 | 1135 | if (cred != NULL && cred_fscmp(pos->cred, cred) != 0) |
d530838b | 1136 | continue; |
1544fa0f TM |
1137 | if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode) |
1138 | continue; | |
e97bc663 TM |
1139 | if (!test_bit(NFS_CONTEXT_FILE_OPEN, &pos->flags)) |
1140 | continue; | |
1544fa0f | 1141 | ctx = get_nfs_open_context(pos); |
0de43976 TM |
1142 | if (ctx) |
1143 | break; | |
1da177e4 | 1144 | } |
0de43976 | 1145 | rcu_read_unlock(); |
1da177e4 LT |
1146 | return ctx; |
1147 | } | |
1148 | ||
aff8d8dc | 1149 | void nfs_file_clear_open_context(struct file *filp) |
1da177e4 | 1150 | { |
cd3758e3 | 1151 | struct nfs_open_context *ctx = nfs_file_open_context(filp); |
1da177e4 LT |
1152 | |
1153 | if (ctx) { | |
2b0143b5 | 1154 | struct inode *inode = d_inode(ctx->dentry); |
c45ffdd2 | 1155 | |
e97bc663 | 1156 | clear_bit(NFS_CONTEXT_FILE_OPEN, &ctx->flags); |
0bcbf039 PT |
1157 | /* |
1158 | * We fatal error on write before. Try to writeback | |
1159 | * every page again. | |
1160 | */ | |
1161 | if (ctx->error < 0) | |
1162 | invalidate_inode_pages2(inode->i_mapping); | |
1da177e4 | 1163 | filp->private_data = NULL; |
4eae5014 | 1164 | put_nfs_open_context_sync(ctx); |
1da177e4 LT |
1165 | } |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | * These allocate and release file read/write context information. | |
1170 | */ | |
1171 | int nfs_open(struct inode *inode, struct file *filp) | |
1172 | { | |
1173 | struct nfs_open_context *ctx; | |
1da177e4 | 1174 | |
532d4def | 1175 | ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp); |
5ede7b1c AV |
1176 | if (IS_ERR(ctx)) |
1177 | return PTR_ERR(ctx); | |
1da177e4 LT |
1178 | nfs_file_set_open_context(filp, ctx); |
1179 | put_nfs_open_context(ctx); | |
f1fe29b4 | 1180 | nfs_fscache_open_file(inode, filp); |
1da177e4 LT |
1181 | return 0; |
1182 | } | |
44942b4e | 1183 | EXPORT_SYMBOL_GPL(nfs_open); |
1da177e4 | 1184 | |
1da177e4 LT |
1185 | /* |
1186 | * This function is called whenever some part of NFS notices that | |
1187 | * the cached attributes have to be refreshed. | |
1188 | */ | |
1189 | int | |
1190 | __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode) | |
1191 | { | |
1192 | int status = -ESTALE; | |
a3cba2aa | 1193 | struct nfs_fattr *fattr = NULL; |
1da177e4 | 1194 | struct nfs_inode *nfsi = NFS_I(inode); |
1da177e4 | 1195 | |
1e8968c5 NV |
1196 | dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n", |
1197 | inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode)); | |
1da177e4 | 1198 | |
f4ce1299 TM |
1199 | trace_nfs_revalidate_inode_enter(inode); |
1200 | ||
85233a7a | 1201 | if (is_bad_inode(inode)) |
691beb13 | 1202 | goto out; |
1da177e4 | 1203 | if (NFS_STALE(inode)) |
412d582e | 1204 | goto out; |
7fdc49c4 | 1205 | |
ac46bd37 TM |
1206 | /* pNFS: Attributes aren't updated until we layoutcommit */ |
1207 | if (S_ISREG(inode->i_mode)) { | |
1208 | status = pnfs_sync_inode(inode, false); | |
1209 | if (status) | |
1210 | goto out; | |
1211 | } | |
1212 | ||
a3cba2aa | 1213 | status = -ENOMEM; |
2ef61e0e | 1214 | fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); |
a3cba2aa TM |
1215 | if (fattr == NULL) |
1216 | goto out; | |
1217 | ||
691beb13 | 1218 | nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE); |
14c43f76 | 1219 | |
2ef61e0e | 1220 | status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, inode); |
1da177e4 | 1221 | if (status != 0) { |
1e8968c5 | 1222 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n", |
1da177e4 | 1223 | inode->i_sb->s_id, |
1e8968c5 | 1224 | (unsigned long long)NFS_FILEID(inode), status); |
c74dfe97 TM |
1225 | switch (status) { |
1226 | case -ETIMEDOUT: | |
1227 | /* A soft timeout occurred. Use cached information? */ | |
1228 | if (server->flags & NFS_MOUNT_SOFTREVAL) | |
1229 | status = 0; | |
1230 | break; | |
1231 | case -ESTALE: | |
1da177e4 | 1232 | if (!S_ISDIR(inode->i_mode)) |
93ce4af7 TM |
1233 | nfs_set_inode_stale(inode); |
1234 | else | |
1235 | nfs_zap_caches(inode); | |
1da177e4 | 1236 | } |
2ef61e0e | 1237 | goto out; |
1da177e4 LT |
1238 | } |
1239 | ||
a3cba2aa | 1240 | status = nfs_refresh_inode(inode, fattr); |
1da177e4 | 1241 | if (status) { |
1e8968c5 | 1242 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n", |
1da177e4 | 1243 | inode->i_sb->s_id, |
1e8968c5 | 1244 | (unsigned long long)NFS_FILEID(inode), status); |
2ef61e0e | 1245 | goto out; |
1da177e4 | 1246 | } |
55296809 | 1247 | |
24aa1fe6 | 1248 | if (nfsi->cache_validity & NFS_INO_INVALID_ACL) |
ada70d94 | 1249 | nfs_zap_acl_cache(inode); |
55296809 | 1250 | |
dd225cb3 | 1251 | nfs_setsecurity(inode, fattr); |
3da580aa | 1252 | |
1e8968c5 | 1253 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n", |
1da177e4 | 1254 | inode->i_sb->s_id, |
1e8968c5 | 1255 | (unsigned long long)NFS_FILEID(inode)); |
1da177e4 | 1256 | |
14c43f76 | 1257 | out: |
a3cba2aa | 1258 | nfs_free_fattr(fattr); |
f4ce1299 | 1259 | trace_nfs_revalidate_inode_exit(inode, status); |
1da177e4 LT |
1260 | return status; |
1261 | } | |
1262 | ||
43f291cd | 1263 | int nfs_attribute_cache_expired(struct inode *inode) |
d7cf8dd0 | 1264 | { |
b4d2314b | 1265 | if (nfs_have_delegated_attributes(inode)) |
1da177e4 | 1266 | return 0; |
d7cf8dd0 | 1267 | return nfs_attribute_timeout(inode); |
1da177e4 LT |
1268 | } |
1269 | ||
1270 | /** | |
1271 | * nfs_revalidate_inode - Revalidate the inode attributes | |
302fad7b | 1272 | * @inode: pointer to inode struct |
1f3208b2 | 1273 | * @flags: cache flags to check |
1da177e4 LT |
1274 | * |
1275 | * Updates inode attribute information by retrieving the data from the server. | |
1276 | */ | |
1f3208b2 | 1277 | int nfs_revalidate_inode(struct inode *inode, unsigned long flags) |
1da177e4 | 1278 | { |
1f3208b2 | 1279 | if (!nfs_check_cache_invalid(inode, flags)) |
1da177e4 | 1280 | return NFS_STALE(inode) ? -ESTALE : 0; |
1f3208b2 | 1281 | return __nfs_revalidate_inode(NFS_SERVER(inode), inode); |
1da177e4 | 1282 | } |
1c606fb7 | 1283 | EXPORT_SYMBOL_GPL(nfs_revalidate_inode); |
1da177e4 | 1284 | |
1cda707d | 1285 | static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping) |
717d44e8 | 1286 | { |
f8806c84 TM |
1287 | int ret; |
1288 | ||
a6b5a28e | 1289 | nfs_fscache_invalidate(inode, 0); |
717d44e8 | 1290 | if (mapping->nrpages != 0) { |
f8806c84 TM |
1291 | if (S_ISREG(inode->i_mode)) { |
1292 | ret = nfs_sync_mapping(mapping); | |
1293 | if (ret < 0) | |
1294 | return ret; | |
1295 | } | |
1296 | ret = invalidate_inode_pages2(mapping); | |
717d44e8 TM |
1297 | if (ret < 0) |
1298 | return ret; | |
1299 | } | |
717d44e8 | 1300 | nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); |
f4ce1299 | 1301 | |
1e8968c5 NV |
1302 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n", |
1303 | inode->i_sb->s_id, | |
1304 | (unsigned long long)NFS_FILEID(inode)); | |
717d44e8 TM |
1305 | return 0; |
1306 | } | |
1307 | ||
717d44e8 | 1308 | /** |
28aa2f9e | 1309 | * nfs_clear_invalid_mapping - Conditionally clear a mapping |
302fad7b | 1310 | * @mapping: pointer to mapping |
28aa2f9e TM |
1311 | * |
1312 | * If the NFS_INO_INVALID_DATA inode flag is set, clear the mapping. | |
717d44e8 | 1313 | */ |
28aa2f9e | 1314 | int nfs_clear_invalid_mapping(struct address_space *mapping) |
717d44e8 | 1315 | { |
28aa2f9e | 1316 | struct inode *inode = mapping->host; |
717d44e8 | 1317 | struct nfs_inode *nfsi = NFS_I(inode); |
d529ef83 | 1318 | unsigned long *bitlock = &nfsi->flags; |
717d44e8 | 1319 | int ret = 0; |
dc59250c | 1320 | |
d529ef83 JL |
1321 | /* |
1322 | * We must clear NFS_INO_INVALID_DATA first to ensure that | |
1323 | * invalidations that come in while we're shooting down the mappings | |
1324 | * are respected. But, that leaves a race window where one revalidator | |
1325 | * can clear the flag, and then another checks it before the mapping | |
1326 | * gets invalidated. Fix that by serializing access to this part of | |
1327 | * the function. | |
1328 | * | |
1329 | * At the same time, we need to allow other tasks to see whether we | |
1330 | * might be in the middle of invalidating the pages, so we only set | |
1331 | * the bit lock here if it looks like we're going to be doing that. | |
1332 | */ | |
1333 | for (;;) { | |
74316201 N |
1334 | ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING, |
1335 | nfs_wait_bit_killable, TASK_KILLABLE); | |
d529ef83 JL |
1336 | if (ret) |
1337 | goto out; | |
17dfeb91 TM |
1338 | spin_lock(&inode->i_lock); |
1339 | if (test_bit(NFS_INO_INVALIDATING, bitlock)) { | |
1340 | spin_unlock(&inode->i_lock); | |
1341 | continue; | |
1342 | } | |
1343 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
d529ef83 | 1344 | break; |
d529ef83 | 1345 | spin_unlock(&inode->i_lock); |
17dfeb91 | 1346 | goto out; |
f4ce1299 TM |
1347 | } |
1348 | ||
17dfeb91 | 1349 | set_bit(NFS_INO_INVALIDATING, bitlock); |
4db72b40 | 1350 | smp_wmb(); |
28aa2f9e TM |
1351 | nfsi->cache_validity &= |
1352 | ~(NFS_INO_INVALID_DATA | NFS_INO_DATA_INVAL_DEFER); | |
17dfeb91 TM |
1353 | spin_unlock(&inode->i_lock); |
1354 | trace_nfs_invalidate_mapping_enter(inode); | |
be527494 | 1355 | ret = nfs_invalidate_mapping(inode, mapping); |
17dfeb91 TM |
1356 | trace_nfs_invalidate_mapping_exit(inode, ret); |
1357 | ||
d529ef83 | 1358 | clear_bit_unlock(NFS_INO_INVALIDATING, bitlock); |
4e857c58 | 1359 | smp_mb__after_atomic(); |
d529ef83 | 1360 | wake_up_bit(bitlock, NFS_INO_INVALIDATING); |
cd9ae2b6 | 1361 | out: |
44b11874 | 1362 | return ret; |
7d52e862 TM |
1363 | } |
1364 | ||
28aa2f9e TM |
1365 | bool nfs_mapping_need_revalidate_inode(struct inode *inode) |
1366 | { | |
13c0b082 | 1367 | return nfs_check_cache_invalid(inode, NFS_INO_INVALID_CHANGE) || |
28aa2f9e TM |
1368 | NFS_STALE(inode); |
1369 | } | |
1370 | ||
1371 | int nfs_revalidate_mapping_rcu(struct inode *inode) | |
1372 | { | |
1373 | struct nfs_inode *nfsi = NFS_I(inode); | |
1374 | unsigned long *bitlock = &nfsi->flags; | |
1375 | int ret = 0; | |
1376 | ||
1377 | if (IS_SWAPFILE(inode)) | |
1378 | goto out; | |
1379 | if (nfs_mapping_need_revalidate_inode(inode)) { | |
1380 | ret = -ECHILD; | |
1381 | goto out; | |
1382 | } | |
1383 | spin_lock(&inode->i_lock); | |
1384 | if (test_bit(NFS_INO_INVALIDATING, bitlock) || | |
1385 | (nfsi->cache_validity & NFS_INO_INVALID_DATA)) | |
1386 | ret = -ECHILD; | |
1387 | spin_unlock(&inode->i_lock); | |
1388 | out: | |
1389 | return ret; | |
1390 | } | |
1391 | ||
1392 | /** | |
1393 | * nfs_revalidate_mapping - Revalidate the pagecache | |
1394 | * @inode: pointer to host inode | |
1395 | * @mapping: pointer to mapping | |
1396 | */ | |
1397 | int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping) | |
1398 | { | |
1399 | /* swapfiles are not supposed to be shared. */ | |
1400 | if (IS_SWAPFILE(inode)) | |
1401 | return 0; | |
1402 | ||
1403 | if (nfs_mapping_need_revalidate_inode(inode)) { | |
1404 | int ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode); | |
1405 | if (ret < 0) | |
1406 | return ret; | |
1407 | } | |
1408 | ||
1409 | return nfs_clear_invalid_mapping(mapping); | |
1410 | } | |
1411 | ||
ca0daa27 | 1412 | static bool nfs_file_has_writers(struct nfs_inode *nfsi) |
874f9463 | 1413 | { |
ca0daa27 TM |
1414 | struct inode *inode = &nfsi->vfs_inode; |
1415 | ||
ca0daa27 TM |
1416 | if (!S_ISREG(inode->i_mode)) |
1417 | return false; | |
1418 | if (list_empty(&nfsi->open_files)) | |
1419 | return false; | |
6ba0c4e5 | 1420 | return inode_is_open_for_write(inode); |
874f9463 TM |
1421 | } |
1422 | ||
651b0e70 | 1423 | static bool nfs_file_has_buffered_writers(struct nfs_inode *nfsi) |
874f9463 | 1424 | { |
651b0e70 | 1425 | return nfs_file_has_writers(nfsi) && nfs_file_io_is_buffered(nfsi); |
874f9463 TM |
1426 | } |
1427 | ||
783b194c | 1428 | static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
a895b4a1 | 1429 | { |
e86d5a02 | 1430 | struct timespec64 ts; |
95582b00 | 1431 | |
9e6e70f8 TM |
1432 | if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE) |
1433 | && (fattr->valid & NFS_ATTR_FATTR_CHANGE) | |
c472c07b | 1434 | && inode_eq_iversion_raw(inode, fattr->pre_change_attr)) { |
1eb5d98f | 1435 | inode_set_iversion_raw(inode, fattr->change_attr); |
70ca8852 | 1436 | if (S_ISDIR(inode->i_mode)) |
6edf9609 | 1437 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
0f44da51 FL |
1438 | else if (nfs_server_capable(inode, NFS_CAP_XATTR)) |
1439 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_XATTR); | |
70ca8852 | 1440 | } |
a895b4a1 | 1441 | /* If we have atomic WCC data, we may update some attributes */ |
e86d5a02 | 1442 | ts = inode->i_ctime; |
9e6e70f8 TM |
1443 | if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME) |
1444 | && (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
e86d5a02 TM |
1445 | && timespec64_equal(&ts, &fattr->pre_ctime)) { |
1446 | inode->i_ctime = fattr->ctime; | |
27dc1cd3 | 1447 | } |
9e6e70f8 | 1448 | |
e86d5a02 | 1449 | ts = inode->i_mtime; |
9e6e70f8 TM |
1450 | if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME) |
1451 | && (fattr->valid & NFS_ATTR_FATTR_MTIME) | |
e86d5a02 TM |
1452 | && timespec64_equal(&ts, &fattr->pre_mtime)) { |
1453 | inode->i_mtime = fattr->mtime; | |
a895b4a1 | 1454 | } |
9e6e70f8 TM |
1455 | if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE) |
1456 | && (fattr->valid & NFS_ATTR_FATTR_SIZE) | |
1457 | && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size) | |
a6b6d5b8 | 1458 | && !nfs_have_writebacks(inode)) { |
110cb2d2 | 1459 | trace_nfs_size_wcc(inode, fattr->size); |
27dc1cd3 | 1460 | i_size_write(inode, nfs_size_to_loff_t(fattr->size)); |
27dc1cd3 | 1461 | } |
a895b4a1 TM |
1462 | } |
1463 | ||
1da177e4 | 1464 | /** |
33801147 | 1465 | * nfs_check_inode_attributes - verify consistency of the inode attribute cache |
302fad7b TM |
1466 | * @inode: pointer to inode |
1467 | * @fattr: updated attributes | |
1da177e4 LT |
1468 | * |
1469 | * Verifies the attribute cache. If we have just changed the attributes, | |
1470 | * so that fattr carries weak cache consistency data, then it may | |
1471 | * also update the ctime/mtime/change_attribute. | |
1472 | */ | |
33801147 | 1473 | static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 LT |
1474 | { |
1475 | struct nfs_inode *nfsi = NFS_I(inode); | |
1476 | loff_t cur_size, new_isize; | |
2a3f5fd4 | 1477 | unsigned long invalid = 0; |
e86d5a02 | 1478 | struct timespec64 ts; |
dc59250c | 1479 | |
4ebe83af | 1480 | if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) |
01da47bd | 1481 | return 0; |
4ebe83af | 1482 | |
eb3d8f42 TM |
1483 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { |
1484 | /* Only a mounted-on-fileid? Just exit */ | |
1485 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) | |
1486 | return 0; | |
ca62b9c3 | 1487 | /* Has the inode gone and changed behind our back? */ |
eb3d8f42 | 1488 | } else if (nfsi->fileid != fattr->fileid) { |
7e10cc25 TM |
1489 | /* Is this perhaps the mounted-on fileid? */ |
1490 | if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && | |
1491 | nfsi->fileid == fattr->mounted_on_fileid) | |
1492 | return 0; | |
cc89684c | 1493 | return -ESTALE; |
7e10cc25 | 1494 | } |
6e3e2c43 | 1495 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) |
cc89684c | 1496 | return -ESTALE; |
ca62b9c3 | 1497 | |
7e10cc25 | 1498 | |
651b0e70 | 1499 | if (!nfs_file_has_buffered_writers(nfsi)) { |
ca0daa27 | 1500 | /* Verify a few of the more important attributes */ |
c472c07b | 1501 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && !inode_eq_iversion_raw(inode, fattr->change_attr)) |
04c63498 | 1502 | invalid |= NFS_INO_INVALID_CHANGE; |
1da177e4 | 1503 | |
e86d5a02 TM |
1504 | ts = inode->i_mtime; |
1505 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec64_equal(&ts, &fattr->mtime)) | |
16e14375 | 1506 | invalid |= NFS_INO_INVALID_MTIME; |
ca62b9c3 | 1507 | |
e86d5a02 TM |
1508 | ts = inode->i_ctime; |
1509 | if ((fattr->valid & NFS_ATTR_FATTR_CTIME) && !timespec64_equal(&ts, &fattr->ctime)) | |
16e14375 | 1510 | invalid |= NFS_INO_INVALID_CTIME; |
ca0daa27 TM |
1511 | |
1512 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { | |
1513 | cur_size = i_size_read(inode); | |
1514 | new_isize = nfs_size_to_loff_t(fattr->size); | |
1515 | if (cur_size != new_isize) | |
04c63498 | 1516 | invalid |= NFS_INO_INVALID_SIZE; |
ca0daa27 | 1517 | } |
9e6e70f8 | 1518 | } |
1da177e4 LT |
1519 | |
1520 | /* Have any file permissions changed? */ | |
9e6e70f8 | 1521 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) |
04c63498 | 1522 | invalid |= NFS_INO_INVALID_MODE; |
9ff593c4 | 1523 | if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid)) |
04c63498 | 1524 | invalid |= NFS_INO_INVALID_OTHER; |
9ff593c4 | 1525 | if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid)) |
04c63498 | 1526 | invalid |= NFS_INO_INVALID_OTHER; |
1da177e4 LT |
1527 | |
1528 | /* Has the link count changed? */ | |
9e6e70f8 | 1529 | if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink) |
fabf2b34 | 1530 | invalid |= NFS_INO_INVALID_NLINK; |
1da177e4 | 1531 | |
e86d5a02 TM |
1532 | ts = inode->i_atime; |
1533 | if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec64_equal(&ts, &fattr->atime)) | |
2a3f5fd4 TM |
1534 | invalid |= NFS_INO_INVALID_ATIME; |
1535 | ||
1536 | if (invalid != 0) | |
4ebe83af | 1537 | nfs_set_cache_invalid(inode, invalid); |
1da177e4 | 1538 | |
33801147 | 1539 | nfsi->read_cache_jiffies = fattr->time_start; |
1da177e4 LT |
1540 | return 0; |
1541 | } | |
1542 | ||
ae05f269 | 1543 | static atomic_long_t nfs_attr_generation_counter; |
4704f0e2 TM |
1544 | |
1545 | static unsigned long nfs_read_attr_generation_counter(void) | |
1546 | { | |
ae05f269 | 1547 | return atomic_long_read(&nfs_attr_generation_counter); |
4704f0e2 TM |
1548 | } |
1549 | ||
1550 | unsigned long nfs_inc_attr_generation_counter(void) | |
1551 | { | |
ae05f269 | 1552 | return atomic_long_inc_return(&nfs_attr_generation_counter); |
4704f0e2 | 1553 | } |
3235b403 | 1554 | EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter); |
4704f0e2 TM |
1555 | |
1556 | void nfs_fattr_init(struct nfs_fattr *fattr) | |
1557 | { | |
1558 | fattr->valid = 0; | |
1559 | fattr->time_start = jiffies; | |
1560 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
6926afd1 TM |
1561 | fattr->owner_name = NULL; |
1562 | fattr->group_name = NULL; | |
4704f0e2 | 1563 | } |
ddda8e0a | 1564 | EXPORT_SYMBOL_GPL(nfs_fattr_init); |
4704f0e2 | 1565 | |
140e049c TM |
1566 | /** |
1567 | * nfs_fattr_set_barrier | |
1568 | * @fattr: attributes | |
1569 | * | |
1570 | * Used to set a barrier after an attribute was updated. This | |
1571 | * barrier ensures that older attributes from RPC calls that may | |
1572 | * have raced with our update cannot clobber these new values. | |
1573 | * Note that you are still responsible for ensuring that other | |
1574 | * operations which change the attribute on the server do not | |
1575 | * collide. | |
1576 | */ | |
1577 | void nfs_fattr_set_barrier(struct nfs_fattr *fattr) | |
1578 | { | |
1579 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
1580 | } | |
1581 | ||
2d36bfde TM |
1582 | struct nfs_fattr *nfs_alloc_fattr(void) |
1583 | { | |
1584 | struct nfs_fattr *fattr; | |
1585 | ||
1586 | fattr = kmalloc(sizeof(*fattr), GFP_NOFS); | |
d4a95a7e | 1587 | if (fattr != NULL) { |
2d36bfde | 1588 | nfs_fattr_init(fattr); |
d4a95a7e TM |
1589 | fattr->label = NULL; |
1590 | } | |
2d36bfde TM |
1591 | return fattr; |
1592 | } | |
ddda8e0a | 1593 | EXPORT_SYMBOL_GPL(nfs_alloc_fattr); |
2d36bfde | 1594 | |
d755ad8d AS |
1595 | struct nfs_fattr *nfs_alloc_fattr_with_label(struct nfs_server *server) |
1596 | { | |
1597 | struct nfs_fattr *fattr = nfs_alloc_fattr(); | |
1598 | ||
1599 | if (!fattr) | |
1600 | return NULL; | |
1601 | ||
1602 | fattr->label = nfs4_label_alloc(server, GFP_NOFS); | |
1603 | if (IS_ERR(fattr->label)) { | |
1604 | kfree(fattr); | |
1605 | return NULL; | |
1606 | } | |
1607 | ||
1608 | return fattr; | |
1609 | } | |
1610 | EXPORT_SYMBOL_GPL(nfs_alloc_fattr_with_label); | |
1611 | ||
2d36bfde TM |
1612 | struct nfs_fh *nfs_alloc_fhandle(void) |
1613 | { | |
1614 | struct nfs_fh *fh; | |
1615 | ||
1616 | fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS); | |
1617 | if (fh != NULL) | |
1618 | fh->size = 0; | |
1619 | return fh; | |
1620 | } | |
ddda8e0a | 1621 | EXPORT_SYMBOL_GPL(nfs_alloc_fhandle); |
2d36bfde | 1622 | |
e27d359e | 1623 | #ifdef NFS_DEBUG |
d8e0539e WAA |
1624 | /* |
1625 | * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle | |
1626 | * in the same way that wireshark does | |
1627 | * | |
1628 | * @fh: file handle | |
1629 | * | |
1630 | * For debugging only. | |
1631 | */ | |
1632 | u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh) | |
1633 | { | |
1634 | /* wireshark uses 32-bit AUTODIN crc and does a bitwise | |
1635 | * not on the result */ | |
1264a2f0 | 1636 | return nfs_fhandle_hash(fh); |
d8e0539e | 1637 | } |
9e6ee76d | 1638 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash); |
d8e0539e WAA |
1639 | |
1640 | /* | |
20d27e92 CL |
1641 | * _nfs_display_fhandle - display an NFS file handle on the console |
1642 | * | |
1643 | * @fh: file handle to display | |
1644 | * @caption: display caption | |
1645 | * | |
1646 | * For debugging only. | |
1647 | */ | |
20d27e92 CL |
1648 | void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption) |
1649 | { | |
1650 | unsigned short i; | |
1651 | ||
fa68a1ba | 1652 | if (fh == NULL || fh->size == 0) { |
20d27e92 CL |
1653 | printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh); |
1654 | return; | |
1655 | } | |
1656 | ||
d8e0539e WAA |
1657 | printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n", |
1658 | caption, fh, fh->size, _nfs_display_fhandle_hash(fh)); | |
20d27e92 CL |
1659 | for (i = 0; i < fh->size; i += 16) { |
1660 | __be32 *pos = (__be32 *)&fh->data[i]; | |
1661 | ||
1662 | switch ((fh->size - i - 1) >> 2) { | |
1663 | case 0: | |
1664 | printk(KERN_DEFAULT " %08x\n", | |
1665 | be32_to_cpup(pos)); | |
1666 | break; | |
1667 | case 1: | |
1668 | printk(KERN_DEFAULT " %08x %08x\n", | |
1669 | be32_to_cpup(pos), be32_to_cpup(pos + 1)); | |
1670 | break; | |
1671 | case 2: | |
1672 | printk(KERN_DEFAULT " %08x %08x %08x\n", | |
1673 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1674 | be32_to_cpup(pos + 2)); | |
1675 | break; | |
1676 | default: | |
1677 | printk(KERN_DEFAULT " %08x %08x %08x %08x\n", | |
1678 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1679 | be32_to_cpup(pos + 2), be32_to_cpup(pos + 3)); | |
1680 | } | |
1681 | } | |
1682 | } | |
9e6ee76d | 1683 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle); |
20d27e92 CL |
1684 | #endif |
1685 | ||
a10ad176 | 1686 | /** |
6f9be83d | 1687 | * nfs_inode_attrs_cmp_generic - compare attributes |
302fad7b | 1688 | * @fattr: attributes |
302fad7b | 1689 | * @inode: pointer to inode |
a10ad176 TM |
1690 | * |
1691 | * Attempt to divine whether or not an RPC call reply carrying stale | |
1692 | * attributes got scheduled after another call carrying updated ones. | |
4704f0e2 | 1693 | * Note also the check for wraparound of 'attr_gencount' |
a10ad176 | 1694 | * |
6f9be83d TM |
1695 | * The function returns '1' if it thinks the attributes in @fattr are |
1696 | * more recent than the ones cached in @inode. Otherwise it returns | |
1697 | * the value '0'. | |
a10ad176 | 1698 | */ |
6f9be83d TM |
1699 | static int nfs_inode_attrs_cmp_generic(const struct nfs_fattr *fattr, |
1700 | const struct inode *inode) | |
a10ad176 | 1701 | { |
9fdbfad1 | 1702 | unsigned long attr_gencount = NFS_I(inode)->attr_gencount; |
a10ad176 | 1703 | |
9fdbfad1 TM |
1704 | return (long)(fattr->gencount - attr_gencount) > 0 || |
1705 | (long)(attr_gencount - nfs_read_attr_generation_counter()) > 0; | |
a10ad176 TM |
1706 | } |
1707 | ||
6f9be83d TM |
1708 | /** |
1709 | * nfs_inode_attrs_cmp_monotonic - compare attributes | |
302fad7b | 1710 | * @fattr: attributes |
6f9be83d | 1711 | * @inode: pointer to inode |
a10ad176 TM |
1712 | * |
1713 | * Attempt to divine whether or not an RPC call reply carrying stale | |
1714 | * attributes got scheduled after another call carrying updated ones. | |
1715 | * | |
6f9be83d TM |
1716 | * We assume that the server observes monotonic semantics for |
1717 | * the change attribute, so a larger value means that the attributes in | |
1718 | * @fattr are more recent, in which case the function returns the | |
1719 | * value '1'. | |
1720 | * A return value of '0' indicates no measurable change | |
1721 | * A return value of '-1' means that the attributes in @inode are | |
1722 | * more recent. | |
1723 | */ | |
1724 | static int nfs_inode_attrs_cmp_monotonic(const struct nfs_fattr *fattr, | |
1725 | const struct inode *inode) | |
a10ad176 | 1726 | { |
6f9be83d TM |
1727 | s64 diff = fattr->change_attr - inode_peek_iversion_raw(inode); |
1728 | if (diff > 0) | |
1729 | return 1; | |
1730 | return diff == 0 ? 0 : -1; | |
1731 | } | |
1732 | ||
1733 | /** | |
1734 | * nfs_inode_attrs_cmp_strict_monotonic - compare attributes | |
1735 | * @fattr: attributes | |
1736 | * @inode: pointer to inode | |
a10ad176 | 1737 | * |
6f9be83d TM |
1738 | * Attempt to divine whether or not an RPC call reply carrying stale |
1739 | * attributes got scheduled after another call carrying updated ones. | |
a10ad176 | 1740 | * |
6f9be83d TM |
1741 | * We assume that the server observes strictly monotonic semantics for |
1742 | * the change attribute, so a larger value means that the attributes in | |
1743 | * @fattr are more recent, in which case the function returns the | |
1744 | * value '1'. | |
1745 | * A return value of '-1' means that the attributes in @inode are | |
1746 | * more recent or unchanged. | |
a10ad176 | 1747 | */ |
6f9be83d TM |
1748 | static int nfs_inode_attrs_cmp_strict_monotonic(const struct nfs_fattr *fattr, |
1749 | const struct inode *inode) | |
a10ad176 | 1750 | { |
6f9be83d TM |
1751 | return nfs_inode_attrs_cmp_monotonic(fattr, inode) > 0 ? 1 : -1; |
1752 | } | |
a10ad176 | 1753 | |
6f9be83d TM |
1754 | /** |
1755 | * nfs_inode_attrs_cmp - compare attributes | |
1756 | * @fattr: attributes | |
1757 | * @inode: pointer to inode | |
1758 | * | |
1759 | * This function returns '1' if it thinks the attributes in @fattr are | |
1760 | * more recent than the ones cached in @inode. It returns '-1' if | |
1761 | * the attributes in @inode are more recent than the ones in @fattr, | |
1762 | * and it returns 0 if not sure. | |
1763 | */ | |
1764 | static int nfs_inode_attrs_cmp(const struct nfs_fattr *fattr, | |
1765 | const struct inode *inode) | |
1766 | { | |
1767 | if (nfs_inode_attrs_cmp_generic(fattr, inode) > 0) | |
1768 | return 1; | |
1769 | switch (NFS_SERVER(inode)->change_attr_type) { | |
1770 | case NFS4_CHANGE_TYPE_IS_UNDEFINED: | |
1771 | break; | |
1772 | case NFS4_CHANGE_TYPE_IS_TIME_METADATA: | |
1773 | if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) | |
1774 | break; | |
1775 | return nfs_inode_attrs_cmp_monotonic(fattr, inode); | |
1776 | default: | |
1777 | if (!(fattr->valid & NFS_ATTR_FATTR_CHANGE)) | |
1778 | break; | |
1779 | return nfs_inode_attrs_cmp_strict_monotonic(fattr, inode); | |
1780 | } | |
1781 | return 0; | |
1782 | } | |
1783 | ||
7b24dacf TM |
1784 | /** |
1785 | * nfs_inode_finish_partial_attr_update - complete a previous inode update | |
1786 | * @fattr: attributes | |
1787 | * @inode: pointer to inode | |
1788 | * | |
1789 | * Returns '1' if the last attribute update left the inode cached | |
1790 | * attributes in a partially unrevalidated state, and @fattr | |
1791 | * matches the change attribute of that partial update. | |
1792 | * Otherwise returns '0'. | |
1793 | */ | |
1794 | static int nfs_inode_finish_partial_attr_update(const struct nfs_fattr *fattr, | |
1795 | const struct inode *inode) | |
1796 | { | |
1797 | const unsigned long check_valid = | |
1798 | NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME | | |
1799 | NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | | |
1800 | NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_OTHER | | |
1801 | NFS_INO_INVALID_NLINK; | |
1802 | unsigned long cache_validity = NFS_I(inode)->cache_validity; | |
eea41330 | 1803 | enum nfs4_change_attr_type ctype = NFS_SERVER(inode)->change_attr_type; |
7b24dacf | 1804 | |
eea41330 TM |
1805 | if (ctype != NFS4_CHANGE_TYPE_IS_UNDEFINED && |
1806 | !(cache_validity & NFS_INO_INVALID_CHANGE) && | |
7b24dacf TM |
1807 | (cache_validity & check_valid) != 0 && |
1808 | (fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && | |
1809 | nfs_inode_attrs_cmp_monotonic(fattr, inode) == 0) | |
1810 | return 1; | |
1811 | return 0; | |
a10ad176 TM |
1812 | } |
1813 | ||
6f9be83d TM |
1814 | static int nfs_refresh_inode_locked(struct inode *inode, |
1815 | struct nfs_fattr *fattr) | |
a10ad176 | 1816 | { |
6f9be83d TM |
1817 | int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); |
1818 | int ret = 0; | |
f4ce1299 TM |
1819 | |
1820 | trace_nfs_refresh_inode_enter(inode); | |
1821 | ||
7b24dacf | 1822 | if (attr_cmp > 0 || nfs_inode_finish_partial_attr_update(fattr, inode)) |
f4ce1299 | 1823 | ret = nfs_update_inode(inode, fattr); |
6f9be83d | 1824 | else if (attr_cmp == 0) |
f4ce1299 TM |
1825 | ret = nfs_check_inode_attributes(inode, fattr); |
1826 | ||
1827 | trace_nfs_refresh_inode_exit(inode, ret); | |
1828 | return ret; | |
870a5be8 TM |
1829 | } |
1830 | ||
33801147 TM |
1831 | /** |
1832 | * nfs_refresh_inode - try to update the inode attribute cache | |
302fad7b TM |
1833 | * @inode: pointer to inode |
1834 | * @fattr: updated attributes | |
33801147 TM |
1835 | * |
1836 | * Check that an RPC call that returned attributes has not overlapped with | |
1837 | * other recent updates of the inode metadata, then decide whether it is | |
1838 | * safe to do a full update of the inode attributes, or whether just to | |
1839 | * call nfs_check_inode_attributes. | |
1840 | */ | |
1841 | int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr) | |
1842 | { | |
33801147 TM |
1843 | int status; |
1844 | ||
1845 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) | |
1846 | return 0; | |
1847 | spin_lock(&inode->i_lock); | |
870a5be8 | 1848 | status = nfs_refresh_inode_locked(inode, fattr); |
33801147 | 1849 | spin_unlock(&inode->i_lock); |
ef79c097 | 1850 | |
33801147 TM |
1851 | return status; |
1852 | } | |
ddda8e0a | 1853 | EXPORT_SYMBOL_GPL(nfs_refresh_inode); |
33801147 | 1854 | |
16e14375 TM |
1855 | static int nfs_post_op_update_inode_locked(struct inode *inode, |
1856 | struct nfs_fattr *fattr, unsigned int invalid) | |
d65f557f | 1857 | { |
6edf9609 TM |
1858 | if (S_ISDIR(inode->i_mode)) |
1859 | invalid |= NFS_INO_INVALID_DATA; | |
1860 | nfs_set_cache_invalid(inode, invalid); | |
d65f557f TM |
1861 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) |
1862 | return 0; | |
1863 | return nfs_refresh_inode_locked(inode, fattr); | |
1864 | } | |
1865 | ||
decf491f TM |
1866 | /** |
1867 | * nfs_post_op_update_inode - try to update the inode attribute cache | |
302fad7b TM |
1868 | * @inode: pointer to inode |
1869 | * @fattr: updated attributes | |
decf491f TM |
1870 | * |
1871 | * After an operation that has changed the inode metadata, mark the | |
1872 | * attribute cache as being invalid, then try to update it. | |
f551e44f CL |
1873 | * |
1874 | * NB: if the server didn't return any post op attributes, this | |
1875 | * function will force the retrieval of attributes before the next | |
1876 | * NFS request. Thus it should be used only for operations that | |
1877 | * are expected to change one or more attributes, to avoid | |
1878 | * unnecessary NFS requests and trips through nfs_update_inode(). | |
decf491f TM |
1879 | */ |
1880 | int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr) | |
1881 | { | |
d65f557f | 1882 | int status; |
decf491f | 1883 | |
7668fdbe | 1884 | spin_lock(&inode->i_lock); |
92d64e47 | 1885 | nfs_fattr_set_barrier(fattr); |
16e14375 TM |
1886 | status = nfs_post_op_update_inode_locked(inode, fattr, |
1887 | NFS_INO_INVALID_CHANGE | |
d554168f TM |
1888 | | NFS_INO_INVALID_CTIME |
1889 | | NFS_INO_REVAL_FORCED); | |
7668fdbe | 1890 | spin_unlock(&inode->i_lock); |
aa9c2669 | 1891 | |
870a5be8 | 1892 | return status; |
decf491f | 1893 | } |
1c606fb7 | 1894 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode); |
decf491f | 1895 | |
70ca8852 | 1896 | /** |
a08a8cd3 | 1897 | * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache |
302fad7b TM |
1898 | * @inode: pointer to inode |
1899 | * @fattr: updated attributes | |
70ca8852 TM |
1900 | * |
1901 | * After an operation that has changed the inode metadata, mark the | |
1902 | * attribute cache as being invalid, then try to update it. Fake up | |
1903 | * weak cache consistency data, if none exist. | |
1904 | * | |
1905 | * This function is mainly designed to be used by the ->write_done() functions. | |
1906 | */ | |
a08a8cd3 | 1907 | int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr) |
70ca8852 | 1908 | { |
6f9be83d | 1909 | int attr_cmp = nfs_inode_attrs_cmp(fattr, inode); |
d65f557f TM |
1910 | int status; |
1911 | ||
d65f557f | 1912 | /* Don't do a WCC update if these attributes are already stale */ |
6f9be83d TM |
1913 | if (attr_cmp < 0) |
1914 | return 0; | |
1915 | if ((fattr->valid & NFS_ATTR_FATTR) == 0 || !attr_cmp) { | |
9e6e70f8 TM |
1916 | fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE |
1917 | | NFS_ATTR_FATTR_PRESIZE | |
1918 | | NFS_ATTR_FATTR_PREMTIME | |
1919 | | NFS_ATTR_FATTR_PRECTIME); | |
d65f557f TM |
1920 | goto out_noforce; |
1921 | } | |
9e6e70f8 TM |
1922 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && |
1923 | (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) { | |
1eb5d98f | 1924 | fattr->pre_change_attr = inode_peek_iversion_raw(inode); |
9e6e70f8 | 1925 | fattr->valid |= NFS_ATTR_FATTR_PRECHANGE; |
70ca8852 | 1926 | } |
9e6e70f8 TM |
1927 | if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 && |
1928 | (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) { | |
e86d5a02 | 1929 | fattr->pre_ctime = inode->i_ctime; |
9e6e70f8 TM |
1930 | fattr->valid |= NFS_ATTR_FATTR_PRECTIME; |
1931 | } | |
1932 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 && | |
1933 | (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) { | |
e86d5a02 | 1934 | fattr->pre_mtime = inode->i_mtime; |
9e6e70f8 TM |
1935 | fattr->valid |= NFS_ATTR_FATTR_PREMTIME; |
1936 | } | |
1937 | if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 && | |
1938 | (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) { | |
a3d01454 | 1939 | fattr->pre_size = i_size_read(inode); |
9e6e70f8 | 1940 | fattr->valid |= NFS_ATTR_FATTR_PRESIZE; |
70ca8852 | 1941 | } |
d65f557f | 1942 | out_noforce: |
16e14375 TM |
1943 | status = nfs_post_op_update_inode_locked(inode, fattr, |
1944 | NFS_INO_INVALID_CHANGE | |
1945 | | NFS_INO_INVALID_CTIME | |
3a39e778 ZB |
1946 | | NFS_INO_INVALID_MTIME |
1947 | | NFS_INO_INVALID_BLOCKS); | |
a08a8cd3 TM |
1948 | return status; |
1949 | } | |
1950 | ||
1951 | /** | |
1952 | * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache | |
302fad7b TM |
1953 | * @inode: pointer to inode |
1954 | * @fattr: updated attributes | |
a08a8cd3 TM |
1955 | * |
1956 | * After an operation that has changed the inode metadata, mark the | |
1957 | * attribute cache as being invalid, then try to update it. Fake up | |
1958 | * weak cache consistency data, if none exist. | |
1959 | * | |
1960 | * This function is mainly designed to be used by the ->write_done() functions. | |
1961 | */ | |
1962 | int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr) | |
1963 | { | |
1964 | int status; | |
1965 | ||
1966 | spin_lock(&inode->i_lock); | |
8f8ba1d7 | 1967 | nfs_fattr_set_barrier(fattr); |
a08a8cd3 | 1968 | status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr); |
d65f557f TM |
1969 | spin_unlock(&inode->i_lock); |
1970 | return status; | |
70ca8852 | 1971 | } |
ddda8e0a | 1972 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc); |
70ca8852 | 1973 | |
ea96d1ec | 1974 | |
1da177e4 LT |
1975 | /* |
1976 | * Many nfs protocol calls return the new file attributes after | |
1977 | * an operation. Here we update the inode to reflect the state | |
1978 | * of the server's inode. | |
1979 | * | |
1980 | * This is a bit tricky because we have to make sure all dirty pages | |
1981 | * have been sent off to the server before calling invalidate_inode_pages. | |
1982 | * To make sure no other process adds more write requests while we try | |
1983 | * our best to flush them, we make them sleep during the attribute refresh. | |
1984 | * | |
1985 | * A very similar scenario holds for the dir cache. | |
1986 | */ | |
24aa1fe6 | 1987 | static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 | 1988 | { |
ce62b114 | 1989 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 | 1990 | struct nfs_inode *nfsi = NFS_I(inode); |
951a143b | 1991 | loff_t cur_isize, new_isize; |
ce62b114 | 1992 | u64 fattr_supported = server->fattr_valid; |
2a3f5fd4 | 1993 | unsigned long invalid = 0; |
3e7d950a | 1994 | unsigned long now = jiffies; |
62ab460c | 1995 | unsigned long save_cache_validity; |
651b0e70 | 1996 | bool have_writers = nfs_file_has_buffered_writers(nfsi); |
944171cb | 1997 | bool cache_revalidated = true; |
0b467264 | 1998 | bool attr_changed = false; |
c80d17c5 | 1999 | bool have_delegation; |
1da177e4 | 2000 | |
1e8968c5 | 2001 | dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n", |
3110ff80 | 2002 | __func__, inode->i_sb->s_id, inode->i_ino, |
4f1abd22 | 2003 | nfs_display_fhandle_hash(NFS_FH(inode)), |
1da177e4 LT |
2004 | atomic_read(&inode->i_count), fattr->valid); |
2005 | ||
eb3d8f42 TM |
2006 | if (!(fattr->valid & NFS_ATTR_FATTR_FILEID)) { |
2007 | /* Only a mounted-on-fileid? Just exit */ | |
2008 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) | |
2009 | return 0; | |
7e10cc25 | 2010 | /* Has the inode gone and changed behind our back? */ |
eb3d8f42 | 2011 | } else if (nfsi->fileid != fattr->fileid) { |
7e10cc25 TM |
2012 | /* Is this perhaps the mounted-on fileid? */ |
2013 | if ((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) && | |
2014 | nfsi->fileid == fattr->mounted_on_fileid) | |
2015 | return 0; | |
e73e6c9e MT |
2016 | printk(KERN_ERR "NFS: server %s error: fileid changed\n" |
2017 | "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n", | |
2018 | NFS_SERVER(inode)->nfs_client->cl_hostname, | |
2019 | inode->i_sb->s_id, (long long)nfsi->fileid, | |
2020 | (long long)fattr->fileid); | |
2021 | goto out_err; | |
2022 | } | |
1da177e4 LT |
2023 | |
2024 | /* | |
2025 | * Make sure the inode's type hasn't changed. | |
2026 | */ | |
6e3e2c43 | 2027 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && inode_wrong_type(inode, fattr->mode)) { |
e73e6c9e MT |
2028 | /* |
2029 | * Big trouble! The inode has become a different object. | |
2030 | */ | |
1e8968c5 | 2031 | printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n", |
e73e6c9e MT |
2032 | __func__, inode->i_ino, inode->i_mode, fattr->mode); |
2033 | goto out_err; | |
2034 | } | |
1da177e4 | 2035 | |
a0356862 | 2036 | /* Update the fsid? */ |
9e6e70f8 | 2037 | if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) && |
c37dcd33 | 2038 | !nfs_fsid_equal(&server->fsid, &fattr->fsid) && |
36d43a43 | 2039 | !IS_AUTOMOUNT(inode)) |
8b4bdcf8 TM |
2040 | server->fsid = fattr->fsid; |
2041 | ||
c80d17c5 TM |
2042 | /* Save the delegation state before clearing cache_validity */ |
2043 | have_delegation = nfs_have_delegated_attributes(inode); | |
2044 | ||
1da177e4 LT |
2045 | /* |
2046 | * Update the read time so we don't revalidate too often. | |
2047 | */ | |
33801147 | 2048 | nfsi->read_cache_jiffies = fattr->time_start; |
3e7d950a | 2049 | |
62ab460c TM |
2050 | save_cache_validity = nfsi->cache_validity; |
2051 | nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR | |
2052 | | NFS_INO_INVALID_ATIME | |
2053 | | NFS_INO_REVAL_FORCED | |
3a39e778 | 2054 | | NFS_INO_INVALID_BLOCKS); |
1da177e4 | 2055 | |
a895b4a1 | 2056 | /* Do atomic weak cache consistency updates */ |
783b194c | 2057 | nfs_wcc_update_inode(inode, fattr); |
a895b4a1 | 2058 | |
944171cb | 2059 | if (pnfs_layoutcommit_outstanding(inode)) { |
709fa576 TM |
2060 | nfsi->cache_validity |= |
2061 | save_cache_validity & | |
2062 | (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME | | |
2063 | NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | | |
cc7f2dae | 2064 | NFS_INO_INVALID_BLOCKS); |
944171cb BC |
2065 | cache_revalidated = false; |
2066 | } | |
10b7e9ad | 2067 | |
47aabaa7 | 2068 | /* More cache consistency checks */ |
9e6e70f8 | 2069 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) { |
c472c07b | 2070 | if (!inode_eq_iversion_raw(inode, fattr->change_attr)) { |
38512aa9 | 2071 | /* Could it be a race with writeback? */ |
c80d17c5 | 2072 | if (!(have_writers || have_delegation)) { |
0b467264 | 2073 | invalid |= NFS_INO_INVALID_DATA |
38512aa9 | 2074 | | NFS_INO_INVALID_ACCESS |
95ad37f9 FL |
2075 | | NFS_INO_INVALID_ACL |
2076 | | NFS_INO_INVALID_XATTR; | |
cac88f94 | 2077 | /* Force revalidate of all attributes */ |
16e14375 TM |
2078 | save_cache_validity |= NFS_INO_INVALID_CTIME |
2079 | | NFS_INO_INVALID_MTIME | |
2080 | | NFS_INO_INVALID_SIZE | |
4cdfeb64 | 2081 | | NFS_INO_INVALID_BLOCKS |
fabf2b34 | 2082 | | NFS_INO_INVALID_NLINK |
720869eb | 2083 | | NFS_INO_INVALID_MODE |
16e14375 | 2084 | | NFS_INO_INVALID_OTHER; |
38512aa9 TM |
2085 | if (S_ISDIR(inode->i_mode)) |
2086 | nfs_force_lookup_revalidate(inode); | |
a9601ac5 | 2087 | attr_changed = true; |
c80d17c5 TM |
2088 | dprintk("NFS: change_attr change on server for file %s/%ld\n", |
2089 | inode->i_sb->s_id, | |
2090 | inode->i_ino); | |
1c341b77 TM |
2091 | } else if (!have_delegation) |
2092 | nfsi->cache_validity |= NFS_INO_DATA_INVAL_DEFER; | |
1eb5d98f | 2093 | inode_set_iversion_raw(inode, fattr->change_attr); |
9e6e70f8 | 2094 | } |
ade14a7d | 2095 | } else { |
cc7f2dae TM |
2096 | nfsi->cache_validity |= |
2097 | save_cache_validity & NFS_INO_INVALID_CHANGE; | |
213bb584 TM |
2098 | if (!have_delegation || |
2099 | (nfsi->cache_validity & NFS_INO_INVALID_CHANGE) != 0) | |
2100 | cache_revalidated = false; | |
ade14a7d | 2101 | } |
9e6e70f8 | 2102 | |
213bb584 | 2103 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) |
e86d5a02 | 2104 | inode->i_mtime = fattr->mtime; |
213bb584 | 2105 | else if (fattr_supported & NFS_ATTR_FATTR_MTIME) |
cc7f2dae TM |
2106 | nfsi->cache_validity |= |
2107 | save_cache_validity & NFS_INO_INVALID_MTIME; | |
62ab460c | 2108 | |
213bb584 | 2109 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
e86d5a02 | 2110 | inode->i_ctime = fattr->ctime; |
213bb584 | 2111 | else if (fattr_supported & NFS_ATTR_FATTR_CTIME) |
cc7f2dae TM |
2112 | nfsi->cache_validity |= |
2113 | save_cache_validity & NFS_INO_INVALID_CTIME; | |
47aabaa7 | 2114 | |
951a143b | 2115 | /* Check if our cached file size is stale */ |
9e6e70f8 TM |
2116 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { |
2117 | new_isize = nfs_size_to_loff_t(fattr->size); | |
2118 | cur_isize = i_size_read(inode); | |
c80d17c5 | 2119 | if (new_isize != cur_isize && !have_delegation) { |
9e6e70f8 TM |
2120 | /* Do we perhaps have any outstanding writes, or has |
2121 | * the file grown beyond our last write? */ | |
a6b6d5b8 | 2122 | if (!nfs_have_writebacks(inode) || new_isize > cur_isize) { |
110cb2d2 | 2123 | trace_nfs_size_update(inode, new_isize); |
9e6e70f8 | 2124 | i_size_write(inode, new_isize); |
ca0daa27 | 2125 | if (!have_writers) |
16e14375 | 2126 | invalid |= NFS_INO_INVALID_DATA; |
9e6e70f8 | 2127 | } |
1da177e4 | 2128 | } |
4cdfeb64 TM |
2129 | if (new_isize == 0 && |
2130 | !(fattr->valid & (NFS_ATTR_FATTR_SPACE_USED | | |
2131 | NFS_ATTR_FATTR_BLOCKS_USED))) { | |
2132 | fattr->du.nfs3.used = 0; | |
2133 | fattr->valid |= NFS_ATTR_FATTR_SPACE_USED; | |
2134 | } | |
213bb584 | 2135 | } else |
cc7f2dae TM |
2136 | nfsi->cache_validity |= |
2137 | save_cache_validity & NFS_INO_INVALID_SIZE; | |
1da177e4 | 2138 | |
9e6e70f8 | 2139 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
e86d5a02 | 2140 | inode->i_atime = fattr->atime; |
213bb584 | 2141 | else if (fattr_supported & NFS_ATTR_FATTR_ATIME) |
cc7f2dae TM |
2142 | nfsi->cache_validity |= |
2143 | save_cache_validity & NFS_INO_INVALID_ATIME; | |
1da177e4 | 2144 | |
9e6e70f8 TM |
2145 | if (fattr->valid & NFS_ATTR_FATTR_MODE) { |
2146 | if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) { | |
9b4b3513 TM |
2147 | umode_t newmode = inode->i_mode & S_IFMT; |
2148 | newmode |= fattr->mode & S_IALLUGO; | |
2149 | inode->i_mode = newmode; | |
16e14375 | 2150 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2151 | | NFS_INO_INVALID_ACL; |
9e6e70f8 | 2152 | } |
213bb584 | 2153 | } else if (fattr_supported & NFS_ATTR_FATTR_MODE) |
cc7f2dae TM |
2154 | nfsi->cache_validity |= |
2155 | save_cache_validity & NFS_INO_INVALID_MODE; | |
62ab460c | 2156 | |
9e6e70f8 | 2157 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) { |
9ff593c4 | 2158 | if (!uid_eq(inode->i_uid, fattr->uid)) { |
16e14375 | 2159 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2160 | | NFS_INO_INVALID_ACL; |
9e6e70f8 TM |
2161 | inode->i_uid = fattr->uid; |
2162 | } | |
213bb584 | 2163 | } else if (fattr_supported & NFS_ATTR_FATTR_OWNER) |
cc7f2dae TM |
2164 | nfsi->cache_validity |= |
2165 | save_cache_validity & NFS_INO_INVALID_OTHER; | |
62ab460c | 2166 | |
9e6e70f8 | 2167 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) { |
9ff593c4 | 2168 | if (!gid_eq(inode->i_gid, fattr->gid)) { |
16e14375 | 2169 | invalid |= NFS_INO_INVALID_ACCESS |
0b467264 | 2170 | | NFS_INO_INVALID_ACL; |
9e6e70f8 TM |
2171 | inode->i_gid = fattr->gid; |
2172 | } | |
213bb584 | 2173 | } else if (fattr_supported & NFS_ATTR_FATTR_GROUP) |
cc7f2dae TM |
2174 | nfsi->cache_validity |= |
2175 | save_cache_validity & NFS_INO_INVALID_OTHER; | |
921615f1 | 2176 | |
9e6e70f8 | 2177 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) { |
36a10a3c | 2178 | if (inode->i_nlink != fattr->nlink) |
bfe86848 | 2179 | set_nlink(inode, fattr->nlink); |
213bb584 | 2180 | } else if (fattr_supported & NFS_ATTR_FATTR_NLINK) |
cc7f2dae TM |
2181 | nfsi->cache_validity |= |
2182 | save_cache_validity & NFS_INO_INVALID_NLINK; | |
1da177e4 | 2183 | |
9e6e70f8 | 2184 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { |
1da177e4 LT |
2185 | /* |
2186 | * report the blocks in 512byte units | |
2187 | */ | |
2188 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
213bb584 | 2189 | } else if (fattr_supported & NFS_ATTR_FATTR_SPACE_USED) |
ce62b114 TM |
2190 | nfsi->cache_validity |= |
2191 | save_cache_validity & NFS_INO_INVALID_BLOCKS; | |
ce62b114 | 2192 | |
213bb584 | 2193 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
9e6e70f8 | 2194 | inode->i_blocks = fattr->du.nfs2.blocks; |
213bb584 | 2195 | else if (fattr_supported & NFS_ATTR_FATTR_BLOCKS_USED) |
cc7f2dae TM |
2196 | nfsi->cache_validity |= |
2197 | save_cache_validity & NFS_INO_INVALID_BLOCKS; | |
1da177e4 LT |
2198 | |
2199 | /* Update attrtimeo value if we're out of the unstable period */ | |
0b467264 | 2200 | if (attr_changed) { |
91d5b470 | 2201 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
1da177e4 | 2202 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
3e7d950a | 2203 | nfsi->attrtimeo_timestamp = now; |
f5062003 | 2204 | /* Set barrier to be more recent than all outstanding updates */ |
4704f0e2 | 2205 | nfsi->attr_gencount = nfs_inc_attr_generation_counter(); |
6d2b2966 | 2206 | } else { |
ade14a7d TM |
2207 | if (cache_revalidated) { |
2208 | if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, | |
2209 | nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) { | |
2210 | nfsi->attrtimeo <<= 1; | |
2211 | if (nfsi->attrtimeo > NFS_MAXATTRTIMEO(inode)) | |
2212 | nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode); | |
2213 | } | |
6d2b2966 TM |
2214 | nfsi->attrtimeo_timestamp = now; |
2215 | } | |
f5062003 | 2216 | /* Set the barrier to be more recent than this fattr */ |
9fdbfad1 | 2217 | if ((long)(fattr->gencount - nfsi->attr_gencount) > 0) |
f5062003 | 2218 | nfsi->attr_gencount = fattr->gencount; |
1da177e4 | 2219 | } |
c812012f | 2220 | |
1da177e4 LT |
2221 | /* Don't invalidate the data if we were to blame */ |
2222 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) | |
2223 | || S_ISLNK(inode->i_mode))) | |
2224 | invalid &= ~NFS_INO_INVALID_DATA; | |
c80d17c5 | 2225 | nfs_set_cache_invalid(inode, invalid); |
de242c0b | 2226 | |
1da177e4 | 2227 | return 0; |
b37b03b7 | 2228 | out_err: |
1da177e4 LT |
2229 | /* |
2230 | * No need to worry about unhashing the dentry, as the | |
2231 | * lookup validation will know that the inode is bad. | |
2232 | * (But we fall through to invalidate the caches.) | |
2233 | */ | |
93ce4af7 | 2234 | nfs_set_inode_stale_locked(inode); |
1da177e4 LT |
2235 | return -ESTALE; |
2236 | } | |
2237 | ||
f7b422b1 | 2238 | struct inode *nfs_alloc_inode(struct super_block *sb) |
1da177e4 LT |
2239 | { |
2240 | struct nfs_inode *nfsi; | |
c456aacf | 2241 | nfsi = kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
2242 | if (!nfsi) |
2243 | return NULL; | |
55296809 CL |
2244 | nfsi->flags = 0UL; |
2245 | nfsi->cache_validity = 0UL; | |
89d77c8f | 2246 | #if IS_ENABLED(CONFIG_NFS_V4) |
e50a1c2e BF |
2247 | nfsi->nfs4_acl = NULL; |
2248 | #endif /* CONFIG_NFS_V4 */ | |
95ad37f9 FL |
2249 | #ifdef CONFIG_NFS_V4_2 |
2250 | nfsi->xattr_cache = NULL; | |
2251 | #endif | |
1da177e4 LT |
2252 | return &nfsi->vfs_inode; |
2253 | } | |
89d77c8f | 2254 | EXPORT_SYMBOL_GPL(nfs_alloc_inode); |
1da177e4 | 2255 | |
ca1a199e | 2256 | void nfs_free_inode(struct inode *inode) |
1da177e4 LT |
2257 | { |
2258 | kmem_cache_free(nfs_inode_cachep, NFS_I(inode)); | |
2259 | } | |
ca1a199e | 2260 | EXPORT_SYMBOL_GPL(nfs_free_inode); |
fa0d7e3d | 2261 | |
d75d5414 AM |
2262 | static inline void nfs4_init_once(struct nfs_inode *nfsi) |
2263 | { | |
89d77c8f | 2264 | #if IS_ENABLED(CONFIG_NFS_V4) |
d75d5414 AM |
2265 | INIT_LIST_HEAD(&nfsi->open_states); |
2266 | nfsi->delegation = NULL; | |
d75d5414 | 2267 | init_rwsem(&nfsi->rwsem); |
e5e94017 | 2268 | nfsi->layout = NULL; |
d75d5414 AM |
2269 | #endif |
2270 | } | |
f7b422b1 | 2271 | |
51cc5068 | 2272 | static void init_once(void *foo) |
1da177e4 LT |
2273 | { |
2274 | struct nfs_inode *nfsi = (struct nfs_inode *) foo; | |
2275 | ||
a35afb83 | 2276 | inode_init_once(&nfsi->vfs_inode); |
a35afb83 CL |
2277 | INIT_LIST_HEAD(&nfsi->open_files); |
2278 | INIT_LIST_HEAD(&nfsi->access_cache_entry_lru); | |
2279 | INIT_LIST_HEAD(&nfsi->access_cache_inode_lru); | |
a35afb83 | 2280 | nfs4_init_once(nfsi); |
1da177e4 | 2281 | } |
20c2df83 | 2282 | |
f7b422b1 | 2283 | static int __init nfs_init_inodecache(void) |
1da177e4 LT |
2284 | { |
2285 | nfs_inode_cachep = kmem_cache_create("nfs_inode_cache", | |
2286 | sizeof(struct nfs_inode), | |
fffb60f9 | 2287 | 0, (SLAB_RECLAIM_ACCOUNT| |
5d097056 | 2288 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
20c2df83 | 2289 | init_once); |
1da177e4 LT |
2290 | if (nfs_inode_cachep == NULL) |
2291 | return -ENOMEM; | |
2292 | ||
2293 | return 0; | |
2294 | } | |
2295 | ||
266bee88 | 2296 | static void nfs_destroy_inodecache(void) |
1da177e4 | 2297 | { |
8c0a8537 KS |
2298 | /* |
2299 | * Make sure all delayed rcu free inodes are flushed before we | |
2300 | * destroy cache. | |
2301 | */ | |
2302 | rcu_barrier(); | |
1a1d92c1 | 2303 | kmem_cache_destroy(nfs_inode_cachep); |
1da177e4 LT |
2304 | } |
2305 | ||
5746006f | 2306 | struct workqueue_struct *nfsiod_workqueue; |
89d77c8f | 2307 | EXPORT_SYMBOL_GPL(nfsiod_workqueue); |
5746006f TM |
2308 | |
2309 | /* | |
2310 | * start up the nfsiod workqueue | |
2311 | */ | |
2312 | static int nfsiod_start(void) | |
2313 | { | |
2314 | struct workqueue_struct *wq; | |
2315 | dprintk("RPC: creating workqueue nfsiod\n"); | |
bf701b76 | 2316 | wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); |
5746006f TM |
2317 | if (wq == NULL) |
2318 | return -ENOMEM; | |
2319 | nfsiod_workqueue = wq; | |
2320 | return 0; | |
2321 | } | |
2322 | ||
2323 | /* | |
2324 | * Destroy the nfsiod workqueue | |
2325 | */ | |
2326 | static void nfsiod_stop(void) | |
2327 | { | |
2328 | struct workqueue_struct *wq; | |
2329 | ||
2330 | wq = nfsiod_workqueue; | |
2331 | if (wq == NULL) | |
2332 | return; | |
2333 | nfsiod_workqueue = NULL; | |
2334 | destroy_workqueue(wq); | |
2335 | } | |
2336 | ||
c7d03a00 | 2337 | unsigned int nfs_net_id; |
9e2e74db | 2338 | EXPORT_SYMBOL_GPL(nfs_net_id); |
1b340d01 SK |
2339 | |
2340 | static int nfs_net_init(struct net *net) | |
2341 | { | |
6b13168b | 2342 | nfs_clients_init(net); |
65b38851 | 2343 | return nfs_fs_proc_net_init(net); |
1b340d01 SK |
2344 | } |
2345 | ||
2346 | static void nfs_net_exit(struct net *net) | |
2347 | { | |
65b38851 | 2348 | nfs_fs_proc_net_exit(net); |
10b7a70c | 2349 | nfs_clients_exit(net); |
1b340d01 SK |
2350 | } |
2351 | ||
2352 | static struct pernet_operations nfs_net_ops = { | |
2353 | .init = nfs_net_init, | |
2354 | .exit = nfs_net_exit, | |
2355 | .id = &nfs_net_id, | |
2356 | .size = sizeof(struct nfs_net), | |
2357 | }; | |
2358 | ||
1da177e4 LT |
2359 | /* |
2360 | * Initialize NFS | |
2361 | */ | |
2362 | static int __init init_nfs_fs(void) | |
2363 | { | |
2364 | int err; | |
2365 | ||
996bc4f4 TM |
2366 | err = nfs_sysfs_init(); |
2367 | if (err < 0) | |
2368 | goto out10; | |
2369 | ||
1b340d01 | 2370 | err = register_pernet_subsys(&nfs_net_ops); |
e571cbf1 | 2371 | if (err < 0) |
89d77c8f | 2372 | goto out9; |
e571cbf1 | 2373 | |
5746006f TM |
2374 | err = nfsiod_start(); |
2375 | if (err) | |
89d77c8f | 2376 | goto out7; |
5746006f | 2377 | |
6aaca566 DH |
2378 | err = nfs_fs_proc_init(); |
2379 | if (err) | |
89d77c8f | 2380 | goto out6; |
6aaca566 | 2381 | |
1da177e4 LT |
2382 | err = nfs_init_nfspagecache(); |
2383 | if (err) | |
89d77c8f | 2384 | goto out5; |
1da177e4 LT |
2385 | |
2386 | err = nfs_init_inodecache(); | |
2387 | if (err) | |
89d77c8f | 2388 | goto out4; |
1da177e4 LT |
2389 | |
2390 | err = nfs_init_readpagecache(); | |
2391 | if (err) | |
89d77c8f | 2392 | goto out3; |
1da177e4 LT |
2393 | |
2394 | err = nfs_init_writepagecache(); | |
2395 | if (err) | |
89d77c8f | 2396 | goto out2; |
1da177e4 | 2397 | |
1da177e4 LT |
2398 | err = nfs_init_directcache(); |
2399 | if (err) | |
89d77c8f | 2400 | goto out1; |
1da177e4 | 2401 | |
ec7652aa | 2402 | rpc_proc_register(&init_net, &nfs_rpcstat); |
cd738ee9 KM |
2403 | |
2404 | err = register_nfs_fs(); | |
2405 | if (err) | |
129d1977 BS |
2406 | goto out0; |
2407 | ||
1da177e4 | 2408 | return 0; |
129d1977 | 2409 | out0: |
ec7652aa | 2410 | rpc_proc_unregister(&init_net, "nfs"); |
1da177e4 | 2411 | nfs_destroy_directcache(); |
89d77c8f | 2412 | out1: |
129d1977 | 2413 | nfs_destroy_writepagecache(); |
89d77c8f | 2414 | out2: |
129d1977 | 2415 | nfs_destroy_readpagecache(); |
89d77c8f | 2416 | out3: |
129d1977 | 2417 | nfs_destroy_inodecache(); |
89d77c8f | 2418 | out4: |
129d1977 | 2419 | nfs_destroy_nfspagecache(); |
89d77c8f | 2420 | out5: |
129d1977 | 2421 | nfs_fs_proc_exit(); |
89d77c8f | 2422 | out6: |
129d1977 | 2423 | nfsiod_stop(); |
89d77c8f | 2424 | out7: |
129d1977 | 2425 | unregister_pernet_subsys(&nfs_net_ops); |
89d77c8f | 2426 | out9: |
996bc4f4 TM |
2427 | nfs_sysfs_exit(); |
2428 | out10: | |
1da177e4 LT |
2429 | return err; |
2430 | } | |
2431 | ||
2432 | static void __exit exit_nfs_fs(void) | |
2433 | { | |
1da177e4 | 2434 | nfs_destroy_directcache(); |
1da177e4 LT |
2435 | nfs_destroy_writepagecache(); |
2436 | nfs_destroy_readpagecache(); | |
2437 | nfs_destroy_inodecache(); | |
2438 | nfs_destroy_nfspagecache(); | |
1b340d01 | 2439 | unregister_pernet_subsys(&nfs_net_ops); |
ec7652aa | 2440 | rpc_proc_unregister(&init_net, "nfs"); |
f7b422b1 | 2441 | unregister_nfs_fs(); |
6aaca566 | 2442 | nfs_fs_proc_exit(); |
5746006f | 2443 | nfsiod_stop(); |
996bc4f4 | 2444 | nfs_sysfs_exit(); |
1da177e4 LT |
2445 | } |
2446 | ||
2447 | /* Not quite true; I just maintain it */ | |
2448 | MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); | |
2449 | MODULE_LICENSE("GPL"); | |
f43bf0be | 2450 | module_param(enable_ino64, bool, 0644); |
1da177e4 LT |
2451 | |
2452 | module_init(init_nfs_fs) | |
2453 | module_exit(exit_nfs_fs) |