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