Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/inode.c | |
3 | * | |
4 | * Copyright (C) 1992 Rick Sladkey | |
5 | * | |
6 | * nfs inode and superblock handling functions | |
7 | * | |
526719ba | 8 | * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some |
1da177e4 LT |
9 | * experimental NFS changes. Modularisation taken straight from SYS5 fs. |
10 | * | |
11 | * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts. | |
12 | * J.S.Peatfield@damtp.cam.ac.uk | |
13 | * | |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/init.h> | |
e8edc6e0 | 18 | #include <linux/sched.h> |
1da177e4 LT |
19 | #include <linux/time.h> |
20 | #include <linux/kernel.h> | |
21 | #include <linux/mm.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/stat.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/unistd.h> | |
26 | #include <linux/sunrpc/clnt.h> | |
27 | #include <linux/sunrpc/stats.h> | |
4ece3a2d | 28 | #include <linux/sunrpc/metrics.h> |
1da177e4 LT |
29 | #include <linux/nfs_fs.h> |
30 | #include <linux/nfs_mount.h> | |
31 | #include <linux/nfs4_mount.h> | |
32 | #include <linux/lockd/bind.h> | |
1da177e4 LT |
33 | #include <linux/seq_file.h> |
34 | #include <linux/mount.h> | |
1da177e4 | 35 | #include <linux/vfs.h> |
9cdb3883 MN |
36 | #include <linux/inet.h> |
37 | #include <linux/nfs_xdr.h> | |
5a0e3ad6 | 38 | #include <linux/slab.h> |
3fa0b4e2 | 39 | #include <linux/compat.h> |
d310310c | 40 | #include <linux/freezer.h> |
1da177e4 | 41 | |
1da177e4 LT |
42 | #include <asm/uaccess.h> |
43 | ||
4ce79717 | 44 | #include "nfs4_fs.h" |
a72b4422 | 45 | #include "callback.h" |
1da177e4 | 46 | #include "delegation.h" |
d9ef5a8c | 47 | #include "iostat.h" |
f7b422b1 | 48 | #include "internal.h" |
8ec442ae | 49 | #include "fscache.h" |
e5e94017 | 50 | #include "pnfs.h" |
ab7017a3 | 51 | #include "nfs.h" |
1b340d01 | 52 | #include "netns.h" |
1da177e4 | 53 | |
f4ce1299 TM |
54 | #include "nfstrace.h" |
55 | ||
1da177e4 | 56 | #define NFSDBG_FACILITY NFSDBG_VFS |
1da177e4 | 57 | |
f43bf0be TM |
58 | #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1 |
59 | ||
60 | /* Default is to see 64-bit inode numbers */ | |
90ab5ee9 | 61 | static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED; |
f43bf0be | 62 | |
1da177e4 | 63 | static void nfs_invalidate_inode(struct inode *); |
24aa1fe6 | 64 | static int nfs_update_inode(struct inode *, struct nfs_fattr *); |
1da177e4 | 65 | |
e18b890b | 66 | static struct kmem_cache * nfs_inode_cachep; |
b7fa0554 | 67 | |
1da177e4 LT |
68 | static inline unsigned long |
69 | nfs_fattr_to_ino_t(struct nfs_fattr *fattr) | |
70 | { | |
71 | return nfs_fileid_to_ino_t(fattr->fileid); | |
72 | } | |
73 | ||
72cb77f4 TM |
74 | /** |
75 | * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks | |
76 | * @word: long word containing the bit lock | |
77 | */ | |
c1221321 | 78 | int nfs_wait_bit_killable(struct wait_bit_key *key) |
72cb77f4 TM |
79 | { |
80 | if (fatal_signal_pending(current)) | |
81 | return -ERESTARTSYS; | |
416ad3c9 | 82 | freezable_schedule_unsafe(); |
72cb77f4 TM |
83 | return 0; |
84 | } | |
89d77c8f | 85 | EXPORT_SYMBOL_GPL(nfs_wait_bit_killable); |
72cb77f4 | 86 | |
f43bf0be TM |
87 | /** |
88 | * nfs_compat_user_ino64 - returns the user-visible inode number | |
89 | * @fileid: 64-bit fileid | |
90 | * | |
91 | * This function returns a 32-bit inode number if the boot parameter | |
92 | * nfs.enable_ino64 is zero. | |
93 | */ | |
94 | u64 nfs_compat_user_ino64(u64 fileid) | |
95 | { | |
3fa0b4e2 FF |
96 | #ifdef CONFIG_COMPAT |
97 | compat_ulong_t ino; | |
98 | #else | |
99 | unsigned long ino; | |
100 | #endif | |
f43bf0be TM |
101 | |
102 | if (enable_ino64) | |
103 | return fileid; | |
104 | ino = fileid; | |
105 | if (sizeof(ino) < sizeof(fileid)) | |
106 | ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8; | |
107 | return ino; | |
108 | } | |
109 | ||
eed99357 TM |
110 | int nfs_drop_inode(struct inode *inode) |
111 | { | |
112 | return NFS_STALE(inode) || generic_drop_inode(inode); | |
113 | } | |
114 | EXPORT_SYMBOL_GPL(nfs_drop_inode); | |
115 | ||
19d87ca3 | 116 | void nfs_clear_inode(struct inode *inode) |
1da177e4 | 117 | { |
da6d503a TM |
118 | /* |
119 | * The following should never happen... | |
120 | */ | |
f48407dd TM |
121 | WARN_ON_ONCE(nfs_have_writebacks(inode)); |
122 | WARN_ON_ONCE(!list_empty(&NFS_I(inode)->open_files)); | |
ada70d94 | 123 | nfs_zap_acl_cache(inode); |
1c3c07e9 | 124 | nfs_access_zap_cache(inode); |
f1fe29b4 | 125 | nfs_fscache_clear_inode(inode); |
1da177e4 | 126 | } |
89d77c8f | 127 | EXPORT_SYMBOL_GPL(nfs_clear_inode); |
1da177e4 | 128 | |
b57922d9 AV |
129 | void nfs_evict_inode(struct inode *inode) |
130 | { | |
91b0abe3 | 131 | truncate_inode_pages_final(&inode->i_data); |
dbd5768f | 132 | clear_inode(inode); |
b57922d9 AV |
133 | nfs_clear_inode(inode); |
134 | } | |
135 | ||
4d346bea TM |
136 | static int nfs_sync_inode(struct inode *inode) |
137 | { | |
138 | nfs_inode_dio_wait(inode); | |
139 | return nfs_wb_all(inode); | |
140 | } | |
141 | ||
29884df0 TM |
142 | /** |
143 | * nfs_sync_mapping - helper to flush all mmapped dirty data to disk | |
144 | */ | |
145 | int nfs_sync_mapping(struct address_space *mapping) | |
146 | { | |
5cf95214 | 147 | int ret = 0; |
29884df0 | 148 | |
5cf95214 TM |
149 | if (mapping->nrpages != 0) { |
150 | unmap_mapping_range(mapping, 0, 0, 0); | |
151 | ret = nfs_wb_all(mapping->host); | |
152 | } | |
29884df0 TM |
153 | return ret; |
154 | } | |
155 | ||
6edf9609 TM |
156 | static void nfs_set_cache_invalid(struct inode *inode, unsigned long flags) |
157 | { | |
158 | struct nfs_inode *nfsi = NFS_I(inode); | |
159 | ||
160 | if (inode->i_mapping->nrpages == 0) | |
161 | flags &= ~NFS_INO_INVALID_DATA; | |
162 | nfsi->cache_validity |= flags; | |
163 | if (flags & NFS_INO_INVALID_DATA) | |
164 | nfs_fscache_invalidate(inode); | |
165 | } | |
166 | ||
1da177e4 LT |
167 | /* |
168 | * Invalidate the local caches | |
169 | */ | |
b37b03b7 | 170 | static void nfs_zap_caches_locked(struct inode *inode) |
1da177e4 LT |
171 | { |
172 | struct nfs_inode *nfsi = NFS_I(inode); | |
173 | int mode = inode->i_mode; | |
174 | ||
91d5b470 CL |
175 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
176 | ||
c7c20973 TM |
177 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
178 | nfsi->attrtimeo_timestamp = jiffies; | |
1da177e4 | 179 | |
c3f52af3 | 180 | memset(NFS_I(inode)->cookieverf, 0, sizeof(NFS_I(inode)->cookieverf)); |
de242c0b | 181 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { |
6edf9609 | 182 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
aa9c2669 DQ |
183 | | NFS_INO_INVALID_DATA |
184 | | NFS_INO_INVALID_ACCESS | |
185 | | NFS_INO_INVALID_ACL | |
6edf9609 | 186 | | NFS_INO_REVAL_PAGECACHE); |
aa9c2669 | 187 | } else |
6edf9609 | 188 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
aa9c2669 DQ |
189 | | NFS_INO_INVALID_ACCESS |
190 | | NFS_INO_INVALID_ACL | |
6edf9609 | 191 | | NFS_INO_REVAL_PAGECACHE); |
fd1defc2 | 192 | nfs_zap_label_cache_locked(nfsi); |
b37b03b7 | 193 | } |
dc59250c | 194 | |
b37b03b7 TM |
195 | void nfs_zap_caches(struct inode *inode) |
196 | { | |
197 | spin_lock(&inode->i_lock); | |
198 | nfs_zap_caches_locked(inode); | |
dc59250c | 199 | spin_unlock(&inode->i_lock); |
ada70d94 | 200 | } |
f4ac1674 | 201 | EXPORT_SYMBOL_GPL(nfs_zap_caches); |
ada70d94 | 202 | |
cd9ae2b6 TM |
203 | void nfs_zap_mapping(struct inode *inode, struct address_space *mapping) |
204 | { | |
205 | if (mapping->nrpages != 0) { | |
206 | spin_lock(&inode->i_lock); | |
6edf9609 | 207 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
cd9ae2b6 TM |
208 | spin_unlock(&inode->i_lock); |
209 | } | |
210 | } | |
211 | ||
f41f7418 | 212 | void nfs_zap_acl_cache(struct inode *inode) |
ada70d94 TM |
213 | { |
214 | void (*clear_acl_cache)(struct inode *); | |
215 | ||
216 | clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache; | |
217 | if (clear_acl_cache != NULL) | |
218 | clear_acl_cache(inode); | |
dc59250c | 219 | spin_lock(&inode->i_lock); |
55296809 | 220 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL; |
dc59250c | 221 | spin_unlock(&inode->i_lock); |
1da177e4 | 222 | } |
1c606fb7 | 223 | EXPORT_SYMBOL_GPL(nfs_zap_acl_cache); |
1da177e4 | 224 | |
c4812998 TM |
225 | void nfs_invalidate_atime(struct inode *inode) |
226 | { | |
227 | spin_lock(&inode->i_lock); | |
6edf9609 | 228 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATIME); |
c4812998 TM |
229 | spin_unlock(&inode->i_lock); |
230 | } | |
ddda8e0a | 231 | EXPORT_SYMBOL_GPL(nfs_invalidate_atime); |
c4812998 | 232 | |
1da177e4 | 233 | /* |
b37b03b7 TM |
234 | * Invalidate, but do not unhash, the inode. |
235 | * NB: must be called with inode->i_lock held! | |
1da177e4 | 236 | */ |
b37b03b7 | 237 | static void nfs_invalidate_inode(struct inode *inode) |
1da177e4 | 238 | { |
3a10c30a | 239 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
b37b03b7 | 240 | nfs_zap_caches_locked(inode); |
1da177e4 LT |
241 | } |
242 | ||
243 | struct nfs_find_desc { | |
244 | struct nfs_fh *fh; | |
245 | struct nfs_fattr *fattr; | |
246 | }; | |
247 | ||
248 | /* | |
249 | * In NFSv3 we can have 64bit inode numbers. In order to support | |
250 | * this, and re-exported directories (also seen in NFSv2) | |
251 | * we are forced to allow 2 different inodes to have the same | |
252 | * i_ino. | |
253 | */ | |
254 | static int | |
255 | nfs_find_actor(struct inode *inode, void *opaque) | |
256 | { | |
257 | struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque; | |
258 | struct nfs_fh *fh = desc->fh; | |
259 | struct nfs_fattr *fattr = desc->fattr; | |
260 | ||
261 | if (NFS_FILEID(inode) != fattr->fileid) | |
262 | return 0; | |
f6488c9b JL |
263 | if ((S_IFMT & inode->i_mode) != (S_IFMT & fattr->mode)) |
264 | return 0; | |
1da177e4 LT |
265 | if (nfs_compare_fh(NFS_FH(inode), fh)) |
266 | return 0; | |
267 | if (is_bad_inode(inode) || NFS_STALE(inode)) | |
268 | return 0; | |
269 | return 1; | |
270 | } | |
271 | ||
272 | static int | |
273 | nfs_init_locked(struct inode *inode, void *opaque) | |
274 | { | |
275 | struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque; | |
276 | struct nfs_fattr *fattr = desc->fattr; | |
277 | ||
99fadcd7 | 278 | set_nfs_fileid(inode, fattr->fileid); |
1da177e4 LT |
279 | nfs_copy_fh(NFS_FH(inode), desc->fh); |
280 | return 0; | |
281 | } | |
282 | ||
e058f70b | 283 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
fd1defc2 TM |
284 | static void nfs_clear_label_invalid(struct inode *inode) |
285 | { | |
286 | spin_lock(&inode->i_lock); | |
287 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_LABEL; | |
288 | spin_unlock(&inode->i_lock); | |
289 | } | |
290 | ||
aa9c2669 DQ |
291 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr, |
292 | struct nfs4_label *label) | |
293 | { | |
294 | int error; | |
295 | ||
296 | if (label == NULL) | |
297 | return; | |
298 | ||
aa9c2669 DQ |
299 | if ((fattr->valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL) && inode->i_security) { |
300 | error = security_inode_notifysecctx(inode, label->label, | |
301 | label->len); | |
302 | if (error) | |
303 | printk(KERN_ERR "%s() %s %d " | |
304 | "security_inode_notifysecctx() %d\n", | |
305 | __func__, | |
306 | (char *)label->label, | |
307 | label->len, error); | |
fd1defc2 | 308 | nfs_clear_label_invalid(inode); |
aa9c2669 DQ |
309 | } |
310 | } | |
311 | ||
e058f70b SD |
312 | struct nfs4_label *nfs4_label_alloc(struct nfs_server *server, gfp_t flags) |
313 | { | |
314 | struct nfs4_label *label = NULL; | |
315 | int minor_version = server->nfs_client->cl_minorversion; | |
316 | ||
317 | if (minor_version < 2) | |
318 | return label; | |
319 | ||
320 | if (!(server->caps & NFS_CAP_SECURITY_LABEL)) | |
321 | return label; | |
322 | ||
323 | label = kzalloc(sizeof(struct nfs4_label), flags); | |
324 | if (label == NULL) | |
325 | return ERR_PTR(-ENOMEM); | |
326 | ||
327 | label->label = kzalloc(NFS4_MAXLABELLEN, flags); | |
328 | if (label->label == NULL) { | |
329 | kfree(label); | |
330 | return ERR_PTR(-ENOMEM); | |
331 | } | |
332 | label->len = NFS4_MAXLABELLEN; | |
333 | ||
334 | return label; | |
335 | } | |
336 | EXPORT_SYMBOL_GPL(nfs4_label_alloc); | |
aa9c2669 | 337 | #else |
829e57d7 | 338 | void nfs_setsecurity(struct inode *inode, struct nfs_fattr *fattr, |
aa9c2669 DQ |
339 | struct nfs4_label *label) |
340 | { | |
341 | } | |
e058f70b | 342 | #endif |
aa9c2669 | 343 | EXPORT_SYMBOL_GPL(nfs_setsecurity); |
e058f70b | 344 | |
1da177e4 LT |
345 | /* |
346 | * This is our front-end to iget that looks up inodes by file handle | |
347 | * instead of inode number. | |
348 | */ | |
349 | struct inode * | |
1775fd3e | 350 | nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr, struct nfs4_label *label) |
1da177e4 LT |
351 | { |
352 | struct nfs_find_desc desc = { | |
353 | .fh = fh, | |
354 | .fattr = fattr | |
355 | }; | |
03f28e3a | 356 | struct inode *inode = ERR_PTR(-ENOENT); |
1da177e4 LT |
357 | unsigned long hash; |
358 | ||
7ebb9315 BS |
359 | nfs_attr_check_mountpoint(sb, fattr); |
360 | ||
2ef47eb1 AS |
361 | if (nfs_attr_use_mounted_on_fileid(fattr)) |
362 | fattr->fileid = fattr->mounted_on_fileid; | |
363 | else if ((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) | |
1da177e4 | 364 | goto out_no_inode; |
9e6e70f8 | 365 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0) |
1da177e4 | 366 | goto out_no_inode; |
1da177e4 LT |
367 | |
368 | hash = nfs_fattr_to_ino_t(fattr); | |
369 | ||
03f28e3a TM |
370 | inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc); |
371 | if (inode == NULL) { | |
372 | inode = ERR_PTR(-ENOMEM); | |
1da177e4 | 373 | goto out_no_inode; |
03f28e3a | 374 | } |
1da177e4 LT |
375 | |
376 | if (inode->i_state & I_NEW) { | |
377 | struct nfs_inode *nfsi = NFS_I(inode); | |
b0c4fddc | 378 | unsigned long now = jiffies; |
1da177e4 LT |
379 | |
380 | /* We set i_ino for the few things that still rely on it, | |
381 | * such as stat(2) */ | |
382 | inode->i_ino = hash; | |
383 | ||
384 | /* We can't support update_atime(), since the server will reset it */ | |
385 | inode->i_flags |= S_NOATIME|S_NOCMTIME; | |
386 | inode->i_mode = fattr->mode; | |
62ab460c TM |
387 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0 |
388 | && nfs_server_capable(inode, NFS_CAP_MODE)) | |
6edf9609 | 389 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
1da177e4 LT |
390 | /* Why so? Because we want revalidate for devices/FIFOs, and |
391 | * that's precisely what we have in nfs_file_inode_operations. | |
392 | */ | |
8fa5c000 | 393 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops; |
1da177e4 | 394 | if (S_ISREG(inode->i_mode)) { |
1788ea6e | 395 | inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops; |
1da177e4 | 396 | inode->i_data.a_ops = &nfs_file_aops; |
1da177e4 | 397 | } else if (S_ISDIR(inode->i_mode)) { |
8fa5c000 | 398 | inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops; |
1da177e4 | 399 | inode->i_fop = &nfs_dir_operations; |
11de3b11 | 400 | inode->i_data.a_ops = &nfs_dir_aops; |
55a97593 | 401 | /* Deal with crossing mountpoints */ |
7ebb9315 BS |
402 | if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT || |
403 | fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) { | |
6b97fd3d MN |
404 | if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) |
405 | inode->i_op = &nfs_referral_inode_operations; | |
406 | else | |
407 | inode->i_op = &nfs_mountpoint_inode_operations; | |
55a97593 | 408 | inode->i_fop = NULL; |
36d43a43 | 409 | inode->i_flags |= S_AUTOMOUNT; |
55a97593 | 410 | } |
1da177e4 LT |
411 | } else if (S_ISLNK(inode->i_mode)) |
412 | inode->i_op = &nfs_symlink_inode_operations; | |
413 | else | |
414 | init_special_inode(inode, inode->i_mode, fattr->rdev); | |
415 | ||
9e6e70f8 TM |
416 | memset(&inode->i_atime, 0, sizeof(inode->i_atime)); |
417 | memset(&inode->i_mtime, 0, sizeof(inode->i_mtime)); | |
418 | memset(&inode->i_ctime, 0, sizeof(inode->i_ctime)); | |
a9a4a87a | 419 | inode->i_version = 0; |
9e6e70f8 | 420 | inode->i_size = 0; |
6d6b77f1 | 421 | clear_nlink(inode); |
9ff593c4 EB |
422 | inode->i_uid = make_kuid(&init_user_ns, -2); |
423 | inode->i_gid = make_kgid(&init_user_ns, -2); | |
9e6e70f8 TM |
424 | inode->i_blocks = 0; |
425 | memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf)); | |
2701d086 AA |
426 | nfsi->write_io = 0; |
427 | nfsi->read_io = 0; | |
9e6e70f8 | 428 | |
33801147 | 429 | nfsi->read_cache_jiffies = fattr->time_start; |
4704f0e2 | 430 | nfsi->attr_gencount = fattr->gencount; |
9e6e70f8 TM |
431 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
432 | inode->i_atime = fattr->atime; | |
62ab460c | 433 | else if (nfs_server_capable(inode, NFS_CAP_ATIME)) |
6edf9609 | 434 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
435 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) |
436 | inode->i_mtime = fattr->mtime; | |
62ab460c | 437 | else if (nfs_server_capable(inode, NFS_CAP_MTIME)) |
6edf9609 | 438 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
439 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) |
440 | inode->i_ctime = fattr->ctime; | |
62ab460c | 441 | else if (nfs_server_capable(inode, NFS_CAP_CTIME)) |
6edf9609 | 442 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 | 443 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) |
a9a4a87a | 444 | inode->i_version = fattr->change_attr; |
62ab460c | 445 | else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR)) |
6edf9609 | 446 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
447 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) |
448 | inode->i_size = nfs_size_to_loff_t(fattr->size); | |
62ab460c | 449 | else |
6edf9609 TM |
450 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR |
451 | | NFS_INO_REVAL_PAGECACHE); | |
9e6e70f8 | 452 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) |
bfe86848 | 453 | set_nlink(inode, fattr->nlink); |
62ab460c | 454 | else if (nfs_server_capable(inode, NFS_CAP_NLINK)) |
6edf9609 | 455 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
456 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) |
457 | inode->i_uid = fattr->uid; | |
62ab460c | 458 | else if (nfs_server_capable(inode, NFS_CAP_OWNER)) |
6edf9609 | 459 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
460 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) |
461 | inode->i_gid = fattr->gid; | |
62ab460c | 462 | else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP)) |
6edf9609 | 463 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ATTR); |
9e6e70f8 TM |
464 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
465 | inode->i_blocks = fattr->du.nfs2.blocks; | |
466 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { | |
1da177e4 LT |
467 | /* |
468 | * report the blocks in 512byte units | |
469 | */ | |
470 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
1da177e4 | 471 | } |
aa9c2669 DQ |
472 | |
473 | nfs_setsecurity(inode, fattr, label); | |
474 | ||
1da177e4 | 475 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
b0c4fddc | 476 | nfsi->attrtimeo_timestamp = now; |
1c3c07e9 | 477 | nfsi->access_cache = RB_ROOT; |
1da177e4 | 478 | |
f1fe29b4 | 479 | nfs_fscache_init_inode(inode); |
ef79c097 | 480 | |
1da177e4 LT |
481 | unlock_new_inode(inode); |
482 | } else | |
483 | nfs_refresh_inode(inode, fattr); | |
1e8968c5 | 484 | dprintk("NFS: nfs_fhget(%s/%Lu fh_crc=0x%08x ct=%d)\n", |
1da177e4 | 485 | inode->i_sb->s_id, |
1e8968c5 | 486 | (unsigned long long)NFS_FILEID(inode), |
4f1abd22 | 487 | nfs_display_fhandle_hash(fh), |
1da177e4 LT |
488 | atomic_read(&inode->i_count)); |
489 | ||
490 | out: | |
491 | return inode; | |
492 | ||
493 | out_no_inode: | |
03f28e3a | 494 | dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode)); |
1da177e4 LT |
495 | goto out; |
496 | } | |
89d77c8f | 497 | EXPORT_SYMBOL_GPL(nfs_fhget); |
1da177e4 | 498 | |
536e43d1 | 499 | #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 |
500 | |
501 | int | |
502 | nfs_setattr(struct dentry *dentry, struct iattr *attr) | |
503 | { | |
504 | struct inode *inode = dentry->d_inode; | |
987f8dfc TM |
505 | struct nfs_fattr *fattr; |
506 | int error = -ENOMEM; | |
1da177e4 | 507 | |
91d5b470 CL |
508 | nfs_inc_stats(inode, NFSIOS_VFSSETATTR); |
509 | ||
188b95dd JL |
510 | /* skip mode change if it's just for clearing setuid/setgid */ |
511 | if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) | |
512 | attr->ia_valid &= ~ATTR_MODE; | |
513 | ||
1da177e4 | 514 | if (attr->ia_valid & ATTR_SIZE) { |
3a7ed3ff OS |
515 | loff_t i_size; |
516 | ||
08a899d5 CH |
517 | BUG_ON(!S_ISREG(inode->i_mode)); |
518 | ||
3a7ed3ff OS |
519 | i_size = i_size_read(inode); |
520 | if (attr->ia_size == i_size) | |
1da177e4 | 521 | attr->ia_valid &= ~ATTR_SIZE; |
3a7ed3ff OS |
522 | else if (attr->ia_size < i_size && IS_SWAPFILE(inode)) |
523 | return -ETXTBSY; | |
1da177e4 LT |
524 | } |
525 | ||
526 | /* Optimization: if the end result is no change, don't RPC */ | |
527 | attr->ia_valid &= NFS_VALID_ATTRS; | |
536e43d1 | 528 | if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
1da177e4 LT |
529 | return 0; |
530 | ||
f4ce1299 TM |
531 | trace_nfs_setattr_enter(inode); |
532 | ||
755c1e20 | 533 | /* Write all dirty data */ |
4d346bea TM |
534 | if (S_ISREG(inode->i_mode)) |
535 | nfs_sync_inode(inode); | |
987f8dfc TM |
536 | |
537 | fattr = nfs_alloc_fattr(); | |
538 | if (fattr == NULL) | |
539 | goto out; | |
642ac549 TM |
540 | /* |
541 | * Return any delegations if we're going to change ACLs | |
542 | */ | |
543 | if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) | |
57ec14c5 | 544 | NFS_PROTO(inode)->return_delegation(inode); |
987f8dfc | 545 | error = NFS_PROTO(inode)->setattr(dentry, fattr, attr); |
65e4308d | 546 | if (error == 0) |
aa9c2669 | 547 | error = nfs_refresh_inode(inode, fattr); |
987f8dfc TM |
548 | nfs_free_fattr(fattr); |
549 | out: | |
f4ce1299 | 550 | trace_nfs_setattr_exit(inode, error); |
65e4308d TM |
551 | return error; |
552 | } | |
ddda8e0a | 553 | EXPORT_SYMBOL_GPL(nfs_setattr); |
65e4308d | 554 | |
a3d01454 TM |
555 | /** |
556 | * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall | |
557 | * @inode: inode of the file used | |
558 | * @offset: file offset to start truncating | |
559 | * | |
560 | * This is a copy of the common vmtruncate, but with the locking | |
561 | * corrected to take into account the fact that NFS requires | |
562 | * inode->i_size to be updated under the inode->i_lock. | |
f044636d | 563 | * Note: must be called with inode->i_lock held! |
a3d01454 TM |
564 | */ |
565 | static int nfs_vmtruncate(struct inode * inode, loff_t offset) | |
566 | { | |
c08d3b0e | 567 | int err; |
a3d01454 | 568 | |
c08d3b0e | 569 | err = inode_newsize_ok(inode, offset); |
570 | if (err) | |
571 | goto out; | |
a3d01454 | 572 | |
c08d3b0e | 573 | i_size_write(inode, offset); |
6edf9609 TM |
574 | /* Optimisation */ |
575 | if (offset == 0) | |
576 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_DATA; | |
c08d3b0e | 577 | |
f044636d | 578 | spin_unlock(&inode->i_lock); |
7caef267 | 579 | truncate_pagecache(inode, offset); |
f044636d | 580 | spin_lock(&inode->i_lock); |
c08d3b0e | 581 | out: |
582 | return err; | |
a3d01454 TM |
583 | } |
584 | ||
65e4308d TM |
585 | /** |
586 | * nfs_setattr_update_inode - Update inode metadata after a setattr call. | |
587 | * @inode: pointer to struct inode | |
588 | * @attr: pointer to struct iattr | |
589 | * | |
590 | * Note: we do this in the *proc.c in order to ensure that | |
591 | * it works for things like exclusive creates too. | |
592 | */ | |
f044636d TM |
593 | void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr, |
594 | struct nfs_fattr *fattr) | |
65e4308d | 595 | { |
f044636d TM |
596 | /* Barrier: bump the attribute generation count. */ |
597 | nfs_fattr_set_barrier(fattr); | |
598 | ||
599 | spin_lock(&inode->i_lock); | |
600 | NFS_I(inode)->attr_gencount = fattr->gencount; | |
65e4308d | 601 | if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) { |
1da177e4 | 602 | if ((attr->ia_valid & ATTR_MODE) != 0) { |
65e4308d TM |
603 | int mode = attr->ia_mode & S_IALLUGO; |
604 | mode |= inode->i_mode & ~S_IALLUGO; | |
1da177e4 LT |
605 | inode->i_mode = mode; |
606 | } | |
607 | if ((attr->ia_valid & ATTR_UID) != 0) | |
608 | inode->i_uid = attr->ia_uid; | |
609 | if ((attr->ia_valid & ATTR_GID) != 0) | |
610 | inode->i_gid = attr->ia_gid; | |
6edf9609 TM |
611 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_ACCESS |
612 | | NFS_INO_INVALID_ACL); | |
65e4308d TM |
613 | } |
614 | if ((attr->ia_valid & ATTR_SIZE) != 0) { | |
91d5b470 | 615 | nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC); |
a3d01454 | 616 | nfs_vmtruncate(inode, attr->ia_size); |
65e4308d | 617 | } |
f044636d TM |
618 | nfs_update_inode(inode, fattr); |
619 | spin_unlock(&inode->i_lock); | |
1da177e4 | 620 | } |
ddda8e0a | 621 | EXPORT_SYMBOL_GPL(nfs_setattr_update_inode); |
1da177e4 | 622 | |
311324ad TM |
623 | static void nfs_request_parent_use_readdirplus(struct dentry *dentry) |
624 | { | |
625 | struct dentry *parent; | |
626 | ||
627 | parent = dget_parent(dentry); | |
628 | nfs_force_use_readdirplus(parent->d_inode); | |
629 | dput(parent); | |
630 | } | |
631 | ||
632 | static bool nfs_need_revalidate_inode(struct inode *inode) | |
633 | { | |
634 | if (NFS_I(inode)->cache_validity & | |
635 | (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL)) | |
636 | return true; | |
637 | if (nfs_attribute_cache_expired(inode)) | |
638 | return true; | |
639 | return false; | |
640 | } | |
641 | ||
1da177e4 LT |
642 | int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
643 | { | |
644 | struct inode *inode = dentry->d_inode; | |
55296809 | 645 | int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME; |
16caf5b6 | 646 | int err = 0; |
1da177e4 | 647 | |
f4ce1299 | 648 | trace_nfs_getattr_enter(inode); |
acdc53b2 | 649 | /* Flush out writes to the server in order to update c/mtime. */ |
28c494c5 | 650 | if (S_ISREG(inode->i_mode)) { |
4d346bea | 651 | err = nfs_sync_inode(inode); |
acdc53b2 TM |
652 | if (err) |
653 | goto out; | |
28c494c5 | 654 | } |
fc33a7bb CH |
655 | |
656 | /* | |
657 | * We may force a getattr if the user cares about atime. | |
658 | * | |
659 | * Note that we only have to check the vfsmount flags here: | |
660 | * - NFS always sets S_NOATIME by so checking it would give a | |
661 | * bogus result | |
662 | * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is | |
663 | * no point in checking those. | |
664 | */ | |
665 | if ((mnt->mnt_flags & MNT_NOATIME) || | |
666 | ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
1da177e4 | 667 | need_atime = 0; |
fc33a7bb | 668 | |
311324ad TM |
669 | if (need_atime || nfs_need_revalidate_inode(inode)) { |
670 | struct nfs_server *server = NFS_SERVER(inode); | |
671 | ||
672 | if (server->caps & NFS_CAP_READDIRPLUS) | |
673 | nfs_request_parent_use_readdirplus(dentry); | |
674 | err = __nfs_revalidate_inode(server, inode); | |
675 | } | |
4e769b93 | 676 | if (!err) { |
1da177e4 | 677 | generic_fillattr(inode, stat); |
f43bf0be | 678 | stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode)); |
4e769b93 | 679 | } |
acdc53b2 | 680 | out: |
f4ce1299 | 681 | trace_nfs_getattr_exit(inode, err); |
1da177e4 LT |
682 | return err; |
683 | } | |
ddda8e0a | 684 | EXPORT_SYMBOL_GPL(nfs_getattr); |
1da177e4 | 685 | |
f11ac8db TM |
686 | static void nfs_init_lock_context(struct nfs_lock_context *l_ctx) |
687 | { | |
688 | atomic_set(&l_ctx->count, 1); | |
2a369153 TM |
689 | l_ctx->lockowner.l_owner = current->files; |
690 | l_ctx->lockowner.l_pid = current->tgid; | |
f11ac8db | 691 | INIT_LIST_HEAD(&l_ctx->list); |
577b4232 | 692 | nfs_iocounter_init(&l_ctx->io_count); |
f11ac8db TM |
693 | } |
694 | ||
695 | static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx) | |
696 | { | |
8c86899f TM |
697 | struct nfs_lock_context *head = &ctx->lock_context; |
698 | struct nfs_lock_context *pos = head; | |
f11ac8db | 699 | |
8c86899f | 700 | do { |
2a369153 | 701 | if (pos->lockowner.l_owner != current->files) |
f11ac8db | 702 | continue; |
2a369153 | 703 | if (pos->lockowner.l_pid != current->tgid) |
f11ac8db TM |
704 | continue; |
705 | atomic_inc(&pos->count); | |
706 | return pos; | |
8c86899f | 707 | } while ((pos = list_entry(pos->list.next, typeof(*pos), list)) != head); |
f11ac8db TM |
708 | return NULL; |
709 | } | |
710 | ||
711 | struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx) | |
712 | { | |
713 | struct nfs_lock_context *res, *new = NULL; | |
3d4ff43d | 714 | struct inode *inode = ctx->dentry->d_inode; |
f11ac8db TM |
715 | |
716 | spin_lock(&inode->i_lock); | |
717 | res = __nfs_find_lock_context(ctx); | |
718 | if (res == NULL) { | |
719 | spin_unlock(&inode->i_lock); | |
720 | new = kmalloc(sizeof(*new), GFP_KERNEL); | |
721 | if (new == NULL) | |
b3c54de6 | 722 | return ERR_PTR(-ENOMEM); |
f11ac8db TM |
723 | nfs_init_lock_context(new); |
724 | spin_lock(&inode->i_lock); | |
725 | res = __nfs_find_lock_context(ctx); | |
726 | if (res == NULL) { | |
727 | list_add_tail(&new->list, &ctx->lock_context.list); | |
728 | new->open_context = ctx; | |
729 | res = new; | |
730 | new = NULL; | |
731 | } | |
732 | } | |
733 | spin_unlock(&inode->i_lock); | |
734 | kfree(new); | |
735 | return res; | |
736 | } | |
1c6dcbe5 | 737 | EXPORT_SYMBOL_GPL(nfs_get_lock_context); |
f11ac8db TM |
738 | |
739 | void nfs_put_lock_context(struct nfs_lock_context *l_ctx) | |
740 | { | |
741 | struct nfs_open_context *ctx = l_ctx->open_context; | |
3d4ff43d | 742 | struct inode *inode = ctx->dentry->d_inode; |
f11ac8db TM |
743 | |
744 | if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock)) | |
745 | return; | |
746 | list_del(&l_ctx->list); | |
747 | spin_unlock(&inode->i_lock); | |
748 | kfree(l_ctx); | |
749 | } | |
1c6dcbe5 | 750 | EXPORT_SYMBOL_GPL(nfs_put_lock_context); |
f11ac8db | 751 | |
7fe5c398 TM |
752 | /** |
753 | * nfs_close_context - Common close_context() routine NFSv2/v3 | |
754 | * @ctx: pointer to context | |
755 | * @is_sync: is this a synchronous close | |
756 | * | |
757 | * always ensure that the attributes are up to date if we're mounted | |
758 | * with close-to-open semantics | |
759 | */ | |
760 | void nfs_close_context(struct nfs_open_context *ctx, int is_sync) | |
761 | { | |
762 | struct inode *inode; | |
763 | struct nfs_server *server; | |
764 | ||
765 | if (!(ctx->mode & FMODE_WRITE)) | |
766 | return; | |
767 | if (!is_sync) | |
768 | return; | |
3d4ff43d | 769 | inode = ctx->dentry->d_inode; |
7fe5c398 TM |
770 | if (!list_empty(&NFS_I(inode)->open_files)) |
771 | return; | |
772 | server = NFS_SERVER(inode); | |
773 | if (server->flags & NFS_MOUNT_NOCTO) | |
774 | return; | |
775 | nfs_revalidate_inode(server, inode); | |
776 | } | |
ddda8e0a | 777 | EXPORT_SYMBOL_GPL(nfs_close_context); |
7fe5c398 | 778 | |
5ede7b1c | 779 | struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode) |
1da177e4 LT |
780 | { |
781 | struct nfs_open_context *ctx; | |
5ede7b1c AV |
782 | struct rpc_cred *cred = rpc_lookup_cred(); |
783 | if (IS_ERR(cred)) | |
784 | return ERR_CAST(cred); | |
1da177e4 | 785 | |
f52720ca | 786 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); |
5ede7b1c AV |
787 | if (!ctx) { |
788 | put_rpccred(cred); | |
789 | return ERR_PTR(-ENOMEM); | |
1da177e4 | 790 | } |
5ede7b1c AV |
791 | nfs_sb_active(dentry->d_sb); |
792 | ctx->dentry = dget(dentry); | |
793 | ctx->cred = cred; | |
794 | ctx->state = NULL; | |
795 | ctx->mode = f_mode; | |
796 | ctx->flags = 0; | |
797 | ctx->error = 0; | |
798 | nfs_init_lock_context(&ctx->lock_context); | |
799 | ctx->lock_context.open_context = ctx; | |
800 | INIT_LIST_HEAD(&ctx->list); | |
82be417a | 801 | ctx->mdsthreshold = NULL; |
1da177e4 LT |
802 | return ctx; |
803 | } | |
89d77c8f | 804 | EXPORT_SYMBOL_GPL(alloc_nfs_open_context); |
1da177e4 LT |
805 | |
806 | struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx) | |
807 | { | |
808 | if (ctx != NULL) | |
f11ac8db | 809 | atomic_inc(&ctx->lock_context.count); |
1da177e4 LT |
810 | return ctx; |
811 | } | |
89d77c8f | 812 | EXPORT_SYMBOL_GPL(get_nfs_open_context); |
1da177e4 | 813 | |
7fe5c398 | 814 | static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync) |
1da177e4 | 815 | { |
3d4ff43d AV |
816 | struct inode *inode = ctx->dentry->d_inode; |
817 | struct super_block *sb = ctx->dentry->d_sb; | |
3bec63db | 818 | |
5c78f58e | 819 | if (!list_empty(&ctx->list)) { |
ef84303e BH |
820 | if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock)) |
821 | return; | |
822 | list_del(&ctx->list); | |
823 | spin_unlock(&inode->i_lock); | |
ef84303e | 824 | } else if (!atomic_dec_and_test(&ctx->lock_context.count)) |
5e11934d | 825 | return; |
5c78f58e TM |
826 | if (inode != NULL) |
827 | NFS_PROTO(inode)->close_context(ctx, is_sync); | |
3bec63db TM |
828 | if (ctx->cred != NULL) |
829 | put_rpccred(ctx->cred); | |
3d4ff43d | 830 | dput(ctx->dentry); |
322b2b90 | 831 | nfs_sb_deactive(sb); |
82be417a | 832 | kfree(ctx->mdsthreshold); |
3bec63db TM |
833 | kfree(ctx); |
834 | } | |
835 | ||
a49c3c77 TM |
836 | void put_nfs_open_context(struct nfs_open_context *ctx) |
837 | { | |
838 | __put_nfs_open_context(ctx, 0); | |
839 | } | |
89d77c8f | 840 | EXPORT_SYMBOL_GPL(put_nfs_open_context); |
a49c3c77 | 841 | |
1da177e4 LT |
842 | /* |
843 | * Ensure that mmap has a recent RPC credential for use when writing out | |
844 | * shared pages | |
845 | */ | |
c45ffdd2 | 846 | void nfs_inode_attach_open_context(struct nfs_open_context *ctx) |
1da177e4 | 847 | { |
c45ffdd2 | 848 | struct inode *inode = ctx->dentry->d_inode; |
1da177e4 LT |
849 | struct nfs_inode *nfsi = NFS_I(inode); |
850 | ||
1da177e4 LT |
851 | spin_lock(&inode->i_lock); |
852 | list_add(&ctx->list, &nfsi->open_files); | |
853 | spin_unlock(&inode->i_lock); | |
854 | } | |
c45ffdd2 TM |
855 | EXPORT_SYMBOL_GPL(nfs_inode_attach_open_context); |
856 | ||
857 | void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx) | |
858 | { | |
859 | filp->private_data = get_nfs_open_context(ctx); | |
860 | if (list_empty(&ctx->list)) | |
861 | nfs_inode_attach_open_context(ctx); | |
862 | } | |
89d77c8f | 863 | EXPORT_SYMBOL_GPL(nfs_file_set_open_context); |
1da177e4 | 864 | |
d530838b TM |
865 | /* |
866 | * Given an inode, search for an open context with the desired characteristics | |
867 | */ | |
dc0b027d | 868 | struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode) |
1da177e4 LT |
869 | { |
870 | struct nfs_inode *nfsi = NFS_I(inode); | |
871 | struct nfs_open_context *pos, *ctx = NULL; | |
872 | ||
873 | spin_lock(&inode->i_lock); | |
874 | list_for_each_entry(pos, &nfsi->open_files, list) { | |
d530838b TM |
875 | if (cred != NULL && pos->cred != cred) |
876 | continue; | |
1544fa0f TM |
877 | if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode) |
878 | continue; | |
879 | ctx = get_nfs_open_context(pos); | |
880 | break; | |
1da177e4 LT |
881 | } |
882 | spin_unlock(&inode->i_lock); | |
883 | return ctx; | |
884 | } | |
885 | ||
b92dccf6 | 886 | static void nfs_file_clear_open_context(struct file *filp) |
1da177e4 | 887 | { |
cd3758e3 | 888 | struct nfs_open_context *ctx = nfs_file_open_context(filp); |
1da177e4 LT |
889 | |
890 | if (ctx) { | |
c45ffdd2 TM |
891 | struct inode *inode = ctx->dentry->d_inode; |
892 | ||
1da177e4 LT |
893 | filp->private_data = NULL; |
894 | spin_lock(&inode->i_lock); | |
895 | list_move_tail(&ctx->list, &NFS_I(inode)->open_files); | |
896 | spin_unlock(&inode->i_lock); | |
f895c53f | 897 | __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1); |
1da177e4 LT |
898 | } |
899 | } | |
900 | ||
901 | /* | |
902 | * These allocate and release file read/write context information. | |
903 | */ | |
904 | int nfs_open(struct inode *inode, struct file *filp) | |
905 | { | |
906 | struct nfs_open_context *ctx; | |
1da177e4 | 907 | |
5ede7b1c AV |
908 | ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode); |
909 | if (IS_ERR(ctx)) | |
910 | return PTR_ERR(ctx); | |
1da177e4 LT |
911 | nfs_file_set_open_context(filp, ctx); |
912 | put_nfs_open_context(ctx); | |
f1fe29b4 | 913 | nfs_fscache_open_file(inode, filp); |
1da177e4 LT |
914 | return 0; |
915 | } | |
916 | ||
917 | int nfs_release(struct inode *inode, struct file *filp) | |
918 | { | |
1da177e4 LT |
919 | nfs_file_clear_open_context(filp); |
920 | return 0; | |
921 | } | |
922 | ||
923 | /* | |
924 | * This function is called whenever some part of NFS notices that | |
925 | * the cached attributes have to be refreshed. | |
926 | */ | |
927 | int | |
928 | __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode) | |
929 | { | |
930 | int status = -ESTALE; | |
1775fd3e | 931 | struct nfs4_label *label = NULL; |
a3cba2aa | 932 | struct nfs_fattr *fattr = NULL; |
1da177e4 | 933 | struct nfs_inode *nfsi = NFS_I(inode); |
1da177e4 | 934 | |
1e8968c5 NV |
935 | dfprintk(PAGECACHE, "NFS: revalidating (%s/%Lu)\n", |
936 | inode->i_sb->s_id, (unsigned long long)NFS_FILEID(inode)); | |
1da177e4 | 937 | |
f4ce1299 TM |
938 | trace_nfs_revalidate_inode_enter(inode); |
939 | ||
85233a7a | 940 | if (is_bad_inode(inode)) |
691beb13 | 941 | goto out; |
1da177e4 | 942 | if (NFS_STALE(inode)) |
412d582e | 943 | goto out; |
7fdc49c4 | 944 | |
a3cba2aa TM |
945 | status = -ENOMEM; |
946 | fattr = nfs_alloc_fattr(); | |
947 | if (fattr == NULL) | |
948 | goto out; | |
949 | ||
691beb13 | 950 | nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE); |
14c43f76 DQ |
951 | |
952 | label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); | |
953 | if (IS_ERR(label)) { | |
954 | status = PTR_ERR(label); | |
955 | goto out; | |
956 | } | |
957 | ||
1775fd3e | 958 | status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr, label); |
1da177e4 | 959 | if (status != 0) { |
1e8968c5 | 960 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) getattr failed, error=%d\n", |
1da177e4 | 961 | inode->i_sb->s_id, |
1e8968c5 | 962 | (unsigned long long)NFS_FILEID(inode), status); |
1da177e4 LT |
963 | if (status == -ESTALE) { |
964 | nfs_zap_caches(inode); | |
965 | if (!S_ISDIR(inode->i_mode)) | |
3a10c30a | 966 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
1da177e4 | 967 | } |
14c43f76 | 968 | goto err_out; |
1da177e4 LT |
969 | } |
970 | ||
a3cba2aa | 971 | status = nfs_refresh_inode(inode, fattr); |
1da177e4 | 972 | if (status) { |
1e8968c5 | 973 | dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Lu) refresh failed, error=%d\n", |
1da177e4 | 974 | inode->i_sb->s_id, |
1e8968c5 | 975 | (unsigned long long)NFS_FILEID(inode), status); |
14c43f76 | 976 | goto err_out; |
1da177e4 | 977 | } |
55296809 | 978 | |
24aa1fe6 | 979 | if (nfsi->cache_validity & NFS_INO_INVALID_ACL) |
ada70d94 | 980 | nfs_zap_acl_cache(inode); |
55296809 | 981 | |
3da580aa JL |
982 | nfs_setsecurity(inode, fattr, label); |
983 | ||
1e8968c5 | 984 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) revalidation complete\n", |
1da177e4 | 985 | inode->i_sb->s_id, |
1e8968c5 | 986 | (unsigned long long)NFS_FILEID(inode)); |
1da177e4 | 987 | |
14c43f76 DQ |
988 | err_out: |
989 | nfs4_label_free(label); | |
990 | out: | |
a3cba2aa | 991 | nfs_free_fattr(fattr); |
f4ce1299 | 992 | trace_nfs_revalidate_inode_exit(inode, status); |
1da177e4 LT |
993 | return status; |
994 | } | |
995 | ||
996 | int nfs_attribute_timeout(struct inode *inode) | |
997 | { | |
998 | struct nfs_inode *nfsi = NFS_I(inode); | |
999 | ||
d7cf8dd0 TM |
1000 | return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo); |
1001 | } | |
1002 | ||
43f291cd | 1003 | int nfs_attribute_cache_expired(struct inode *inode) |
d7cf8dd0 | 1004 | { |
b4d2314b | 1005 | if (nfs_have_delegated_attributes(inode)) |
1da177e4 | 1006 | return 0; |
d7cf8dd0 | 1007 | return nfs_attribute_timeout(inode); |
1da177e4 LT |
1008 | } |
1009 | ||
1010 | /** | |
1011 | * nfs_revalidate_inode - Revalidate the inode attributes | |
1012 | * @server - pointer to nfs_server struct | |
1013 | * @inode - pointer to inode struct | |
1014 | * | |
1015 | * Updates inode attribute information by retrieving the data from the server. | |
1016 | */ | |
1017 | int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode) | |
1018 | { | |
311324ad | 1019 | if (!nfs_need_revalidate_inode(inode)) |
1da177e4 LT |
1020 | return NFS_STALE(inode) ? -ESTALE : 0; |
1021 | return __nfs_revalidate_inode(server, inode); | |
1022 | } | |
1c606fb7 | 1023 | EXPORT_SYMBOL_GPL(nfs_revalidate_inode); |
1da177e4 | 1024 | |
912a108d N |
1025 | int nfs_revalidate_inode_rcu(struct nfs_server *server, struct inode *inode) |
1026 | { | |
1027 | if (!(NFS_I(inode)->cache_validity & | |
1028 | (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_LABEL)) | |
1029 | && !nfs_attribute_cache_expired(inode)) | |
1030 | return NFS_STALE(inode) ? -ESTALE : 0; | |
1031 | return -ECHILD; | |
1032 | } | |
1033 | ||
1cda707d | 1034 | static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping) |
717d44e8 TM |
1035 | { |
1036 | struct nfs_inode *nfsi = NFS_I(inode); | |
f8806c84 TM |
1037 | int ret; |
1038 | ||
717d44e8 | 1039 | if (mapping->nrpages != 0) { |
f8806c84 | 1040 | if (S_ISREG(inode->i_mode)) { |
ef070dcb | 1041 | unmap_mapping_range(mapping, 0, 0, 0); |
f8806c84 TM |
1042 | ret = nfs_sync_mapping(mapping); |
1043 | if (ret < 0) | |
1044 | return ret; | |
1045 | } | |
1046 | ret = invalidate_inode_pages2(mapping); | |
717d44e8 TM |
1047 | if (ret < 0) |
1048 | return ret; | |
1049 | } | |
d529ef83 JL |
1050 | if (S_ISDIR(inode->i_mode)) { |
1051 | spin_lock(&inode->i_lock); | |
717d44e8 | 1052 | memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf)); |
d529ef83 JL |
1053 | spin_unlock(&inode->i_lock); |
1054 | } | |
717d44e8 | 1055 | nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); |
de242c0b | 1056 | nfs_fscache_wait_on_invalidate(inode); |
f4ce1299 | 1057 | |
1e8968c5 NV |
1058 | dfprintk(PAGECACHE, "NFS: (%s/%Lu) data cache invalidated\n", |
1059 | inode->i_sb->s_id, | |
1060 | (unsigned long long)NFS_FILEID(inode)); | |
717d44e8 TM |
1061 | return 0; |
1062 | } | |
1063 | ||
b4b1eadf TM |
1064 | static bool nfs_mapping_need_revalidate_inode(struct inode *inode) |
1065 | { | |
1066 | if (nfs_have_delegated_attributes(inode)) | |
1067 | return false; | |
1068 | return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE) | |
1069 | || nfs_attribute_timeout(inode) | |
1070 | || NFS_STALE(inode); | |
1071 | } | |
1072 | ||
717d44e8 | 1073 | /** |
874f9463 | 1074 | * __nfs_revalidate_mapping - Revalidate the pagecache |
717d44e8 TM |
1075 | * @inode - pointer to host inode |
1076 | * @mapping - pointer to mapping | |
874f9463 | 1077 | * @may_lock - take inode->i_mutex? |
717d44e8 | 1078 | */ |
874f9463 TM |
1079 | static int __nfs_revalidate_mapping(struct inode *inode, |
1080 | struct address_space *mapping, | |
1081 | bool may_lock) | |
717d44e8 TM |
1082 | { |
1083 | struct nfs_inode *nfsi = NFS_I(inode); | |
d529ef83 | 1084 | unsigned long *bitlock = &nfsi->flags; |
717d44e8 | 1085 | int ret = 0; |
dc59250c | 1086 | |
29418aa4 MG |
1087 | /* swapfiles are not supposed to be shared. */ |
1088 | if (IS_SWAPFILE(inode)) | |
1089 | goto out; | |
1090 | ||
b4b1eadf | 1091 | if (nfs_mapping_need_revalidate_inode(inode)) { |
717d44e8 TM |
1092 | ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode); |
1093 | if (ret < 0) | |
1094 | goto out; | |
7d52e862 | 1095 | } |
d529ef83 JL |
1096 | |
1097 | /* | |
1098 | * We must clear NFS_INO_INVALID_DATA first to ensure that | |
1099 | * invalidations that come in while we're shooting down the mappings | |
1100 | * are respected. But, that leaves a race window where one revalidator | |
1101 | * can clear the flag, and then another checks it before the mapping | |
1102 | * gets invalidated. Fix that by serializing access to this part of | |
1103 | * the function. | |
1104 | * | |
1105 | * At the same time, we need to allow other tasks to see whether we | |
1106 | * might be in the middle of invalidating the pages, so we only set | |
1107 | * the bit lock here if it looks like we're going to be doing that. | |
1108 | */ | |
1109 | for (;;) { | |
74316201 N |
1110 | ret = wait_on_bit_action(bitlock, NFS_INO_INVALIDATING, |
1111 | nfs_wait_bit_killable, TASK_KILLABLE); | |
d529ef83 JL |
1112 | if (ret) |
1113 | goto out; | |
17dfeb91 TM |
1114 | spin_lock(&inode->i_lock); |
1115 | if (test_bit(NFS_INO_INVALIDATING, bitlock)) { | |
1116 | spin_unlock(&inode->i_lock); | |
1117 | continue; | |
1118 | } | |
1119 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) | |
d529ef83 | 1120 | break; |
d529ef83 | 1121 | spin_unlock(&inode->i_lock); |
17dfeb91 | 1122 | goto out; |
f4ce1299 TM |
1123 | } |
1124 | ||
17dfeb91 | 1125 | set_bit(NFS_INO_INVALIDATING, bitlock); |
4db72b40 | 1126 | smp_wmb(); |
17dfeb91 TM |
1127 | nfsi->cache_validity &= ~NFS_INO_INVALID_DATA; |
1128 | spin_unlock(&inode->i_lock); | |
1129 | trace_nfs_invalidate_mapping_enter(inode); | |
874f9463 TM |
1130 | if (may_lock) { |
1131 | mutex_lock(&inode->i_mutex); | |
1132 | ret = nfs_invalidate_mapping(inode, mapping); | |
1133 | mutex_unlock(&inode->i_mutex); | |
1134 | } else | |
1135 | ret = nfs_invalidate_mapping(inode, mapping); | |
17dfeb91 TM |
1136 | trace_nfs_invalidate_mapping_exit(inode, ret); |
1137 | ||
d529ef83 | 1138 | clear_bit_unlock(NFS_INO_INVALIDATING, bitlock); |
4e857c58 | 1139 | smp_mb__after_atomic(); |
d529ef83 | 1140 | wake_up_bit(bitlock, NFS_INO_INVALIDATING); |
cd9ae2b6 | 1141 | out: |
44b11874 | 1142 | return ret; |
7d52e862 TM |
1143 | } |
1144 | ||
874f9463 TM |
1145 | /** |
1146 | * nfs_revalidate_mapping - Revalidate the pagecache | |
1147 | * @inode - pointer to host inode | |
1148 | * @mapping - pointer to mapping | |
1149 | */ | |
1150 | int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping) | |
1151 | { | |
1152 | return __nfs_revalidate_mapping(inode, mapping, false); | |
1153 | } | |
1154 | ||
1155 | /** | |
1156 | * nfs_revalidate_mapping_protected - Revalidate the pagecache | |
1157 | * @inode - pointer to host inode | |
1158 | * @mapping - pointer to mapping | |
1159 | * | |
1160 | * Differs from nfs_revalidate_mapping() in that it grabs the inode->i_mutex | |
1161 | * while invalidating the mapping. | |
1162 | */ | |
1163 | int nfs_revalidate_mapping_protected(struct inode *inode, struct address_space *mapping) | |
1164 | { | |
1165 | return __nfs_revalidate_mapping(inode, mapping, true); | |
1166 | } | |
1167 | ||
27dc1cd3 | 1168 | static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
a895b4a1 TM |
1169 | { |
1170 | struct nfs_inode *nfsi = NFS_I(inode); | |
27dc1cd3 | 1171 | unsigned long ret = 0; |
a895b4a1 | 1172 | |
9e6e70f8 TM |
1173 | if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE) |
1174 | && (fattr->valid & NFS_ATTR_FATTR_CHANGE) | |
a9a4a87a TM |
1175 | && inode->i_version == fattr->pre_change_attr) { |
1176 | inode->i_version = fattr->change_attr; | |
70ca8852 | 1177 | if (S_ISDIR(inode->i_mode)) |
6edf9609 | 1178 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
27dc1cd3 | 1179 | ret |= NFS_INO_INVALID_ATTR; |
70ca8852 | 1180 | } |
a895b4a1 | 1181 | /* If we have atomic WCC data, we may update some attributes */ |
9e6e70f8 TM |
1182 | if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME) |
1183 | && (fattr->valid & NFS_ATTR_FATTR_CTIME) | |
27dc1cd3 TM |
1184 | && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) { |
1185 | memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime)); | |
1186 | ret |= NFS_INO_INVALID_ATTR; | |
1187 | } | |
9e6e70f8 TM |
1188 | |
1189 | if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME) | |
1190 | && (fattr->valid & NFS_ATTR_FATTR_MTIME) | |
1191 | && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) { | |
27dc1cd3 TM |
1192 | memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime)); |
1193 | if (S_ISDIR(inode->i_mode)) | |
6edf9609 | 1194 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_DATA); |
27dc1cd3 | 1195 | ret |= NFS_INO_INVALID_ATTR; |
a895b4a1 | 1196 | } |
9e6e70f8 TM |
1197 | if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE) |
1198 | && (fattr->valid & NFS_ATTR_FATTR_SIZE) | |
1199 | && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size) | |
cb1410c7 | 1200 | && nfsi->nrequests == 0) { |
27dc1cd3 TM |
1201 | i_size_write(inode, nfs_size_to_loff_t(fattr->size)); |
1202 | ret |= NFS_INO_INVALID_ATTR; | |
1203 | } | |
de242c0b | 1204 | |
27dc1cd3 | 1205 | return ret; |
a895b4a1 TM |
1206 | } |
1207 | ||
1da177e4 | 1208 | /** |
33801147 | 1209 | * nfs_check_inode_attributes - verify consistency of the inode attribute cache |
1da177e4 LT |
1210 | * @inode - pointer to inode |
1211 | * @fattr - updated attributes | |
1212 | * | |
1213 | * Verifies the attribute cache. If we have just changed the attributes, | |
1214 | * so that fattr carries weak cache consistency data, then it may | |
1215 | * also update the ctime/mtime/change_attribute. | |
1216 | */ | |
33801147 | 1217 | static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 LT |
1218 | { |
1219 | struct nfs_inode *nfsi = NFS_I(inode); | |
1220 | loff_t cur_size, new_isize; | |
2a3f5fd4 | 1221 | unsigned long invalid = 0; |
1da177e4 | 1222 | |
dc59250c | 1223 | |
01da47bd TM |
1224 | if (nfs_have_delegated_attributes(inode)) |
1225 | return 0; | |
ca62b9c3 | 1226 | /* Has the inode gone and changed behind our back? */ |
9e6e70f8 TM |
1227 | if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) |
1228 | return -EIO; | |
1229 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) | |
ca62b9c3 | 1230 | return -EIO; |
ca62b9c3 | 1231 | |
9e6e70f8 | 1232 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && |
a9a4a87a | 1233 | inode->i_version != fattr->change_attr) |
2a3f5fd4 | 1234 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE; |
1da177e4 | 1235 | |
1da177e4 | 1236 | /* Verify a few of the more important attributes */ |
9e6e70f8 | 1237 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime)) |
fee7fe19 | 1238 | invalid |= NFS_INO_INVALID_ATTR; |
ca62b9c3 | 1239 | |
9e6e70f8 TM |
1240 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { |
1241 | cur_size = i_size_read(inode); | |
1242 | new_isize = nfs_size_to_loff_t(fattr->size); | |
cb1410c7 | 1243 | if (cur_size != new_isize && nfsi->nrequests == 0) |
9e6e70f8 TM |
1244 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE; |
1245 | } | |
1da177e4 LT |
1246 | |
1247 | /* Have any file permissions changed? */ | |
9e6e70f8 TM |
1248 | if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) |
1249 | invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL; | |
9ff593c4 | 1250 | if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && !uid_eq(inode->i_uid, fattr->uid)) |
9e6e70f8 | 1251 | invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL; |
9ff593c4 | 1252 | if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && !gid_eq(inode->i_gid, fattr->gid)) |
2a3f5fd4 | 1253 | invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL; |
1da177e4 LT |
1254 | |
1255 | /* Has the link count changed? */ | |
9e6e70f8 | 1256 | if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink) |
2a3f5fd4 | 1257 | invalid |= NFS_INO_INVALID_ATTR; |
1da177e4 | 1258 | |
9e6e70f8 | 1259 | if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime)) |
2a3f5fd4 TM |
1260 | invalid |= NFS_INO_INVALID_ATIME; |
1261 | ||
1262 | if (invalid != 0) | |
6edf9609 | 1263 | nfs_set_cache_invalid(inode, invalid); |
1da177e4 | 1264 | |
33801147 | 1265 | nfsi->read_cache_jiffies = fattr->time_start; |
1da177e4 LT |
1266 | return 0; |
1267 | } | |
1268 | ||
a10ad176 | 1269 | static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr) |
870a5be8 | 1270 | { |
9e6e70f8 TM |
1271 | if (!(fattr->valid & NFS_ATTR_FATTR_CTIME)) |
1272 | return 0; | |
a10ad176 TM |
1273 | return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0; |
1274 | } | |
1275 | ||
ae05f269 | 1276 | static atomic_long_t nfs_attr_generation_counter; |
4704f0e2 TM |
1277 | |
1278 | static unsigned long nfs_read_attr_generation_counter(void) | |
1279 | { | |
ae05f269 | 1280 | return atomic_long_read(&nfs_attr_generation_counter); |
4704f0e2 TM |
1281 | } |
1282 | ||
1283 | unsigned long nfs_inc_attr_generation_counter(void) | |
1284 | { | |
ae05f269 | 1285 | return atomic_long_inc_return(&nfs_attr_generation_counter); |
4704f0e2 | 1286 | } |
3235b403 | 1287 | EXPORT_SYMBOL_GPL(nfs_inc_attr_generation_counter); |
4704f0e2 TM |
1288 | |
1289 | void nfs_fattr_init(struct nfs_fattr *fattr) | |
1290 | { | |
1291 | fattr->valid = 0; | |
1292 | fattr->time_start = jiffies; | |
1293 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
6926afd1 TM |
1294 | fattr->owner_name = NULL; |
1295 | fattr->group_name = NULL; | |
4704f0e2 | 1296 | } |
ddda8e0a | 1297 | EXPORT_SYMBOL_GPL(nfs_fattr_init); |
4704f0e2 | 1298 | |
140e049c TM |
1299 | /** |
1300 | * nfs_fattr_set_barrier | |
1301 | * @fattr: attributes | |
1302 | * | |
1303 | * Used to set a barrier after an attribute was updated. This | |
1304 | * barrier ensures that older attributes from RPC calls that may | |
1305 | * have raced with our update cannot clobber these new values. | |
1306 | * Note that you are still responsible for ensuring that other | |
1307 | * operations which change the attribute on the server do not | |
1308 | * collide. | |
1309 | */ | |
1310 | void nfs_fattr_set_barrier(struct nfs_fattr *fattr) | |
1311 | { | |
1312 | fattr->gencount = nfs_inc_attr_generation_counter(); | |
1313 | } | |
1314 | ||
2d36bfde TM |
1315 | struct nfs_fattr *nfs_alloc_fattr(void) |
1316 | { | |
1317 | struct nfs_fattr *fattr; | |
1318 | ||
1319 | fattr = kmalloc(sizeof(*fattr), GFP_NOFS); | |
1320 | if (fattr != NULL) | |
1321 | nfs_fattr_init(fattr); | |
1322 | return fattr; | |
1323 | } | |
ddda8e0a | 1324 | EXPORT_SYMBOL_GPL(nfs_alloc_fattr); |
2d36bfde TM |
1325 | |
1326 | struct nfs_fh *nfs_alloc_fhandle(void) | |
1327 | { | |
1328 | struct nfs_fh *fh; | |
1329 | ||
1330 | fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS); | |
1331 | if (fh != NULL) | |
1332 | fh->size = 0; | |
1333 | return fh; | |
1334 | } | |
ddda8e0a | 1335 | EXPORT_SYMBOL_GPL(nfs_alloc_fhandle); |
2d36bfde | 1336 | |
e27d359e | 1337 | #ifdef NFS_DEBUG |
d8e0539e WAA |
1338 | /* |
1339 | * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle | |
1340 | * in the same way that wireshark does | |
1341 | * | |
1342 | * @fh: file handle | |
1343 | * | |
1344 | * For debugging only. | |
1345 | */ | |
1346 | u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh) | |
1347 | { | |
1348 | /* wireshark uses 32-bit AUTODIN crc and does a bitwise | |
1349 | * not on the result */ | |
1264a2f0 | 1350 | return nfs_fhandle_hash(fh); |
d8e0539e | 1351 | } |
9e6ee76d | 1352 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle_hash); |
d8e0539e WAA |
1353 | |
1354 | /* | |
20d27e92 CL |
1355 | * _nfs_display_fhandle - display an NFS file handle on the console |
1356 | * | |
1357 | * @fh: file handle to display | |
1358 | * @caption: display caption | |
1359 | * | |
1360 | * For debugging only. | |
1361 | */ | |
20d27e92 CL |
1362 | void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption) |
1363 | { | |
1364 | unsigned short i; | |
1365 | ||
fa68a1ba | 1366 | if (fh == NULL || fh->size == 0) { |
20d27e92 CL |
1367 | printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh); |
1368 | return; | |
1369 | } | |
1370 | ||
d8e0539e WAA |
1371 | printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n", |
1372 | caption, fh, fh->size, _nfs_display_fhandle_hash(fh)); | |
20d27e92 CL |
1373 | for (i = 0; i < fh->size; i += 16) { |
1374 | __be32 *pos = (__be32 *)&fh->data[i]; | |
1375 | ||
1376 | switch ((fh->size - i - 1) >> 2) { | |
1377 | case 0: | |
1378 | printk(KERN_DEFAULT " %08x\n", | |
1379 | be32_to_cpup(pos)); | |
1380 | break; | |
1381 | case 1: | |
1382 | printk(KERN_DEFAULT " %08x %08x\n", | |
1383 | be32_to_cpup(pos), be32_to_cpup(pos + 1)); | |
1384 | break; | |
1385 | case 2: | |
1386 | printk(KERN_DEFAULT " %08x %08x %08x\n", | |
1387 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1388 | be32_to_cpup(pos + 2)); | |
1389 | break; | |
1390 | default: | |
1391 | printk(KERN_DEFAULT " %08x %08x %08x %08x\n", | |
1392 | be32_to_cpup(pos), be32_to_cpup(pos + 1), | |
1393 | be32_to_cpup(pos + 2), be32_to_cpup(pos + 3)); | |
1394 | } | |
1395 | } | |
1396 | } | |
9e6ee76d | 1397 | EXPORT_SYMBOL_GPL(_nfs_display_fhandle); |
20d27e92 CL |
1398 | #endif |
1399 | ||
a10ad176 TM |
1400 | /** |
1401 | * nfs_inode_attrs_need_update - check if the inode attributes need updating | |
1402 | * @inode - pointer to inode | |
1403 | * @fattr - attributes | |
1404 | * | |
1405 | * Attempt to divine whether or not an RPC call reply carrying stale | |
1406 | * attributes got scheduled after another call carrying updated ones. | |
1407 | * | |
1408 | * To do so, the function first assumes that a more recent ctime means | |
1409 | * that the attributes in fattr are newer, however it also attempt to | |
1410 | * catch the case where ctime either didn't change, or went backwards | |
1411 | * (if someone reset the clock on the server) by looking at whether | |
1412 | * or not this RPC call was started after the inode was last updated. | |
4704f0e2 | 1413 | * Note also the check for wraparound of 'attr_gencount' |
a10ad176 TM |
1414 | * |
1415 | * The function returns 'true' if it thinks the attributes in 'fattr' are | |
1416 | * more recent than the ones cached in the inode. | |
1417 | * | |
1418 | */ | |
1419 | static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr) | |
1420 | { | |
1421 | const struct nfs_inode *nfsi = NFS_I(inode); | |
1422 | ||
4704f0e2 | 1423 | return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 || |
03254e65 | 1424 | nfs_ctime_need_update(inode, fattr) || |
4704f0e2 | 1425 | ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0); |
a10ad176 TM |
1426 | } |
1427 | ||
d8c951c3 TM |
1428 | /* |
1429 | * Don't trust the change_attribute, mtime, ctime or size if | |
1430 | * a pnfs LAYOUTCOMMIT is outstanding | |
1431 | */ | |
1432 | static void nfs_inode_attrs_handle_layoutcommit(struct inode *inode, | |
1433 | struct nfs_fattr *fattr) | |
1434 | { | |
1435 | if (pnfs_layoutcommit_outstanding(inode)) | |
1436 | fattr->valid &= ~(NFS_ATTR_FATTR_CHANGE | | |
1437 | NFS_ATTR_FATTR_MTIME | | |
1438 | NFS_ATTR_FATTR_CTIME | | |
1439 | NFS_ATTR_FATTR_SIZE); | |
1440 | } | |
1441 | ||
a10ad176 TM |
1442 | static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr) |
1443 | { | |
f4ce1299 TM |
1444 | int ret; |
1445 | ||
1446 | trace_nfs_refresh_inode_enter(inode); | |
1447 | ||
d8c951c3 TM |
1448 | nfs_inode_attrs_handle_layoutcommit(inode, fattr); |
1449 | ||
a10ad176 | 1450 | if (nfs_inode_attrs_need_update(inode, fattr)) |
f4ce1299 TM |
1451 | ret = nfs_update_inode(inode, fattr); |
1452 | else | |
1453 | ret = nfs_check_inode_attributes(inode, fattr); | |
1454 | ||
1455 | trace_nfs_refresh_inode_exit(inode, ret); | |
1456 | return ret; | |
870a5be8 TM |
1457 | } |
1458 | ||
33801147 TM |
1459 | /** |
1460 | * nfs_refresh_inode - try to update the inode attribute cache | |
1461 | * @inode - pointer to inode | |
1462 | * @fattr - updated attributes | |
1463 | * | |
1464 | * Check that an RPC call that returned attributes has not overlapped with | |
1465 | * other recent updates of the inode metadata, then decide whether it is | |
1466 | * safe to do a full update of the inode attributes, or whether just to | |
1467 | * call nfs_check_inode_attributes. | |
1468 | */ | |
1469 | int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr) | |
1470 | { | |
33801147 TM |
1471 | int status; |
1472 | ||
1473 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) | |
1474 | return 0; | |
1475 | spin_lock(&inode->i_lock); | |
870a5be8 | 1476 | status = nfs_refresh_inode_locked(inode, fattr); |
33801147 | 1477 | spin_unlock(&inode->i_lock); |
ef79c097 | 1478 | |
33801147 TM |
1479 | return status; |
1480 | } | |
ddda8e0a | 1481 | EXPORT_SYMBOL_GPL(nfs_refresh_inode); |
33801147 | 1482 | |
d65f557f TM |
1483 | static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr) |
1484 | { | |
6edf9609 | 1485 | unsigned long invalid = NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE; |
d65f557f | 1486 | |
6edf9609 TM |
1487 | if (S_ISDIR(inode->i_mode)) |
1488 | invalid |= NFS_INO_INVALID_DATA; | |
1489 | nfs_set_cache_invalid(inode, invalid); | |
d65f557f TM |
1490 | if ((fattr->valid & NFS_ATTR_FATTR) == 0) |
1491 | return 0; | |
1492 | return nfs_refresh_inode_locked(inode, fattr); | |
1493 | } | |
1494 | ||
decf491f TM |
1495 | /** |
1496 | * nfs_post_op_update_inode - try to update the inode attribute cache | |
1497 | * @inode - pointer to inode | |
1498 | * @fattr - updated attributes | |
1499 | * | |
1500 | * After an operation that has changed the inode metadata, mark the | |
1501 | * attribute cache as being invalid, then try to update it. | |
f551e44f CL |
1502 | * |
1503 | * NB: if the server didn't return any post op attributes, this | |
1504 | * function will force the retrieval of attributes before the next | |
1505 | * NFS request. Thus it should be used only for operations that | |
1506 | * are expected to change one or more attributes, to avoid | |
1507 | * unnecessary NFS requests and trips through nfs_update_inode(). | |
decf491f TM |
1508 | */ |
1509 | int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr) | |
1510 | { | |
d65f557f | 1511 | int status; |
decf491f | 1512 | |
7668fdbe | 1513 | spin_lock(&inode->i_lock); |
92d64e47 | 1514 | nfs_fattr_set_barrier(fattr); |
d65f557f | 1515 | status = nfs_post_op_update_inode_locked(inode, fattr); |
7668fdbe | 1516 | spin_unlock(&inode->i_lock); |
aa9c2669 | 1517 | |
870a5be8 | 1518 | return status; |
decf491f | 1519 | } |
1c606fb7 | 1520 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode); |
decf491f | 1521 | |
70ca8852 | 1522 | /** |
a08a8cd3 | 1523 | * nfs_post_op_update_inode_force_wcc_locked - update the inode attribute cache |
70ca8852 TM |
1524 | * @inode - pointer to inode |
1525 | * @fattr - updated attributes | |
1526 | * | |
1527 | * After an operation that has changed the inode metadata, mark the | |
1528 | * attribute cache as being invalid, then try to update it. Fake up | |
1529 | * weak cache consistency data, if none exist. | |
1530 | * | |
1531 | * This function is mainly designed to be used by the ->write_done() functions. | |
1532 | */ | |
a08a8cd3 | 1533 | int nfs_post_op_update_inode_force_wcc_locked(struct inode *inode, struct nfs_fattr *fattr) |
70ca8852 | 1534 | { |
d65f557f TM |
1535 | int status; |
1536 | ||
d65f557f TM |
1537 | /* Don't do a WCC update if these attributes are already stale */ |
1538 | if ((fattr->valid & NFS_ATTR_FATTR) == 0 || | |
1539 | !nfs_inode_attrs_need_update(inode, fattr)) { | |
9e6e70f8 TM |
1540 | fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE |
1541 | | NFS_ATTR_FATTR_PRESIZE | |
1542 | | NFS_ATTR_FATTR_PREMTIME | |
1543 | | NFS_ATTR_FATTR_PRECTIME); | |
d65f557f TM |
1544 | goto out_noforce; |
1545 | } | |
9e6e70f8 TM |
1546 | if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 && |
1547 | (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) { | |
a9a4a87a | 1548 | fattr->pre_change_attr = inode->i_version; |
9e6e70f8 | 1549 | fattr->valid |= NFS_ATTR_FATTR_PRECHANGE; |
70ca8852 | 1550 | } |
9e6e70f8 TM |
1551 | if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 && |
1552 | (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) { | |
70ca8852 | 1553 | memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime)); |
9e6e70f8 TM |
1554 | fattr->valid |= NFS_ATTR_FATTR_PRECTIME; |
1555 | } | |
1556 | if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 && | |
1557 | (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) { | |
70ca8852 | 1558 | memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime)); |
9e6e70f8 TM |
1559 | fattr->valid |= NFS_ATTR_FATTR_PREMTIME; |
1560 | } | |
1561 | if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 && | |
1562 | (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) { | |
a3d01454 | 1563 | fattr->pre_size = i_size_read(inode); |
9e6e70f8 | 1564 | fattr->valid |= NFS_ATTR_FATTR_PRESIZE; |
70ca8852 | 1565 | } |
d65f557f TM |
1566 | out_noforce: |
1567 | status = nfs_post_op_update_inode_locked(inode, fattr); | |
a08a8cd3 TM |
1568 | return status; |
1569 | } | |
1570 | ||
1571 | /** | |
1572 | * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache | |
1573 | * @inode - pointer to inode | |
1574 | * @fattr - updated attributes | |
1575 | * | |
1576 | * After an operation that has changed the inode metadata, mark the | |
1577 | * attribute cache as being invalid, then try to update it. Fake up | |
1578 | * weak cache consistency data, if none exist. | |
1579 | * | |
1580 | * This function is mainly designed to be used by the ->write_done() functions. | |
1581 | */ | |
1582 | int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr) | |
1583 | { | |
1584 | int status; | |
1585 | ||
1586 | spin_lock(&inode->i_lock); | |
8f8ba1d7 | 1587 | nfs_fattr_set_barrier(fattr); |
a08a8cd3 | 1588 | status = nfs_post_op_update_inode_force_wcc_locked(inode, fattr); |
d65f557f TM |
1589 | spin_unlock(&inode->i_lock); |
1590 | return status; | |
70ca8852 | 1591 | } |
ddda8e0a | 1592 | EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc); |
70ca8852 | 1593 | |
1da177e4 LT |
1594 | /* |
1595 | * Many nfs protocol calls return the new file attributes after | |
1596 | * an operation. Here we update the inode to reflect the state | |
1597 | * of the server's inode. | |
1598 | * | |
1599 | * This is a bit tricky because we have to make sure all dirty pages | |
1600 | * have been sent off to the server before calling invalidate_inode_pages. | |
1601 | * To make sure no other process adds more write requests while we try | |
1602 | * our best to flush them, we make them sleep during the attribute refresh. | |
1603 | * | |
1604 | * A very similar scenario holds for the dir cache. | |
1605 | */ | |
24aa1fe6 | 1606 | static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr) |
1da177e4 | 1607 | { |
8b4bdcf8 | 1608 | struct nfs_server *server; |
1da177e4 | 1609 | struct nfs_inode *nfsi = NFS_I(inode); |
951a143b | 1610 | loff_t cur_isize, new_isize; |
2a3f5fd4 | 1611 | unsigned long invalid = 0; |
3e7d950a | 1612 | unsigned long now = jiffies; |
62ab460c | 1613 | unsigned long save_cache_validity; |
1da177e4 | 1614 | |
1e8968c5 | 1615 | dfprintk(VFS, "NFS: %s(%s/%lu fh_crc=0x%08x ct=%d info=0x%x)\n", |
3110ff80 | 1616 | __func__, inode->i_sb->s_id, inode->i_ino, |
4f1abd22 | 1617 | nfs_display_fhandle_hash(NFS_FH(inode)), |
1da177e4 LT |
1618 | atomic_read(&inode->i_count), fattr->valid); |
1619 | ||
e73e6c9e MT |
1620 | if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) { |
1621 | printk(KERN_ERR "NFS: server %s error: fileid changed\n" | |
1622 | "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n", | |
1623 | NFS_SERVER(inode)->nfs_client->cl_hostname, | |
1624 | inode->i_sb->s_id, (long long)nfsi->fileid, | |
1625 | (long long)fattr->fileid); | |
1626 | goto out_err; | |
1627 | } | |
1da177e4 LT |
1628 | |
1629 | /* | |
1630 | * Make sure the inode's type hasn't changed. | |
1631 | */ | |
e73e6c9e MT |
1632 | if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) { |
1633 | /* | |
1634 | * Big trouble! The inode has become a different object. | |
1635 | */ | |
1e8968c5 | 1636 | printk(KERN_DEBUG "NFS: %s: inode %lu mode changed, %07o to %07o\n", |
e73e6c9e MT |
1637 | __func__, inode->i_ino, inode->i_mode, fattr->mode); |
1638 | goto out_err; | |
1639 | } | |
1da177e4 | 1640 | |
8b4bdcf8 | 1641 | server = NFS_SERVER(inode); |
a0356862 | 1642 | /* Update the fsid? */ |
9e6e70f8 | 1643 | if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) && |
c37dcd33 | 1644 | !nfs_fsid_equal(&server->fsid, &fattr->fsid) && |
36d43a43 | 1645 | !IS_AUTOMOUNT(inode)) |
8b4bdcf8 TM |
1646 | server->fsid = fattr->fsid; |
1647 | ||
1da177e4 LT |
1648 | /* |
1649 | * Update the read time so we don't revalidate too often. | |
1650 | */ | |
33801147 | 1651 | nfsi->read_cache_jiffies = fattr->time_start; |
3e7d950a | 1652 | |
62ab460c TM |
1653 | save_cache_validity = nfsi->cache_validity; |
1654 | nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR | |
1655 | | NFS_INO_INVALID_ATIME | |
1656 | | NFS_INO_REVAL_FORCED | |
1657 | | NFS_INO_REVAL_PAGECACHE); | |
1da177e4 | 1658 | |
a895b4a1 | 1659 | /* Do atomic weak cache consistency updates */ |
27dc1cd3 | 1660 | invalid |= nfs_wcc_update_inode(inode, fattr); |
a895b4a1 | 1661 | |
47aabaa7 | 1662 | /* More cache consistency checks */ |
9e6e70f8 | 1663 | if (fattr->valid & NFS_ATTR_FATTR_CHANGE) { |
a9a4a87a | 1664 | if (inode->i_version != fattr->change_attr) { |
9e6e70f8 TM |
1665 | dprintk("NFS: change_attr change on server for file %s/%ld\n", |
1666 | inode->i_sb->s_id, inode->i_ino); | |
6a4506c0 TM |
1667 | invalid |= NFS_INO_INVALID_ATTR |
1668 | | NFS_INO_INVALID_DATA | |
1669 | | NFS_INO_INVALID_ACCESS | |
1670 | | NFS_INO_INVALID_ACL | |
1671 | | NFS_INO_REVAL_PAGECACHE; | |
9e6e70f8 TM |
1672 | if (S_ISDIR(inode->i_mode)) |
1673 | nfs_force_lookup_revalidate(inode); | |
a9a4a87a | 1674 | inode->i_version = fattr->change_attr; |
9e6e70f8 | 1675 | } |
62ab460c | 1676 | } else if (server->caps & NFS_CAP_CHANGE_ATTR) |
43b6535e | 1677 | nfsi->cache_validity |= save_cache_validity; |
9e6e70f8 TM |
1678 | |
1679 | if (fattr->valid & NFS_ATTR_FATTR_MTIME) { | |
fee7fe19 | 1680 | memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime)); |
62ab460c | 1681 | } else if (server->caps & NFS_CAP_MTIME) |
43b6535e TM |
1682 | nfsi->cache_validity |= save_cache_validity & |
1683 | (NFS_INO_INVALID_ATTR | |
62ab460c TM |
1684 | | NFS_INO_REVAL_FORCED); |
1685 | ||
9e6e70f8 | 1686 | if (fattr->valid & NFS_ATTR_FATTR_CTIME) { |
fee7fe19 | 1687 | memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime)); |
62ab460c | 1688 | } else if (server->caps & NFS_CAP_CTIME) |
43b6535e TM |
1689 | nfsi->cache_validity |= save_cache_validity & |
1690 | (NFS_INO_INVALID_ATTR | |
62ab460c | 1691 | | NFS_INO_REVAL_FORCED); |
47aabaa7 | 1692 | |
951a143b | 1693 | /* Check if our cached file size is stale */ |
9e6e70f8 TM |
1694 | if (fattr->valid & NFS_ATTR_FATTR_SIZE) { |
1695 | new_isize = nfs_size_to_loff_t(fattr->size); | |
1696 | cur_isize = i_size_read(inode); | |
1697 | if (new_isize != cur_isize) { | |
1698 | /* Do we perhaps have any outstanding writes, or has | |
1699 | * the file grown beyond our last write? */ | |
cb1410c7 | 1700 | if ((nfsi->nrequests == 0) || new_isize > cur_isize) { |
9e6e70f8 TM |
1701 | i_size_write(inode, new_isize); |
1702 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; | |
f2467b6f | 1703 | invalid &= ~NFS_INO_REVAL_PAGECACHE; |
9e6e70f8 | 1704 | } |
60c16ea8 HJ |
1705 | dprintk("NFS: isize change on server for file %s/%ld " |
1706 | "(%Ld to %Ld)\n", | |
1707 | inode->i_sb->s_id, | |
1708 | inode->i_ino, | |
1709 | (long long)cur_isize, | |
1710 | (long long)new_isize); | |
1da177e4 | 1711 | } |
62ab460c | 1712 | } else |
43b6535e TM |
1713 | nfsi->cache_validity |= save_cache_validity & |
1714 | (NFS_INO_INVALID_ATTR | |
62ab460c TM |
1715 | | NFS_INO_REVAL_PAGECACHE |
1716 | | NFS_INO_REVAL_FORCED); | |
1da177e4 | 1717 | |
1da177e4 | 1718 | |
9e6e70f8 TM |
1719 | if (fattr->valid & NFS_ATTR_FATTR_ATIME) |
1720 | memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime)); | |
62ab460c | 1721 | else if (server->caps & NFS_CAP_ATIME) |
43b6535e TM |
1722 | nfsi->cache_validity |= save_cache_validity & |
1723 | (NFS_INO_INVALID_ATIME | |
62ab460c | 1724 | | NFS_INO_REVAL_FORCED); |
1da177e4 | 1725 | |
9e6e70f8 TM |
1726 | if (fattr->valid & NFS_ATTR_FATTR_MODE) { |
1727 | if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) { | |
9b4b3513 TM |
1728 | umode_t newmode = inode->i_mode & S_IFMT; |
1729 | newmode |= fattr->mode & S_IALLUGO; | |
1730 | inode->i_mode = newmode; | |
9e6e70f8 | 1731 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL; |
9e6e70f8 | 1732 | } |
62ab460c | 1733 | } else if (server->caps & NFS_CAP_MODE) |
43b6535e TM |
1734 | nfsi->cache_validity |= save_cache_validity & |
1735 | (NFS_INO_INVALID_ATTR | |
62ab460c TM |
1736 | | NFS_INO_INVALID_ACCESS |
1737 | | NFS_INO_INVALID_ACL | |
1738 | | NFS_INO_REVAL_FORCED); | |
1739 | ||
9e6e70f8 | 1740 | if (fattr->valid & NFS_ATTR_FATTR_OWNER) { |
9ff593c4 | 1741 | if (!uid_eq(inode->i_uid, fattr->uid)) { |
9e6e70f8 TM |
1742 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL; |
1743 | inode->i_uid = fattr->uid; | |
1744 | } | |
62ab460c | 1745 | } else if (server->caps & NFS_CAP_OWNER) |
43b6535e TM |
1746 | nfsi->cache_validity |= save_cache_validity & |
1747 | (NFS_INO_INVALID_ATTR | |
62ab460c TM |
1748 | | NFS_INO_INVALID_ACCESS |
1749 | | NFS_INO_INVALID_ACL | |
1750 | | NFS_INO_REVAL_FORCED); | |
1751 | ||
9e6e70f8 | 1752 | if (fattr->valid & NFS_ATTR_FATTR_GROUP) { |
9ff593c4 | 1753 | if (!gid_eq(inode->i_gid, fattr->gid)) { |
9e6e70f8 TM |
1754 | invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL; |
1755 | inode->i_gid = fattr->gid; | |
1756 | } | |
62ab460c | 1757 | } else if (server->caps & NFS_CAP_OWNER_GROUP) |
43b6535e TM |
1758 | nfsi->cache_validity |= save_cache_validity & |
1759 | (NFS_INO_INVALID_ATTR | |
62ab460c TM |
1760 | | NFS_INO_INVALID_ACCESS |
1761 | | NFS_INO_INVALID_ACL | |
1762 | | NFS_INO_REVAL_FORCED); | |
921615f1 | 1763 | |
9e6e70f8 TM |
1764 | if (fattr->valid & NFS_ATTR_FATTR_NLINK) { |
1765 | if (inode->i_nlink != fattr->nlink) { | |
1766 | invalid |= NFS_INO_INVALID_ATTR; | |
1767 | if (S_ISDIR(inode->i_mode)) | |
1768 | invalid |= NFS_INO_INVALID_DATA; | |
bfe86848 | 1769 | set_nlink(inode, fattr->nlink); |
9e6e70f8 | 1770 | } |
62ab460c | 1771 | } else if (server->caps & NFS_CAP_NLINK) |
43b6535e TM |
1772 | nfsi->cache_validity |= save_cache_validity & |
1773 | (NFS_INO_INVALID_ATTR | |
62ab460c | 1774 | | NFS_INO_REVAL_FORCED); |
1da177e4 | 1775 | |
9e6e70f8 | 1776 | if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) { |
1da177e4 LT |
1777 | /* |
1778 | * report the blocks in 512byte units | |
1779 | */ | |
1780 | inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used); | |
1da177e4 | 1781 | } |
9e6e70f8 TM |
1782 | if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED) |
1783 | inode->i_blocks = fattr->du.nfs2.blocks; | |
1da177e4 LT |
1784 | |
1785 | /* Update attrtimeo value if we're out of the unstable period */ | |
fd1defc2 | 1786 | if (invalid & NFS_INO_INVALID_ATTR) { |
91d5b470 | 1787 | nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE); |
1da177e4 | 1788 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
3e7d950a | 1789 | nfsi->attrtimeo_timestamp = now; |
f5062003 | 1790 | /* Set barrier to be more recent than all outstanding updates */ |
4704f0e2 | 1791 | nfsi->attr_gencount = nfs_inc_attr_generation_counter(); |
6d2b2966 | 1792 | } else { |
64672d55 | 1793 | if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) { |
6d2b2966 TM |
1794 | if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode)) |
1795 | nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode); | |
1796 | nfsi->attrtimeo_timestamp = now; | |
1797 | } | |
f5062003 TM |
1798 | /* Set the barrier to be more recent than this fattr */ |
1799 | if ((long)fattr->gencount - (long)nfsi->attr_gencount > 0) | |
1800 | nfsi->attr_gencount = fattr->gencount; | |
1da177e4 | 1801 | } |
f2115dc9 | 1802 | invalid &= ~NFS_INO_INVALID_ATTR; |
1da177e4 LT |
1803 | /* Don't invalidate the data if we were to blame */ |
1804 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) | |
1805 | || S_ISLNK(inode->i_mode))) | |
1806 | invalid &= ~NFS_INO_INVALID_DATA; | |
011e2a7f | 1807 | if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) || |
62ab460c | 1808 | (save_cache_validity & NFS_INO_REVAL_FORCED)) |
6edf9609 | 1809 | nfs_set_cache_invalid(inode, invalid); |
de242c0b | 1810 | |
1da177e4 | 1811 | return 0; |
b37b03b7 | 1812 | out_err: |
1da177e4 LT |
1813 | /* |
1814 | * No need to worry about unhashing the dentry, as the | |
1815 | * lookup validation will know that the inode is bad. | |
1816 | * (But we fall through to invalidate the caches.) | |
1817 | */ | |
1818 | nfs_invalidate_inode(inode); | |
1da177e4 LT |
1819 | return -ESTALE; |
1820 | } | |
1821 | ||
f7b422b1 | 1822 | struct inode *nfs_alloc_inode(struct super_block *sb) |
1da177e4 LT |
1823 | { |
1824 | struct nfs_inode *nfsi; | |
e94b1766 | 1825 | nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
1826 | if (!nfsi) |
1827 | return NULL; | |
55296809 CL |
1828 | nfsi->flags = 0UL; |
1829 | nfsi->cache_validity = 0UL; | |
89d77c8f | 1830 | #if IS_ENABLED(CONFIG_NFS_V4) |
e50a1c2e BF |
1831 | nfsi->nfs4_acl = NULL; |
1832 | #endif /* CONFIG_NFS_V4 */ | |
1da177e4 LT |
1833 | return &nfsi->vfs_inode; |
1834 | } | |
89d77c8f | 1835 | EXPORT_SYMBOL_GPL(nfs_alloc_inode); |
1da177e4 | 1836 | |
fa0d7e3d | 1837 | static void nfs_i_callback(struct rcu_head *head) |
1da177e4 | 1838 | { |
fa0d7e3d | 1839 | struct inode *inode = container_of(head, struct inode, i_rcu); |
1da177e4 LT |
1840 | kmem_cache_free(nfs_inode_cachep, NFS_I(inode)); |
1841 | } | |
1842 | ||
fa0d7e3d NP |
1843 | void nfs_destroy_inode(struct inode *inode) |
1844 | { | |
1845 | call_rcu(&inode->i_rcu, nfs_i_callback); | |
1846 | } | |
89d77c8f | 1847 | EXPORT_SYMBOL_GPL(nfs_destroy_inode); |
fa0d7e3d | 1848 | |
d75d5414 AM |
1849 | static inline void nfs4_init_once(struct nfs_inode *nfsi) |
1850 | { | |
89d77c8f | 1851 | #if IS_ENABLED(CONFIG_NFS_V4) |
d75d5414 AM |
1852 | INIT_LIST_HEAD(&nfsi->open_states); |
1853 | nfsi->delegation = NULL; | |
d75d5414 | 1854 | init_rwsem(&nfsi->rwsem); |
e5e94017 | 1855 | nfsi->layout = NULL; |
d75d5414 AM |
1856 | #endif |
1857 | } | |
f7b422b1 | 1858 | |
51cc5068 | 1859 | static void init_once(void *foo) |
1da177e4 LT |
1860 | { |
1861 | struct nfs_inode *nfsi = (struct nfs_inode *) foo; | |
1862 | ||
a35afb83 | 1863 | inode_init_once(&nfsi->vfs_inode); |
a35afb83 CL |
1864 | INIT_LIST_HEAD(&nfsi->open_files); |
1865 | INIT_LIST_HEAD(&nfsi->access_cache_entry_lru); | |
1866 | INIT_LIST_HEAD(&nfsi->access_cache_inode_lru); | |
ea2cf228 | 1867 | INIT_LIST_HEAD(&nfsi->commit_info.list); |
cb1410c7 | 1868 | nfsi->nrequests = 0; |
ea2cf228 | 1869 | nfsi->commit_info.ncommit = 0; |
1a0de48a | 1870 | atomic_set(&nfsi->commit_info.rpcs_out, 0); |
565277f6 TM |
1871 | atomic_set(&nfsi->silly_count, 1); |
1872 | INIT_HLIST_HEAD(&nfsi->silly_list); | |
1873 | init_waitqueue_head(&nfsi->waitqueue); | |
a35afb83 | 1874 | nfs4_init_once(nfsi); |
1da177e4 | 1875 | } |
20c2df83 | 1876 | |
f7b422b1 | 1877 | static int __init nfs_init_inodecache(void) |
1da177e4 LT |
1878 | { |
1879 | nfs_inode_cachep = kmem_cache_create("nfs_inode_cache", | |
1880 | sizeof(struct nfs_inode), | |
fffb60f9 PJ |
1881 | 0, (SLAB_RECLAIM_ACCOUNT| |
1882 | SLAB_MEM_SPREAD), | |
20c2df83 | 1883 | init_once); |
1da177e4 LT |
1884 | if (nfs_inode_cachep == NULL) |
1885 | return -ENOMEM; | |
1886 | ||
1887 | return 0; | |
1888 | } | |
1889 | ||
266bee88 | 1890 | static void nfs_destroy_inodecache(void) |
1da177e4 | 1891 | { |
8c0a8537 KS |
1892 | /* |
1893 | * Make sure all delayed rcu free inodes are flushed before we | |
1894 | * destroy cache. | |
1895 | */ | |
1896 | rcu_barrier(); | |
1a1d92c1 | 1897 | kmem_cache_destroy(nfs_inode_cachep); |
1da177e4 LT |
1898 | } |
1899 | ||
5746006f | 1900 | struct workqueue_struct *nfsiod_workqueue; |
89d77c8f | 1901 | EXPORT_SYMBOL_GPL(nfsiod_workqueue); |
5746006f TM |
1902 | |
1903 | /* | |
1904 | * start up the nfsiod workqueue | |
1905 | */ | |
1906 | static int nfsiod_start(void) | |
1907 | { | |
1908 | struct workqueue_struct *wq; | |
1909 | dprintk("RPC: creating workqueue nfsiod\n"); | |
ada609ee | 1910 | wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0); |
5746006f TM |
1911 | if (wq == NULL) |
1912 | return -ENOMEM; | |
1913 | nfsiod_workqueue = wq; | |
1914 | return 0; | |
1915 | } | |
1916 | ||
1917 | /* | |
1918 | * Destroy the nfsiod workqueue | |
1919 | */ | |
1920 | static void nfsiod_stop(void) | |
1921 | { | |
1922 | struct workqueue_struct *wq; | |
1923 | ||
1924 | wq = nfsiod_workqueue; | |
1925 | if (wq == NULL) | |
1926 | return; | |
1927 | nfsiod_workqueue = NULL; | |
1928 | destroy_workqueue(wq); | |
1929 | } | |
1930 | ||
1b340d01 | 1931 | int nfs_net_id; |
9e2e74db | 1932 | EXPORT_SYMBOL_GPL(nfs_net_id); |
1b340d01 SK |
1933 | |
1934 | static int nfs_net_init(struct net *net) | |
1935 | { | |
6b13168b | 1936 | nfs_clients_init(net); |
65b38851 | 1937 | return nfs_fs_proc_net_init(net); |
1b340d01 SK |
1938 | } |
1939 | ||
1940 | static void nfs_net_exit(struct net *net) | |
1941 | { | |
65b38851 | 1942 | nfs_fs_proc_net_exit(net); |
28cd1b3f | 1943 | nfs_cleanup_cb_ident_idr(net); |
1b340d01 SK |
1944 | } |
1945 | ||
1946 | static struct pernet_operations nfs_net_ops = { | |
1947 | .init = nfs_net_init, | |
1948 | .exit = nfs_net_exit, | |
1949 | .id = &nfs_net_id, | |
1950 | .size = sizeof(struct nfs_net), | |
1951 | }; | |
1952 | ||
1da177e4 LT |
1953 | /* |
1954 | * Initialize NFS | |
1955 | */ | |
1956 | static int __init init_nfs_fs(void) | |
1957 | { | |
1958 | int err; | |
1959 | ||
1b340d01 | 1960 | err = register_pernet_subsys(&nfs_net_ops); |
e571cbf1 | 1961 | if (err < 0) |
89d77c8f | 1962 | goto out9; |
e571cbf1 | 1963 | |
8ec442ae DH |
1964 | err = nfs_fscache_register(); |
1965 | if (err < 0) | |
89d77c8f | 1966 | goto out8; |
8ec442ae | 1967 | |
5746006f TM |
1968 | err = nfsiod_start(); |
1969 | if (err) | |
89d77c8f | 1970 | goto out7; |
5746006f | 1971 | |
6aaca566 DH |
1972 | err = nfs_fs_proc_init(); |
1973 | if (err) | |
89d77c8f | 1974 | goto out6; |
6aaca566 | 1975 | |
1da177e4 LT |
1976 | err = nfs_init_nfspagecache(); |
1977 | if (err) | |
89d77c8f | 1978 | goto out5; |
1da177e4 LT |
1979 | |
1980 | err = nfs_init_inodecache(); | |
1981 | if (err) | |
89d77c8f | 1982 | goto out4; |
1da177e4 LT |
1983 | |
1984 | err = nfs_init_readpagecache(); | |
1985 | if (err) | |
89d77c8f | 1986 | goto out3; |
1da177e4 LT |
1987 | |
1988 | err = nfs_init_writepagecache(); | |
1989 | if (err) | |
89d77c8f | 1990 | goto out2; |
1da177e4 | 1991 | |
1da177e4 LT |
1992 | err = nfs_init_directcache(); |
1993 | if (err) | |
89d77c8f | 1994 | goto out1; |
1da177e4 LT |
1995 | |
1996 | #ifdef CONFIG_PROC_FS | |
ec7652aa | 1997 | rpc_proc_register(&init_net, &nfs_rpcstat); |
1da177e4 | 1998 | #endif |
f7b422b1 | 1999 | if ((err = register_nfs_fs()) != 0) |
129d1977 BS |
2000 | goto out0; |
2001 | ||
1da177e4 | 2002 | return 0; |
129d1977 | 2003 | out0: |
1da177e4 | 2004 | #ifdef CONFIG_PROC_FS |
ec7652aa | 2005 | rpc_proc_unregister(&init_net, "nfs"); |
1da177e4 | 2006 | #endif |
1da177e4 | 2007 | nfs_destroy_directcache(); |
89d77c8f | 2008 | out1: |
129d1977 | 2009 | nfs_destroy_writepagecache(); |
89d77c8f | 2010 | out2: |
129d1977 | 2011 | nfs_destroy_readpagecache(); |
89d77c8f | 2012 | out3: |
129d1977 | 2013 | nfs_destroy_inodecache(); |
89d77c8f | 2014 | out4: |
129d1977 | 2015 | nfs_destroy_nfspagecache(); |
89d77c8f | 2016 | out5: |
129d1977 | 2017 | nfs_fs_proc_exit(); |
89d77c8f | 2018 | out6: |
129d1977 | 2019 | nfsiod_stop(); |
89d77c8f | 2020 | out7: |
129d1977 | 2021 | nfs_fscache_unregister(); |
89d77c8f | 2022 | out8: |
129d1977 | 2023 | unregister_pernet_subsys(&nfs_net_ops); |
89d77c8f | 2024 | out9: |
1da177e4 LT |
2025 | return err; |
2026 | } | |
2027 | ||
2028 | static void __exit exit_nfs_fs(void) | |
2029 | { | |
1da177e4 | 2030 | nfs_destroy_directcache(); |
1da177e4 LT |
2031 | nfs_destroy_writepagecache(); |
2032 | nfs_destroy_readpagecache(); | |
2033 | nfs_destroy_inodecache(); | |
2034 | nfs_destroy_nfspagecache(); | |
8ec442ae | 2035 | nfs_fscache_unregister(); |
1b340d01 | 2036 | unregister_pernet_subsys(&nfs_net_ops); |
1da177e4 | 2037 | #ifdef CONFIG_PROC_FS |
ec7652aa | 2038 | rpc_proc_unregister(&init_net, "nfs"); |
1da177e4 | 2039 | #endif |
f7b422b1 | 2040 | unregister_nfs_fs(); |
6aaca566 | 2041 | nfs_fs_proc_exit(); |
5746006f | 2042 | nfsiod_stop(); |
1da177e4 LT |
2043 | } |
2044 | ||
2045 | /* Not quite true; I just maintain it */ | |
2046 | MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); | |
2047 | MODULE_LICENSE("GPL"); | |
f43bf0be | 2048 | module_param(enable_ino64, bool, 0644); |
1da177e4 LT |
2049 | |
2050 | module_init(init_nfs_fs) | |
2051 | module_exit(exit_nfs_fs) |