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