Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
355da1eb SW |
3 | |
4 | #include <linux/module.h> | |
5 | #include <linux/fs.h> | |
355da1eb SW |
6 | #include <linux/slab.h> |
7 | #include <linux/string.h> | |
8 | #include <linux/uaccess.h> | |
9 | #include <linux/kernel.h> | |
355da1eb SW |
10 | #include <linux/writeback.h> |
11 | #include <linux/vmalloc.h> | |
2cdeb1e4 | 12 | #include <linux/xattr.h> |
4db658ea | 13 | #include <linux/posix_acl.h> |
3e7fbe9c | 14 | #include <linux/random.h> |
a407846e | 15 | #include <linux/sort.h> |
a35ead31 | 16 | #include <linux/iversion.h> |
2d332d5b | 17 | #include <linux/fscrypt.h> |
355da1eb SW |
18 | |
19 | #include "super.h" | |
3d14c5d2 | 20 | #include "mds_client.h" |
99ccbd22 | 21 | #include "cache.h" |
2d332d5b | 22 | #include "crypto.h" |
3d14c5d2 | 23 | #include <linux/ceph/decode.h> |
355da1eb SW |
24 | |
25 | /* | |
26 | * Ceph inode operations | |
27 | * | |
28 | * Implement basic inode helpers (get, alloc) and inode ops (getattr, | |
29 | * setattr, etc.), xattr helpers, and helpers for assimilating | |
30 | * metadata returned by the MDS into our cache. | |
31 | * | |
32 | * Also define helpers for doing asynchronous writeback, invalidation, | |
33 | * and truncation for the benefit of those who can't afford to block | |
34 | * (typically because they are in the message handler path). | |
35 | */ | |
36 | ||
37 | static const struct inode_operations ceph_symlink_iops; | |
79f2f6ad | 38 | static const struct inode_operations ceph_encrypted_symlink_iops; |
355da1eb | 39 | |
1cf89a8d | 40 | static void ceph_inode_work(struct work_struct *work); |
355da1eb SW |
41 | |
42 | /* | |
43 | * find or create an inode, given the ceph ino number | |
44 | */ | |
ad1fee96 YS |
45 | static int ceph_set_ino_cb(struct inode *inode, void *data) |
46 | { | |
ebce3eb2 | 47 | struct ceph_inode_info *ci = ceph_inode(inode); |
1dd8d470 | 48 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
ebce3eb2 JL |
49 | |
50 | ci->i_vino = *(struct ceph_vino *)data; | |
51 | inode->i_ino = ceph_vino_to_ino_t(ci->i_vino); | |
a35ead31 | 52 | inode_set_iversion_raw(inode, 0); |
1dd8d470 XL |
53 | percpu_counter_inc(&mdsc->metric.total_inodes); |
54 | ||
ad1fee96 YS |
55 | return 0; |
56 | } | |
57 | ||
ec9595c0 JL |
58 | /** |
59 | * ceph_new_inode - allocate a new inode in advance of an expected create | |
60 | * @dir: parent directory for new inode | |
61 | * @dentry: dentry that may eventually point to new inode | |
62 | * @mode: mode of new inode | |
63 | * @as_ctx: pointer to inherited security context | |
64 | * | |
65 | * Allocate a new inode in advance of an operation to create a new inode. | |
66 | * This allocates the inode and sets up the acl_sec_ctx with appropriate | |
67 | * info for the new inode. | |
68 | * | |
69 | * Returns a pointer to the new inode or an ERR_PTR. | |
70 | */ | |
71 | struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry, | |
72 | umode_t *mode, struct ceph_acl_sec_ctx *as_ctx) | |
73 | { | |
74 | int err; | |
75 | struct inode *inode; | |
76 | ||
77 | inode = new_inode(dir->i_sb); | |
78 | if (!inode) | |
79 | return ERR_PTR(-ENOMEM); | |
80 | ||
81 | if (!S_ISLNK(*mode)) { | |
82 | err = ceph_pre_init_acls(dir, mode, as_ctx); | |
83 | if (err < 0) | |
84 | goto out_err; | |
85 | } | |
86 | ||
6b5717bd JL |
87 | inode->i_state = 0; |
88 | inode->i_mode = *mode; | |
89 | ||
ec9595c0 JL |
90 | err = ceph_security_init_secctx(dentry, *mode, as_ctx); |
91 | if (err < 0) | |
92 | goto out_err; | |
93 | ||
dd66df00 LH |
94 | /* |
95 | * We'll skip setting fscrypt context for snapshots, leaving that for | |
96 | * the handle_reply(). | |
97 | */ | |
98 | if (ceph_snap(dir) != CEPH_SNAPDIR) { | |
99 | err = ceph_fscrypt_prepare_context(dir, inode, as_ctx); | |
100 | if (err) | |
101 | goto out_err; | |
102 | } | |
6b5717bd | 103 | |
ec9595c0 JL |
104 | return inode; |
105 | out_err: | |
106 | iput(inode); | |
107 | return ERR_PTR(err); | |
108 | } | |
109 | ||
110 | void ceph_as_ctx_to_req(struct ceph_mds_request *req, | |
111 | struct ceph_acl_sec_ctx *as_ctx) | |
112 | { | |
113 | if (as_ctx->pagelist) { | |
114 | req->r_pagelist = as_ctx->pagelist; | |
115 | as_ctx->pagelist = NULL; | |
116 | } | |
6b5717bd | 117 | ceph_fscrypt_as_ctx_to_req(req, as_ctx); |
ec9595c0 JL |
118 | } |
119 | ||
120 | /** | |
121 | * ceph_get_inode - find or create/hash a new inode | |
122 | * @sb: superblock to search and allocate in | |
123 | * @vino: vino to search for | |
124 | * @newino: optional new inode to insert if one isn't found (may be NULL) | |
125 | * | |
126 | * Search for or insert a new inode into the hash for the given vino, and | |
127 | * return a reference to it. If new is non-NULL, its reference is consumed. | |
128 | */ | |
129 | struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino, | |
130 | struct inode *newino) | |
355da1eb | 131 | { |
38d46409 XL |
132 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb); |
133 | struct ceph_client *cl = mdsc->fsc->client; | |
355da1eb | 134 | struct inode *inode; |
355da1eb | 135 | |
d4f6b31d JL |
136 | if (ceph_vino_is_reserved(vino)) |
137 | return ERR_PTR(-EREMOTEIO); | |
138 | ||
ec9595c0 JL |
139 | if (newino) { |
140 | inode = inode_insert5(newino, (unsigned long)vino.ino, | |
141 | ceph_ino_compare, ceph_set_ino_cb, &vino); | |
142 | if (inode != newino) | |
143 | iput(newino); | |
144 | } else { | |
145 | inode = iget5_locked(sb, (unsigned long)vino.ino, | |
146 | ceph_ino_compare, ceph_set_ino_cb, &vino); | |
147 | } | |
148 | ||
149 | if (!inode) { | |
38d46409 | 150 | doutc(cl, "no inode found for %llx.%llx\n", vino.ino, vino.snap); |
355da1eb | 151 | return ERR_PTR(-ENOMEM); |
ec9595c0 | 152 | } |
355da1eb | 153 | |
38d46409 XL |
154 | doutc(cl, "on %llx=%llx.%llx got %p new %d\n", |
155 | ceph_present_inode(inode), ceph_vinop(inode), inode, | |
156 | !!(inode->i_state & I_NEW)); | |
355da1eb SW |
157 | return inode; |
158 | } | |
159 | ||
160 | /* | |
161 | * get/constuct snapdir inode for a given directory | |
162 | */ | |
163 | struct inode *ceph_get_snapdir(struct inode *parent) | |
164 | { | |
38d46409 | 165 | struct ceph_client *cl = ceph_inode_to_client(parent); |
355da1eb SW |
166 | struct ceph_vino vino = { |
167 | .ino = ceph_ino(parent), | |
168 | .snap = CEPH_SNAPDIR, | |
169 | }; | |
ec9595c0 | 170 | struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL); |
b377ff13 | 171 | struct ceph_inode_info *ci = ceph_inode(inode); |
dd66df00 | 172 | int ret = -ENOTDIR; |
355da1eb | 173 | |
355da1eb | 174 | if (IS_ERR(inode)) |
7e34bc52 | 175 | return inode; |
3e10a15f JL |
176 | |
177 | if (!S_ISDIR(parent->i_mode)) { | |
38d46409 XL |
178 | pr_warn_once_client(cl, "bad snapdir parent type (mode=0%o)\n", |
179 | parent->i_mode); | |
322794d3 | 180 | goto err; |
3e10a15f JL |
181 | } |
182 | ||
183 | if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) { | |
38d46409 XL |
184 | pr_warn_once_client(cl, "bad snapdir inode type (mode=0%o)\n", |
185 | inode->i_mode); | |
322794d3 | 186 | goto err; |
3e10a15f JL |
187 | } |
188 | ||
355da1eb SW |
189 | inode->i_mode = parent->i_mode; |
190 | inode->i_uid = parent->i_uid; | |
191 | inode->i_gid = parent->i_gid; | |
c453bdb5 | 192 | inode_set_mtime_to_ts(inode, inode_get_mtime(parent)); |
7795aef0 | 193 | inode_set_ctime_to_ts(inode, inode_get_ctime(parent)); |
c453bdb5 | 194 | inode_set_atime_to_ts(inode, inode_get_atime(parent)); |
b377ff13 | 195 | ci->i_rbytes = 0; |
ef915725 | 196 | ci->i_btime = ceph_inode(parent)->i_btime; |
893e456b | 197 | |
dd66df00 LH |
198 | #ifdef CONFIG_FS_ENCRYPTION |
199 | /* if encrypted, just borrow fscrypt_auth from parent */ | |
200 | if (IS_ENCRYPTED(parent)) { | |
201 | struct ceph_inode_info *pci = ceph_inode(parent); | |
202 | ||
203 | ci->fscrypt_auth = kmemdup(pci->fscrypt_auth, | |
204 | pci->fscrypt_auth_len, | |
205 | GFP_KERNEL); | |
206 | if (ci->fscrypt_auth) { | |
207 | inode->i_flags |= S_ENCRYPTED; | |
208 | ci->fscrypt_auth_len = pci->fscrypt_auth_len; | |
209 | } else { | |
38d46409 | 210 | doutc(cl, "Failed to alloc snapdir fscrypt_auth\n"); |
dd66df00 LH |
211 | ret = -ENOMEM; |
212 | goto err; | |
213 | } | |
214 | } | |
215 | #endif | |
d3c51ae1 JL |
216 | if (inode->i_state & I_NEW) { |
217 | inode->i_op = &ceph_snapdir_iops; | |
218 | inode->i_fop = &ceph_snapdir_fops; | |
219 | ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ | |
893e456b | 220 | unlock_new_inode(inode); |
d3c51ae1 | 221 | } |
893e456b | 222 | |
355da1eb | 223 | return inode; |
322794d3 XL |
224 | err: |
225 | if ((inode->i_state & I_NEW)) | |
226 | discard_new_inode(inode); | |
227 | else | |
228 | iput(inode); | |
dd66df00 | 229 | return ERR_PTR(ret); |
355da1eb SW |
230 | } |
231 | ||
232 | const struct inode_operations ceph_file_iops = { | |
233 | .permission = ceph_permission, | |
234 | .setattr = ceph_setattr, | |
235 | .getattr = ceph_getattr, | |
355da1eb | 236 | .listxattr = ceph_listxattr, |
cac2f8b8 | 237 | .get_inode_acl = ceph_get_acl, |
72466d0b | 238 | .set_acl = ceph_set_acl, |
355da1eb SW |
239 | }; |
240 | ||
241 | ||
242 | /* | |
243 | * We use a 'frag tree' to keep track of the MDS's directory fragments | |
244 | * for a given inode (usually there is just a single fragment). We | |
245 | * need to know when a child frag is delegated to a new MDS, or when | |
246 | * it is flagged as replicated, so we can direct our requests | |
247 | * accordingly. | |
248 | */ | |
249 | ||
250 | /* | |
251 | * find/create a frag in the tree | |
252 | */ | |
253 | static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, | |
254 | u32 f) | |
255 | { | |
38d46409 XL |
256 | struct inode *inode = &ci->netfs.inode; |
257 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
355da1eb SW |
258 | struct rb_node **p; |
259 | struct rb_node *parent = NULL; | |
260 | struct ceph_inode_frag *frag; | |
261 | int c; | |
262 | ||
263 | p = &ci->i_fragtree.rb_node; | |
264 | while (*p) { | |
265 | parent = *p; | |
266 | frag = rb_entry(parent, struct ceph_inode_frag, node); | |
267 | c = ceph_frag_compare(f, frag->frag); | |
268 | if (c < 0) | |
269 | p = &(*p)->rb_left; | |
270 | else if (c > 0) | |
271 | p = &(*p)->rb_right; | |
272 | else | |
273 | return frag; | |
274 | } | |
275 | ||
276 | frag = kmalloc(sizeof(*frag), GFP_NOFS); | |
51308806 | 277 | if (!frag) |
355da1eb | 278 | return ERR_PTR(-ENOMEM); |
51308806 | 279 | |
355da1eb SW |
280 | frag->frag = f; |
281 | frag->split_by = 0; | |
282 | frag->mds = -1; | |
283 | frag->ndist = 0; | |
284 | ||
285 | rb_link_node(&frag->node, parent, p); | |
286 | rb_insert_color(&frag->node, &ci->i_fragtree); | |
287 | ||
38d46409 | 288 | doutc(cl, "added %p %llx.%llx frag %x\n", inode, ceph_vinop(inode), f); |
355da1eb SW |
289 | return frag; |
290 | } | |
291 | ||
292 | /* | |
293 | * find a specific frag @f | |
294 | */ | |
295 | struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) | |
296 | { | |
297 | struct rb_node *n = ci->i_fragtree.rb_node; | |
298 | ||
299 | while (n) { | |
300 | struct ceph_inode_frag *frag = | |
301 | rb_entry(n, struct ceph_inode_frag, node); | |
302 | int c = ceph_frag_compare(f, frag->frag); | |
303 | if (c < 0) | |
304 | n = n->rb_left; | |
305 | else if (c > 0) | |
306 | n = n->rb_right; | |
307 | else | |
308 | return frag; | |
309 | } | |
310 | return NULL; | |
311 | } | |
312 | ||
313 | /* | |
314 | * Choose frag containing the given value @v. If @pfrag is | |
315 | * specified, copy the frag delegation info to the caller if | |
316 | * it is present. | |
317 | */ | |
3e7fbe9c YZ |
318 | static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
319 | struct ceph_inode_frag *pfrag, int *found) | |
355da1eb | 320 | { |
38d46409 | 321 | struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode); |
355da1eb SW |
322 | u32 t = ceph_frag_make(0, 0); |
323 | struct ceph_inode_frag *frag; | |
324 | unsigned nway, i; | |
325 | u32 n; | |
326 | ||
327 | if (found) | |
328 | *found = 0; | |
329 | ||
355da1eb SW |
330 | while (1) { |
331 | WARN_ON(!ceph_frag_contains_value(t, v)); | |
332 | frag = __ceph_find_frag(ci, t); | |
333 | if (!frag) | |
334 | break; /* t is a leaf */ | |
335 | if (frag->split_by == 0) { | |
336 | if (pfrag) | |
337 | memcpy(pfrag, frag, sizeof(*pfrag)); | |
338 | if (found) | |
339 | *found = 1; | |
340 | break; | |
341 | } | |
342 | ||
343 | /* choose child */ | |
344 | nway = 1 << frag->split_by; | |
38d46409 XL |
345 | doutc(cl, "frag(%x) %x splits by %d (%d ways)\n", v, t, |
346 | frag->split_by, nway); | |
355da1eb SW |
347 | for (i = 0; i < nway; i++) { |
348 | n = ceph_frag_make_child(t, frag->split_by, i); | |
349 | if (ceph_frag_contains_value(n, v)) { | |
350 | t = n; | |
351 | break; | |
352 | } | |
353 | } | |
354 | BUG_ON(i == nway); | |
355 | } | |
38d46409 | 356 | doutc(cl, "frag(%x) = %x\n", v, t); |
355da1eb | 357 | |
355da1eb SW |
358 | return t; |
359 | } | |
360 | ||
3e7fbe9c YZ |
361 | u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
362 | struct ceph_inode_frag *pfrag, int *found) | |
363 | { | |
364 | u32 ret; | |
365 | mutex_lock(&ci->i_fragtree_mutex); | |
366 | ret = __ceph_choose_frag(ci, v, pfrag, found); | |
367 | mutex_unlock(&ci->i_fragtree_mutex); | |
368 | return ret; | |
369 | } | |
370 | ||
355da1eb SW |
371 | /* |
372 | * Process dirfrag (delegation) info from the mds. Include leaf | |
373 | * fragment in tree ONLY if ndist > 0. Otherwise, only | |
374 | * branches/splits are included in i_fragtree) | |
375 | */ | |
376 | static int ceph_fill_dirfrag(struct inode *inode, | |
377 | struct ceph_mds_reply_dirfrag *dirinfo) | |
378 | { | |
379 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 380 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb SW |
381 | struct ceph_inode_frag *frag; |
382 | u32 id = le32_to_cpu(dirinfo->frag); | |
383 | int mds = le32_to_cpu(dirinfo->auth); | |
384 | int ndist = le32_to_cpu(dirinfo->ndist); | |
8d08503c | 385 | int diri_auth = -1; |
355da1eb SW |
386 | int i; |
387 | int err = 0; | |
388 | ||
8d08503c YZ |
389 | spin_lock(&ci->i_ceph_lock); |
390 | if (ci->i_auth_cap) | |
391 | diri_auth = ci->i_auth_cap->mds; | |
392 | spin_unlock(&ci->i_ceph_lock); | |
393 | ||
42172119 YZ |
394 | if (mds == -1) /* CDIR_AUTH_PARENT */ |
395 | mds = diri_auth; | |
396 | ||
355da1eb | 397 | mutex_lock(&ci->i_fragtree_mutex); |
8d08503c | 398 | if (ndist == 0 && mds == diri_auth) { |
355da1eb SW |
399 | /* no delegation info needed. */ |
400 | frag = __ceph_find_frag(ci, id); | |
401 | if (!frag) | |
402 | goto out; | |
403 | if (frag->split_by == 0) { | |
404 | /* tree leaf, remove */ | |
38d46409 XL |
405 | doutc(cl, "removed %p %llx.%llx frag %x (no ref)\n", |
406 | inode, ceph_vinop(inode), id); | |
355da1eb SW |
407 | rb_erase(&frag->node, &ci->i_fragtree); |
408 | kfree(frag); | |
409 | } else { | |
410 | /* tree branch, keep and clear */ | |
38d46409 XL |
411 | doutc(cl, "cleared %p %llx.%llx frag %x referral\n", |
412 | inode, ceph_vinop(inode), id); | |
355da1eb SW |
413 | frag->mds = -1; |
414 | frag->ndist = 0; | |
415 | } | |
416 | goto out; | |
417 | } | |
418 | ||
419 | ||
420 | /* find/add this frag to store mds delegation info */ | |
421 | frag = __get_or_create_frag(ci, id); | |
422 | if (IS_ERR(frag)) { | |
423 | /* this is not the end of the world; we can continue | |
424 | with bad/inaccurate delegation info */ | |
38d46409 XL |
425 | pr_err_client(cl, "ENOMEM on mds ref %p %llx.%llx fg %x\n", |
426 | inode, ceph_vinop(inode), | |
427 | le32_to_cpu(dirinfo->frag)); | |
355da1eb SW |
428 | err = -ENOMEM; |
429 | goto out; | |
430 | } | |
431 | ||
432 | frag->mds = mds; | |
433 | frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); | |
434 | for (i = 0; i < frag->ndist; i++) | |
435 | frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); | |
38d46409 XL |
436 | doutc(cl, "%p %llx.%llx frag %x ndist=%d\n", inode, |
437 | ceph_vinop(inode), frag->frag, frag->ndist); | |
355da1eb SW |
438 | |
439 | out: | |
440 | mutex_unlock(&ci->i_fragtree_mutex); | |
441 | return err; | |
442 | } | |
443 | ||
a407846e YZ |
444 | static int frag_tree_split_cmp(const void *l, const void *r) |
445 | { | |
446 | struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l; | |
447 | struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r; | |
fe2ed425 JL |
448 | return ceph_frag_compare(le32_to_cpu(ls->frag), |
449 | le32_to_cpu(rs->frag)); | |
a407846e YZ |
450 | } |
451 | ||
a4b7431f YZ |
452 | static bool is_frag_child(u32 f, struct ceph_inode_frag *frag) |
453 | { | |
454 | if (!frag) | |
455 | return f == ceph_frag_make(0, 0); | |
456 | if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by) | |
457 | return false; | |
458 | return ceph_frag_contains_value(frag->frag, ceph_frag_value(f)); | |
459 | } | |
460 | ||
3e7fbe9c YZ |
461 | static int ceph_fill_fragtree(struct inode *inode, |
462 | struct ceph_frag_tree_head *fragtree, | |
463 | struct ceph_mds_reply_dirfrag *dirinfo) | |
464 | { | |
38d46409 | 465 | struct ceph_client *cl = ceph_inode_to_client(inode); |
3e7fbe9c | 466 | struct ceph_inode_info *ci = ceph_inode(inode); |
a4b7431f | 467 | struct ceph_inode_frag *frag, *prev_frag = NULL; |
3e7fbe9c | 468 | struct rb_node *rb_node; |
1b1bc16d YZ |
469 | unsigned i, split_by, nsplits; |
470 | u32 id; | |
3e7fbe9c YZ |
471 | bool update = false; |
472 | ||
473 | mutex_lock(&ci->i_fragtree_mutex); | |
474 | nsplits = le32_to_cpu(fragtree->nsplits); | |
1b1bc16d YZ |
475 | if (nsplits != ci->i_fragtree_nsplits) { |
476 | update = true; | |
477 | } else if (nsplits) { | |
8032bf12 | 478 | i = get_random_u32_below(nsplits); |
3e7fbe9c YZ |
479 | id = le32_to_cpu(fragtree->splits[i].frag); |
480 | if (!__ceph_find_frag(ci, id)) | |
481 | update = true; | |
482 | } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) { | |
483 | rb_node = rb_first(&ci->i_fragtree); | |
484 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
485 | if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node)) | |
486 | update = true; | |
487 | } | |
488 | if (!update && dirinfo) { | |
489 | id = le32_to_cpu(dirinfo->frag); | |
490 | if (id != __ceph_choose_frag(ci, id, NULL, NULL)) | |
491 | update = true; | |
492 | } | |
493 | if (!update) | |
494 | goto out_unlock; | |
495 | ||
a407846e YZ |
496 | if (nsplits > 1) { |
497 | sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]), | |
498 | frag_tree_split_cmp, NULL); | |
499 | } | |
500 | ||
38d46409 | 501 | doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); |
3e7fbe9c YZ |
502 | rb_node = rb_first(&ci->i_fragtree); |
503 | for (i = 0; i < nsplits; i++) { | |
504 | id = le32_to_cpu(fragtree->splits[i].frag); | |
1b1bc16d YZ |
505 | split_by = le32_to_cpu(fragtree->splits[i].by); |
506 | if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) { | |
38d46409 XL |
507 | pr_err_client(cl, "%p %llx.%llx invalid split %d/%u, " |
508 | "frag %x split by %d\n", inode, | |
509 | ceph_vinop(inode), i, nsplits, id, split_by); | |
1b1bc16d YZ |
510 | continue; |
511 | } | |
3e7fbe9c YZ |
512 | frag = NULL; |
513 | while (rb_node) { | |
514 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
515 | if (ceph_frag_compare(frag->frag, id) >= 0) { | |
516 | if (frag->frag != id) | |
517 | frag = NULL; | |
518 | else | |
519 | rb_node = rb_next(rb_node); | |
520 | break; | |
521 | } | |
522 | rb_node = rb_next(rb_node); | |
a4b7431f YZ |
523 | /* delete stale split/leaf node */ |
524 | if (frag->split_by > 0 || | |
525 | !is_frag_child(frag->frag, prev_frag)) { | |
526 | rb_erase(&frag->node, &ci->i_fragtree); | |
1b1bc16d YZ |
527 | if (frag->split_by > 0) |
528 | ci->i_fragtree_nsplits--; | |
a4b7431f YZ |
529 | kfree(frag); |
530 | } | |
3e7fbe9c YZ |
531 | frag = NULL; |
532 | } | |
533 | if (!frag) { | |
534 | frag = __get_or_create_frag(ci, id); | |
535 | if (IS_ERR(frag)) | |
536 | continue; | |
537 | } | |
1b1bc16d YZ |
538 | if (frag->split_by == 0) |
539 | ci->i_fragtree_nsplits++; | |
540 | frag->split_by = split_by; | |
38d46409 | 541 | doutc(cl, " frag %x split by %d\n", frag->frag, frag->split_by); |
a4b7431f | 542 | prev_frag = frag; |
3e7fbe9c YZ |
543 | } |
544 | while (rb_node) { | |
545 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
546 | rb_node = rb_next(rb_node); | |
a4b7431f YZ |
547 | /* delete stale split/leaf node */ |
548 | if (frag->split_by > 0 || | |
549 | !is_frag_child(frag->frag, prev_frag)) { | |
550 | rb_erase(&frag->node, &ci->i_fragtree); | |
1b1bc16d YZ |
551 | if (frag->split_by > 0) |
552 | ci->i_fragtree_nsplits--; | |
a4b7431f YZ |
553 | kfree(frag); |
554 | } | |
3e7fbe9c YZ |
555 | } |
556 | out_unlock: | |
557 | mutex_unlock(&ci->i_fragtree_mutex); | |
558 | return 0; | |
559 | } | |
355da1eb SW |
560 | |
561 | /* | |
562 | * initialize a newly allocated inode. | |
563 | */ | |
564 | struct inode *ceph_alloc_inode(struct super_block *sb) | |
565 | { | |
38d46409 | 566 | struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb); |
355da1eb SW |
567 | struct ceph_inode_info *ci; |
568 | int i; | |
569 | ||
fd60b288 | 570 | ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS); |
355da1eb SW |
571 | if (!ci) |
572 | return NULL; | |
573 | ||
38d46409 | 574 | doutc(fsc->client, "%p\n", &ci->netfs.inode); |
355da1eb | 575 | |
bc899ee1 | 576 | /* Set parameters for the netfs library */ |
100ccd18 | 577 | netfs_inode_init(&ci->netfs, &ceph_netfs_ops, false); |
bc899ee1 | 578 | |
be655596 SW |
579 | spin_lock_init(&ci->i_ceph_lock); |
580 | ||
355da1eb | 581 | ci->i_version = 0; |
31c542a1 | 582 | ci->i_inline_version = 0; |
355da1eb SW |
583 | ci->i_time_warp_seq = 0; |
584 | ci->i_ceph_flags = 0; | |
fdd4e158 YZ |
585 | atomic64_set(&ci->i_ordered_count, 1); |
586 | atomic64_set(&ci->i_release_count, 1); | |
587 | atomic64_set(&ci->i_complete_seq[0], 0); | |
588 | atomic64_set(&ci->i_complete_seq[1], 0); | |
355da1eb SW |
589 | ci->i_symlink = NULL; |
590 | ||
fb18a575 LH |
591 | ci->i_max_bytes = 0; |
592 | ci->i_max_files = 0; | |
593 | ||
6c0f3af7 | 594 | memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout)); |
785892fe | 595 | memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); |
30c156d9 | 596 | RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL); |
6c0f3af7 | 597 | |
355da1eb SW |
598 | ci->i_fragtree = RB_ROOT; |
599 | mutex_init(&ci->i_fragtree_mutex); | |
600 | ||
601 | ci->i_xattrs.blob = NULL; | |
602 | ci->i_xattrs.prealloc_blob = NULL; | |
603 | ci->i_xattrs.dirty = false; | |
604 | ci->i_xattrs.index = RB_ROOT; | |
605 | ci->i_xattrs.count = 0; | |
606 | ci->i_xattrs.names_size = 0; | |
607 | ci->i_xattrs.vals_size = 0; | |
608 | ci->i_xattrs.version = 0; | |
609 | ci->i_xattrs.index_version = 0; | |
610 | ||
611 | ci->i_caps = RB_ROOT; | |
612 | ci->i_auth_cap = NULL; | |
613 | ci->i_dirty_caps = 0; | |
614 | ci->i_flushing_caps = 0; | |
615 | INIT_LIST_HEAD(&ci->i_dirty_item); | |
616 | INIT_LIST_HEAD(&ci->i_flushing_item); | |
f66fd9f0 | 617 | ci->i_prealloc_cap_flush = NULL; |
e4500b5e | 618 | INIT_LIST_HEAD(&ci->i_cap_flush_list); |
355da1eb | 619 | init_waitqueue_head(&ci->i_cap_wq); |
355da1eb SW |
620 | ci->i_hold_caps_max = 0; |
621 | INIT_LIST_HEAD(&ci->i_cap_delay_list); | |
355da1eb SW |
622 | INIT_LIST_HEAD(&ci->i_cap_snaps); |
623 | ci->i_head_snapc = NULL; | |
624 | ci->i_snap_caps = 0; | |
625 | ||
719a2514 | 626 | ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ; |
774a6a11 | 627 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) |
355da1eb SW |
628 | ci->i_nr_by_mode[i] = 0; |
629 | ||
b0d7c223 | 630 | mutex_init(&ci->i_truncate_mutex); |
355da1eb SW |
631 | ci->i_truncate_seq = 0; |
632 | ci->i_truncate_size = 0; | |
633 | ci->i_truncate_pending = 0; | |
5c64737d | 634 | ci->i_truncate_pagecache_size = 0; |
355da1eb SW |
635 | |
636 | ci->i_max_size = 0; | |
637 | ci->i_reported_size = 0; | |
638 | ci->i_wanted_max_size = 0; | |
639 | ci->i_requested_max_size = 0; | |
640 | ||
641 | ci->i_pin_ref = 0; | |
642 | ci->i_rd_ref = 0; | |
643 | ci->i_rdcache_ref = 0; | |
644 | ci->i_wr_ref = 0; | |
d3d0720d | 645 | ci->i_wb_ref = 0; |
f85122af | 646 | ci->i_fx_ref = 0; |
355da1eb SW |
647 | ci->i_wrbuffer_ref = 0; |
648 | ci->i_wrbuffer_ref_head = 0; | |
89aa5930 | 649 | atomic_set(&ci->i_filelock_ref, 0); |
1e9c2eb6 | 650 | atomic_set(&ci->i_shared_gen, 1); |
355da1eb SW |
651 | ci->i_rdcache_gen = 0; |
652 | ci->i_rdcache_revoking = 0; | |
653 | ||
355da1eb | 654 | INIT_LIST_HEAD(&ci->i_unsafe_dirops); |
68cd5b4b | 655 | INIT_LIST_HEAD(&ci->i_unsafe_iops); |
355da1eb SW |
656 | spin_lock_init(&ci->i_unsafe_lock); |
657 | ||
658 | ci->i_snap_realm = NULL; | |
659 | INIT_LIST_HEAD(&ci->i_snap_realm_item); | |
660 | INIT_LIST_HEAD(&ci->i_snap_flush_item); | |
661 | ||
1cf89a8d YZ |
662 | INIT_WORK(&ci->i_work, ceph_inode_work); |
663 | ci->i_work_mask = 0; | |
245ce991 | 664 | memset(&ci->i_btime, '\0', sizeof(ci->i_btime)); |
2d332d5b JL |
665 | #ifdef CONFIG_FS_ENCRYPTION |
666 | ci->fscrypt_auth = NULL; | |
667 | ci->fscrypt_auth_len = 0; | |
668 | #endif | |
874c8ca1 | 669 | return &ci->netfs.inode; |
355da1eb SW |
670 | } |
671 | ||
cfa6d412 | 672 | void ceph_free_inode(struct inode *inode) |
fa0d7e3d | 673 | { |
fa0d7e3d NP |
674 | struct ceph_inode_info *ci = ceph_inode(inode); |
675 | ||
daf5cc27 | 676 | kfree(ci->i_symlink); |
2d332d5b JL |
677 | #ifdef CONFIG_FS_ENCRYPTION |
678 | kfree(ci->fscrypt_auth); | |
679 | #endif | |
79f2f6ad | 680 | fscrypt_free_inode(inode); |
fa0d7e3d NP |
681 | kmem_cache_free(ceph_inode_cachep, ci); |
682 | } | |
683 | ||
87bc5b89 | 684 | void ceph_evict_inode(struct inode *inode) |
355da1eb SW |
685 | { |
686 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1dd8d470 | 687 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
38d46409 | 688 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb SW |
689 | struct ceph_inode_frag *frag; |
690 | struct rb_node *n; | |
691 | ||
38d46409 | 692 | doutc(cl, "%p ino %llx.%llx\n", inode, ceph_vinop(inode)); |
87bc5b89 | 693 | |
1dd8d470 XL |
694 | percpu_counter_dec(&mdsc->metric.total_inodes); |
695 | ||
87bc5b89 | 696 | truncate_inode_pages_final(&inode->i_data); |
c9c4ff12 | 697 | if (inode->i_state & I_PINNING_NETFS_WB) |
400e1286 | 698 | ceph_fscache_unuse_cookie(inode, true); |
87bc5b89 | 699 | clear_inode(inode); |
355da1eb | 700 | |
99ccbd22 | 701 | ceph_fscache_unregister_inode_cookie(ci); |
2d332d5b | 702 | fscrypt_put_encryption_info(inode); |
99ccbd22 | 703 | |
d6e47819 | 704 | __ceph_remove_caps(ci); |
355da1eb | 705 | |
55ab5520 | 706 | if (__ceph_has_quota(ci, QUOTA_GET_ANY)) |
d557c48d LH |
707 | ceph_adjust_quota_realms_count(inode, false); |
708 | ||
8b218b8a SW |
709 | /* |
710 | * we may still have a snap_realm reference if there are stray | |
d9df2783 | 711 | * caps in i_snap_caps. |
8b218b8a SW |
712 | */ |
713 | if (ci->i_snap_realm) { | |
75c9627e | 714 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
38d46409 XL |
715 | doutc(cl, " dropping residual ref to snap realm %p\n", |
716 | ci->i_snap_realm); | |
0ba92e1c | 717 | ceph_change_snap_realm(inode, NULL); |
75c9627e YZ |
718 | } else { |
719 | ceph_put_snapid_map(mdsc, ci->i_snapid_map); | |
720 | ci->i_snap_realm = NULL; | |
721 | } | |
8b218b8a SW |
722 | } |
723 | ||
355da1eb SW |
724 | while ((n = rb_first(&ci->i_fragtree)) != NULL) { |
725 | frag = rb_entry(n, struct ceph_inode_frag, node); | |
726 | rb_erase(n, &ci->i_fragtree); | |
727 | kfree(frag); | |
728 | } | |
1b1bc16d | 729 | ci->i_fragtree_nsplits = 0; |
355da1eb SW |
730 | |
731 | __ceph_destroy_xattrs(ci); | |
b6c1d5b8 SW |
732 | if (ci->i_xattrs.blob) |
733 | ceph_buffer_put(ci->i_xattrs.blob); | |
734 | if (ci->i_xattrs.prealloc_blob) | |
735 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
355da1eb | 736 | |
779fe0fb | 737 | ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns)); |
785892fe | 738 | ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); |
355da1eb SW |
739 | } |
740 | ||
224a7542 YZ |
741 | static inline blkcnt_t calc_inode_blocks(u64 size) |
742 | { | |
743 | return (size + (1<<9) - 1) >> 9; | |
744 | } | |
745 | ||
355da1eb SW |
746 | /* |
747 | * Helpers to fill in size, ctime, mtime, and atime. We have to be | |
748 | * careful because either the client or MDS may have more up to date | |
749 | * info, depending on which capabilities are held, and whether | |
750 | * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime | |
751 | * and size are monotonically increasing, except when utimes() or | |
752 | * truncate() increments the corresponding _seq values.) | |
753 | */ | |
754 | int ceph_fill_file_size(struct inode *inode, int issued, | |
755 | u32 truncate_seq, u64 truncate_size, u64 size) | |
756 | { | |
38d46409 | 757 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb SW |
758 | struct ceph_inode_info *ci = ceph_inode(inode); |
759 | int queue_trunc = 0; | |
2d6795fb | 760 | loff_t isize = i_size_read(inode); |
355da1eb SW |
761 | |
762 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || | |
2d6795fb | 763 | (truncate_seq == ci->i_truncate_seq && size > isize)) { |
38d46409 | 764 | doutc(cl, "size %lld -> %llu\n", isize, size); |
a3d714c3 | 765 | if (size > 0 && S_ISDIR(inode->i_mode)) { |
38d46409 | 766 | pr_err_client(cl, "non-zero size for directory\n"); |
a3d714c3 YZ |
767 | size = 0; |
768 | } | |
99c88e69 | 769 | i_size_write(inode, size); |
224a7542 | 770 | inode->i_blocks = calc_inode_blocks(size); |
400e1286 JL |
771 | /* |
772 | * If we're expanding, then we should be able to just update | |
773 | * the existing cookie. | |
774 | */ | |
775 | if (size > isize) | |
776 | ceph_fscache_update(inode); | |
355da1eb SW |
777 | ci->i_reported_size = size; |
778 | if (truncate_seq != ci->i_truncate_seq) { | |
38d46409 XL |
779 | doutc(cl, "truncate_seq %u -> %u\n", |
780 | ci->i_truncate_seq, truncate_seq); | |
355da1eb | 781 | ci->i_truncate_seq = truncate_seq; |
b0d7c223 YZ |
782 | |
783 | /* the MDS should have revoked these caps */ | |
15c0a870 | 784 | WARN_ON_ONCE(issued & (CEPH_CAP_FILE_RD | |
b0d7c223 | 785 | CEPH_CAP_FILE_LAZYIO)); |
3d497d85 YS |
786 | /* |
787 | * If we hold relevant caps, or in the case where we're | |
788 | * not the only client referencing this file and we | |
789 | * don't hold those caps, then we need to check whether | |
790 | * the file is either opened or mmaped | |
791 | */ | |
b0d7c223 YZ |
792 | if ((issued & (CEPH_CAP_FILE_CACHE| |
793 | CEPH_CAP_FILE_BUFFER)) || | |
3d497d85 | 794 | mapping_mapped(inode->i_mapping) || |
719a2514 | 795 | __ceph_is_file_opened(ci)) { |
355da1eb SW |
796 | ci->i_truncate_pending++; |
797 | queue_trunc = 1; | |
798 | } | |
799 | } | |
800 | } | |
295fc4aa XL |
801 | |
802 | /* | |
803 | * It's possible that the new sizes of the two consecutive | |
804 | * size truncations will be in the same fscrypt last block, | |
805 | * and we need to truncate the corresponding page caches | |
806 | * anyway. | |
807 | */ | |
808 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) { | |
38d46409 XL |
809 | doutc(cl, "truncate_size %lld -> %llu, encrypted %d\n", |
810 | ci->i_truncate_size, truncate_size, | |
811 | !!IS_ENCRYPTED(inode)); | |
295fc4aa | 812 | |
355da1eb | 813 | ci->i_truncate_size = truncate_size; |
295fc4aa XL |
814 | |
815 | if (IS_ENCRYPTED(inode)) { | |
38d46409 XL |
816 | doutc(cl, "truncate_pagecache_size %lld -> %llu\n", |
817 | ci->i_truncate_pagecache_size, size); | |
5c64737d | 818 | ci->i_truncate_pagecache_size = size; |
295fc4aa | 819 | } else { |
5c64737d | 820 | ci->i_truncate_pagecache_size = truncate_size; |
295fc4aa | 821 | } |
355da1eb SW |
822 | } |
823 | return queue_trunc; | |
824 | } | |
825 | ||
826 | void ceph_fill_file_time(struct inode *inode, int issued, | |
9bbeab41 AB |
827 | u64 time_warp_seq, struct timespec64 *ctime, |
828 | struct timespec64 *mtime, struct timespec64 *atime) | |
355da1eb | 829 | { |
38d46409 | 830 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb | 831 | struct ceph_inode_info *ci = ceph_inode(inode); |
7795aef0 | 832 | struct timespec64 ictime = inode_get_ctime(inode); |
355da1eb SW |
833 | int warn = 0; |
834 | ||
835 | if (issued & (CEPH_CAP_FILE_EXCL| | |
836 | CEPH_CAP_FILE_WR| | |
d8672d64 SW |
837 | CEPH_CAP_FILE_BUFFER| |
838 | CEPH_CAP_AUTH_EXCL| | |
839 | CEPH_CAP_XATTR_EXCL)) { | |
ffdeec7a | 840 | if (ci->i_version == 0 || |
7795aef0 | 841 | timespec64_compare(ctime, &ictime) > 0) { |
38d46409 | 842 | doutc(cl, "ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n", |
7795aef0 | 843 | ictime.tv_sec, ictime.tv_nsec, |
9bbeab41 | 844 | ctime->tv_sec, ctime->tv_nsec); |
7795aef0 | 845 | inode_set_ctime_to_ts(inode, *ctime); |
355da1eb | 846 | } |
ffdeec7a YZ |
847 | if (ci->i_version == 0 || |
848 | ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { | |
355da1eb | 849 | /* the MDS did a utimes() */ |
38d46409 | 850 | doutc(cl, "mtime %lld.%09ld -> %lld.%09ld tw %d -> %d\n", |
c453bdb5 JL |
851 | inode_get_mtime_sec(inode), |
852 | inode_get_mtime_nsec(inode), | |
9bbeab41 | 853 | mtime->tv_sec, mtime->tv_nsec, |
355da1eb SW |
854 | ci->i_time_warp_seq, (int)time_warp_seq); |
855 | ||
c453bdb5 JL |
856 | inode_set_mtime_to_ts(inode, *mtime); |
857 | inode_set_atime_to_ts(inode, *atime); | |
355da1eb SW |
858 | ci->i_time_warp_seq = time_warp_seq; |
859 | } else if (time_warp_seq == ci->i_time_warp_seq) { | |
c453bdb5 JL |
860 | struct timespec64 ts; |
861 | ||
355da1eb | 862 | /* nobody did utimes(); take the max */ |
c453bdb5 JL |
863 | ts = inode_get_mtime(inode); |
864 | if (timespec64_compare(mtime, &ts) > 0) { | |
38d46409 | 865 | doutc(cl, "mtime %lld.%09ld -> %lld.%09ld inc\n", |
c453bdb5 | 866 | ts.tv_sec, ts.tv_nsec, |
9bbeab41 | 867 | mtime->tv_sec, mtime->tv_nsec); |
c453bdb5 | 868 | inode_set_mtime_to_ts(inode, *mtime); |
355da1eb | 869 | } |
c453bdb5 JL |
870 | ts = inode_get_atime(inode); |
871 | if (timespec64_compare(atime, &ts) > 0) { | |
38d46409 | 872 | doutc(cl, "atime %lld.%09ld -> %lld.%09ld inc\n", |
c453bdb5 | 873 | ts.tv_sec, ts.tv_nsec, |
9bbeab41 | 874 | atime->tv_sec, atime->tv_nsec); |
c453bdb5 | 875 | inode_set_atime_to_ts(inode, *atime); |
355da1eb SW |
876 | } |
877 | } else if (issued & CEPH_CAP_FILE_EXCL) { | |
878 | /* we did a utimes(); ignore mds values */ | |
879 | } else { | |
880 | warn = 1; | |
881 | } | |
882 | } else { | |
d8672d64 | 883 | /* we have no write|excl caps; whatever the MDS says is true */ |
355da1eb | 884 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { |
7795aef0 | 885 | inode_set_ctime_to_ts(inode, *ctime); |
c453bdb5 JL |
886 | inode_set_mtime_to_ts(inode, *mtime); |
887 | inode_set_atime_to_ts(inode, *atime); | |
355da1eb SW |
888 | ci->i_time_warp_seq = time_warp_seq; |
889 | } else { | |
890 | warn = 1; | |
891 | } | |
892 | } | |
893 | if (warn) /* time_warp_seq shouldn't go backwards */ | |
38d46409 XL |
894 | doutc(cl, "%p mds time_warp_seq %llu < %u\n", inode, |
895 | time_warp_seq, ci->i_time_warp_seq); | |
355da1eb SW |
896 | } |
897 | ||
79f2f6ad | 898 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
38d46409 XL |
899 | static int decode_encrypted_symlink(struct ceph_mds_client *mdsc, |
900 | const char *encsym, | |
901 | int enclen, u8 **decsym) | |
79f2f6ad | 902 | { |
38d46409 | 903 | struct ceph_client *cl = mdsc->fsc->client; |
79f2f6ad JL |
904 | int declen; |
905 | u8 *sym; | |
906 | ||
907 | sym = kmalloc(enclen + 1, GFP_NOFS); | |
908 | if (!sym) | |
909 | return -ENOMEM; | |
910 | ||
911 | declen = ceph_base64_decode(encsym, enclen, sym); | |
912 | if (declen < 0) { | |
38d46409 XL |
913 | pr_err_client(cl, |
914 | "can't decode symlink (%d). Content: %.*s\n", | |
915 | declen, enclen, encsym); | |
79f2f6ad JL |
916 | kfree(sym); |
917 | return -EIO; | |
918 | } | |
919 | sym[declen + 1] = '\0'; | |
920 | *decsym = sym; | |
921 | return declen; | |
922 | } | |
923 | #else | |
38d46409 XL |
924 | static int decode_encrypted_symlink(struct ceph_mds_client *mdsc, |
925 | const char *encsym, | |
926 | int symlen, u8 **decsym) | |
79f2f6ad JL |
927 | { |
928 | return -EOPNOTSUPP; | |
929 | } | |
930 | #endif | |
931 | ||
355da1eb SW |
932 | /* |
933 | * Populate an inode based on info from mds. May be called on new or | |
934 | * existing inodes. | |
935 | */ | |
966c7160 JL |
936 | int ceph_fill_inode(struct inode *inode, struct page *locked_page, |
937 | struct ceph_mds_reply_info_in *iinfo, | |
938 | struct ceph_mds_reply_dirfrag *dirinfo, | |
939 | struct ceph_mds_session *session, int cap_fmode, | |
940 | struct ceph_cap_reservation *caps_reservation) | |
355da1eb | 941 | { |
2678da88 | 942 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
38d46409 | 943 | struct ceph_client *cl = mdsc->fsc->client; |
355da1eb SW |
944 | struct ceph_mds_reply_inode *info = iinfo->in; |
945 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2af54a72 | 946 | int issued, new_issued, info_caps; |
9bbeab41 | 947 | struct timespec64 mtime, atime, ctime; |
355da1eb | 948 | struct ceph_buffer *xattr_blob = NULL; |
af8a85a4 | 949 | struct ceph_buffer *old_blob = NULL; |
779fe0fb | 950 | struct ceph_string *pool_ns = NULL; |
d9df2783 | 951 | struct ceph_cap *new_cap = NULL; |
355da1eb | 952 | int err = 0; |
d9df2783 | 953 | bool wake = false; |
f98a128a YZ |
954 | bool queue_trunc = false; |
955 | bool new_version = false; | |
31c542a1 | 956 | bool fill_inline = false; |
ed94f87c JL |
957 | umode_t mode = le32_to_cpu(info->mode); |
958 | dev_t rdev = le32_to_cpu(info->rdev); | |
355da1eb | 959 | |
27171ae6 JL |
960 | lockdep_assert_held(&mdsc->snap_rwsem); |
961 | ||
38d46409 XL |
962 | doutc(cl, "%p ino %llx.%llx v %llu had %llu\n", inode, ceph_vinop(inode), |
963 | le64_to_cpu(info->version), ci->i_version); | |
355da1eb | 964 | |
ed94f87c JL |
965 | /* Once I_NEW is cleared, we can't change type or dev numbers */ |
966 | if (inode->i_state & I_NEW) { | |
967 | inode->i_mode = mode; | |
968 | } else { | |
969 | if (inode_wrong_type(inode, mode)) { | |
38d46409 XL |
970 | pr_warn_once_client(cl, |
971 | "inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n", | |
972 | ceph_vinop(inode), inode->i_mode, mode); | |
ed94f87c JL |
973 | return -ESTALE; |
974 | } | |
975 | ||
976 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) { | |
38d46409 XL |
977 | pr_warn_once_client(cl, |
978 | "dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n", | |
979 | ceph_vinop(inode), MAJOR(inode->i_rdev), | |
980 | MINOR(inode->i_rdev), MAJOR(rdev), | |
981 | MINOR(rdev)); | |
ed94f87c JL |
982 | return -ESTALE; |
983 | } | |
984 | } | |
985 | ||
2af54a72 YZ |
986 | info_caps = le32_to_cpu(info->cap.caps); |
987 | ||
d9df2783 | 988 | /* prealloc new cap struct */ |
9a6bed4f | 989 | if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) { |
d9df2783 | 990 | new_cap = ceph_get_cap(mdsc, caps_reservation); |
9a6bed4f JL |
991 | if (!new_cap) |
992 | return -ENOMEM; | |
993 | } | |
d9df2783 | 994 | |
355da1eb SW |
995 | /* |
996 | * prealloc xattr data, if it looks like we'll need it. only | |
997 | * if len > 4 (meaning there are actually xattrs; the first 4 | |
998 | * bytes are the xattr count). | |
999 | */ | |
1000 | if (iinfo->xattr_len > 4) { | |
b6c1d5b8 | 1001 | xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); |
355da1eb | 1002 | if (!xattr_blob) |
38d46409 XL |
1003 | pr_err_client(cl, "ENOMEM xattr blob %d bytes\n", |
1004 | iinfo->xattr_len); | |
355da1eb SW |
1005 | } |
1006 | ||
779fe0fb YZ |
1007 | if (iinfo->pool_ns_len > 0) |
1008 | pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data, | |
1009 | iinfo->pool_ns_len); | |
1010 | ||
75c9627e YZ |
1011 | if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map) |
1012 | ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode)); | |
1013 | ||
be655596 | 1014 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
1015 | |
1016 | /* | |
1017 | * provided version will be odd if inode value is projected, | |
8bd59e01 SW |
1018 | * even if stable. skip the update if we have newer stable |
1019 | * info (ours>=theirs, e.g. due to racing mds replies), unless | |
1020 | * we are getting projected (unstable) info (in which case the | |
1021 | * version is odd, and we want ours>theirs). | |
1022 | * us them | |
1023 | * 2 2 skip | |
1024 | * 3 2 skip | |
1025 | * 3 3 update | |
355da1eb | 1026 | */ |
f98a128a YZ |
1027 | if (ci->i_version == 0 || |
1028 | ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && | |
1029 | le64_to_cpu(info->version) > (ci->i_version & ~1))) | |
1030 | new_version = true; | |
1031 | ||
a35ead31 JL |
1032 | /* Update change_attribute */ |
1033 | inode_set_max_iversion_raw(inode, iinfo->change_attr); | |
1034 | ||
2af54a72 YZ |
1035 | __ceph_caps_issued(ci, &issued); |
1036 | issued |= __ceph_caps_dirty(ci); | |
1037 | new_issued = ~issued & info_caps; | |
355da1eb | 1038 | |
d557c48d | 1039 | __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files); |
fb18a575 | 1040 | |
2d332d5b | 1041 | #ifdef CONFIG_FS_ENCRYPTION |
e127e030 LH |
1042 | if (iinfo->fscrypt_auth_len && |
1043 | ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) { | |
2d332d5b JL |
1044 | kfree(ci->fscrypt_auth); |
1045 | ci->fscrypt_auth_len = iinfo->fscrypt_auth_len; | |
1046 | ci->fscrypt_auth = iinfo->fscrypt_auth; | |
1047 | iinfo->fscrypt_auth = NULL; | |
1048 | iinfo->fscrypt_auth_len = 0; | |
1049 | inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED); | |
1050 | } | |
1051 | #endif | |
1052 | ||
f98a128a YZ |
1053 | if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && |
1054 | (issued & CEPH_CAP_AUTH_EXCL) == 0) { | |
ed94f87c | 1055 | inode->i_mode = mode; |
ab871b90 EB |
1056 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); |
1057 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); | |
38d46409 XL |
1058 | doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode, |
1059 | ceph_vinop(inode), inode->i_mode, | |
1060 | from_kuid(&init_user_ns, inode->i_uid), | |
1061 | from_kgid(&init_user_ns, inode->i_gid)); | |
245ce991 | 1062 | ceph_decode_timespec64(&ci->i_btime, &iinfo->btime); |
193e7b37 | 1063 | ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime); |
355da1eb SW |
1064 | } |
1065 | ||
94af0470 JL |
1066 | /* directories have fl_stripe_unit set to zero */ |
1067 | if (IS_ENCRYPTED(inode)) | |
1068 | inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT; | |
1069 | else if (le32_to_cpu(info->layout.fl_stripe_unit)) | |
1070 | inode->i_blkbits = | |
1071 | fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; | |
1072 | else | |
1073 | inode->i_blkbits = CEPH_BLOCK_SHIFT; | |
1074 | ||
f98a128a YZ |
1075 | if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && |
1076 | (issued & CEPH_CAP_LINK_EXCL) == 0) | |
bfe86848 | 1077 | set_nlink(inode, le32_to_cpu(info->nlink)); |
355da1eb | 1078 | |
f98a128a YZ |
1079 | if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { |
1080 | /* be careful with mtime, atime, size */ | |
9bbeab41 AB |
1081 | ceph_decode_timespec64(&atime, &info->atime); |
1082 | ceph_decode_timespec64(&mtime, &info->mtime); | |
1083 | ceph_decode_timespec64(&ctime, &info->ctime); | |
f98a128a YZ |
1084 | ceph_fill_file_time(inode, issued, |
1085 | le32_to_cpu(info->time_warp_seq), | |
1086 | &ctime, &mtime, &atime); | |
1087 | } | |
1088 | ||
2af54a72 YZ |
1089 | if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) { |
1090 | ci->i_files = le64_to_cpu(info->files); | |
1091 | ci->i_subdirs = le64_to_cpu(info->subdirs); | |
1092 | } | |
1093 | ||
f98a128a YZ |
1094 | if (new_version || |
1095 | (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { | |
16be62fc | 1096 | u64 size = le64_to_cpu(info->size); |
7627151e | 1097 | s64 old_pool = ci->i_layout.pool_id; |
779fe0fb YZ |
1098 | struct ceph_string *old_ns; |
1099 | ||
7627151e | 1100 | ceph_file_layout_from_legacy(&ci->i_layout, &info->layout); |
779fe0fb YZ |
1101 | old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, |
1102 | lockdep_is_held(&ci->i_ceph_lock)); | |
1103 | rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns); | |
1104 | ||
1105 | if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns) | |
10183a69 | 1106 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; |
10183a69 | 1107 | |
779fe0fb | 1108 | pool_ns = old_ns; |
10183a69 | 1109 | |
16be62fc JL |
1110 | if (IS_ENCRYPTED(inode) && size && |
1111 | iinfo->fscrypt_file_len == sizeof(__le64)) { | |
1112 | u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file); | |
1113 | ||
1114 | if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) { | |
1115 | size = fsize; | |
1116 | } else { | |
38d46409 XL |
1117 | pr_warn_client(cl, |
1118 | "fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n", | |
16be62fc JL |
1119 | info->size, size); |
1120 | } | |
1121 | } | |
1122 | ||
f98a128a YZ |
1123 | queue_trunc = ceph_fill_file_size(inode, issued, |
1124 | le32_to_cpu(info->truncate_seq), | |
1125 | le64_to_cpu(info->truncate_size), | |
16be62fc | 1126 | size); |
f98a128a YZ |
1127 | /* only update max_size on auth cap */ |
1128 | if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && | |
1129 | ci->i_max_size != le64_to_cpu(info->max_size)) { | |
38d46409 XL |
1130 | doutc(cl, "max_size %lld -> %llu\n", |
1131 | ci->i_max_size, le64_to_cpu(info->max_size)); | |
f98a128a YZ |
1132 | ci->i_max_size = le64_to_cpu(info->max_size); |
1133 | } | |
1134 | } | |
355da1eb | 1135 | |
49a9f4f6 YZ |
1136 | /* layout and rstat are not tracked by capability, update them if |
1137 | * the inode info is from auth mds */ | |
1138 | if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) { | |
1139 | if (S_ISDIR(inode->i_mode)) { | |
1140 | ci->i_dir_layout = iinfo->dir_layout; | |
1141 | ci->i_rbytes = le64_to_cpu(info->rbytes); | |
1142 | ci->i_rfiles = le64_to_cpu(info->rfiles); | |
1143 | ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); | |
08796873 | 1144 | ci->i_dir_pin = iinfo->dir_pin; |
e7f72952 | 1145 | ci->i_rsnaps = iinfo->rsnaps; |
9bbeab41 | 1146 | ceph_decode_timespec64(&ci->i_rctime, &info->rctime); |
49a9f4f6 YZ |
1147 | } |
1148 | } | |
1149 | ||
355da1eb SW |
1150 | /* xattrs */ |
1151 | /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ | |
508b32d8 | 1152 | if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && |
355da1eb SW |
1153 | le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { |
1154 | if (ci->i_xattrs.blob) | |
af8a85a4 | 1155 | old_blob = ci->i_xattrs.blob; |
355da1eb SW |
1156 | ci->i_xattrs.blob = xattr_blob; |
1157 | if (xattr_blob) | |
1158 | memcpy(ci->i_xattrs.blob->vec.iov_base, | |
1159 | iinfo->xattr_data, iinfo->xattr_len); | |
1160 | ci->i_xattrs.version = le64_to_cpu(info->xattr_version); | |
7221fe4c | 1161 | ceph_forget_all_cached_acls(inode); |
ac6713cc | 1162 | ceph_security_invalidate_secctx(inode); |
a6424e48 | 1163 | xattr_blob = NULL; |
355da1eb SW |
1164 | } |
1165 | ||
ffdeec7a | 1166 | /* finally update i_version */ |
aae1a442 YZ |
1167 | if (le64_to_cpu(info->version) > ci->i_version) |
1168 | ci->i_version = le64_to_cpu(info->version); | |
ffdeec7a | 1169 | |
355da1eb | 1170 | inode->i_mapping->a_ops = &ceph_aops; |
355da1eb SW |
1171 | |
1172 | switch (inode->i_mode & S_IFMT) { | |
1173 | case S_IFIFO: | |
1174 | case S_IFBLK: | |
1175 | case S_IFCHR: | |
1176 | case S_IFSOCK: | |
5ba72e60 | 1177 | inode->i_blkbits = PAGE_SHIFT; |
ed94f87c | 1178 | init_special_inode(inode, inode->i_mode, rdev); |
355da1eb SW |
1179 | inode->i_op = &ceph_file_iops; |
1180 | break; | |
1181 | case S_IFREG: | |
1182 | inode->i_op = &ceph_file_iops; | |
1183 | inode->i_fop = &ceph_file_fops; | |
1184 | break; | |
1185 | case S_IFLNK: | |
355da1eb | 1186 | if (!ci->i_symlink) { |
810339ec | 1187 | u32 symlen = iinfo->symlink_len; |
355da1eb SW |
1188 | char *sym; |
1189 | ||
be655596 | 1190 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 1191 | |
79f2f6ad JL |
1192 | if (IS_ENCRYPTED(inode)) { |
1193 | if (symlen != i_size_read(inode)) | |
38d46409 XL |
1194 | pr_err_client(cl, |
1195 | "%p %llx.%llx BAD symlink size %lld\n", | |
1196 | inode, ceph_vinop(inode), | |
79f2f6ad JL |
1197 | i_size_read(inode)); |
1198 | ||
38d46409 | 1199 | err = decode_encrypted_symlink(mdsc, iinfo->symlink, |
79f2f6ad JL |
1200 | symlen, (u8 **)&sym); |
1201 | if (err < 0) { | |
38d46409 XL |
1202 | pr_err_client(cl, |
1203 | "decoding encrypted symlink failed: %d\n", | |
1204 | err); | |
79f2f6ad JL |
1205 | goto out; |
1206 | } | |
1207 | symlen = err; | |
224a7542 YZ |
1208 | i_size_write(inode, symlen); |
1209 | inode->i_blocks = calc_inode_blocks(symlen); | |
79f2f6ad JL |
1210 | } else { |
1211 | if (symlen != i_size_read(inode)) { | |
38d46409 XL |
1212 | pr_err_client(cl, |
1213 | "%p %llx.%llx BAD symlink size %lld\n", | |
1214 | inode, ceph_vinop(inode), | |
79f2f6ad JL |
1215 | i_size_read(inode)); |
1216 | i_size_write(inode, symlen); | |
1217 | inode->i_blocks = calc_inode_blocks(symlen); | |
1218 | } | |
810339ec | 1219 | |
79f2f6ad JL |
1220 | err = -ENOMEM; |
1221 | sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); | |
1222 | if (!sym) | |
1223 | goto out; | |
1224 | } | |
355da1eb | 1225 | |
be655596 | 1226 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
1227 | if (!ci->i_symlink) |
1228 | ci->i_symlink = sym; | |
1229 | else | |
1230 | kfree(sym); /* lost a race */ | |
1231 | } | |
79f2f6ad JL |
1232 | |
1233 | if (IS_ENCRYPTED(inode)) { | |
1234 | /* | |
1235 | * Encrypted symlinks need to be decrypted before we can | |
1236 | * cache their targets in i_link. Don't touch it here. | |
1237 | */ | |
1238 | inode->i_op = &ceph_encrypted_symlink_iops; | |
1239 | } else { | |
1240 | inode->i_link = ci->i_symlink; | |
1241 | inode->i_op = &ceph_symlink_iops; | |
1242 | } | |
355da1eb SW |
1243 | break; |
1244 | case S_IFDIR: | |
1245 | inode->i_op = &ceph_dir_iops; | |
1246 | inode->i_fop = &ceph_dir_fops; | |
355da1eb SW |
1247 | break; |
1248 | default: | |
38d46409 XL |
1249 | pr_err_client(cl, "%p %llx.%llx BAD mode 0%o\n", inode, |
1250 | ceph_vinop(inode), inode->i_mode); | |
355da1eb SW |
1251 | } |
1252 | ||
355da1eb | 1253 | /* were we issued a capability? */ |
2af54a72 | 1254 | if (info_caps) { |
355da1eb SW |
1255 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
1256 | ceph_add_cap(inode, session, | |
1257 | le64_to_cpu(info->cap.cap_id), | |
135e671e | 1258 | info_caps, |
355da1eb SW |
1259 | le32_to_cpu(info->cap.wanted), |
1260 | le32_to_cpu(info->cap.seq), | |
1261 | le32_to_cpu(info->cap.mseq), | |
1262 | le64_to_cpu(info->cap.realm), | |
d9df2783 | 1263 | info->cap.flags, &new_cap); |
2f92b3d0 YZ |
1264 | |
1265 | /* set dir completion flag? */ | |
1266 | if (S_ISDIR(inode->i_mode) && | |
1267 | ci->i_files == 0 && ci->i_subdirs == 0 && | |
2af54a72 | 1268 | (info_caps & CEPH_CAP_FILE_SHARED) && |
2f92b3d0 YZ |
1269 | (issued & CEPH_CAP_FILE_EXCL) == 0 && |
1270 | !__ceph_dir_is_complete(ci)) { | |
38d46409 XL |
1271 | doutc(cl, " marking %p complete (empty)\n", |
1272 | inode); | |
fdd4e158 | 1273 | i_size_write(inode, 0); |
2f92b3d0 | 1274 | __ceph_dir_set_complete(ci, |
fdd4e158 YZ |
1275 | atomic64_read(&ci->i_release_count), |
1276 | atomic64_read(&ci->i_ordered_count)); | |
2f92b3d0 YZ |
1277 | } |
1278 | ||
d9df2783 | 1279 | wake = true; |
355da1eb | 1280 | } else { |
38d46409 XL |
1281 | doutc(cl, " %p got snap_caps %s\n", inode, |
1282 | ceph_cap_string(info_caps)); | |
2af54a72 | 1283 | ci->i_snap_caps |= info_caps; |
355da1eb SW |
1284 | } |
1285 | } | |
31c542a1 YZ |
1286 | |
1287 | if (iinfo->inline_version > 0 && | |
1288 | iinfo->inline_version >= ci->i_inline_version) { | |
1289 | int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; | |
1290 | ci->i_inline_version = iinfo->inline_version; | |
48490776 | 1291 | if (ceph_has_inline_data(ci) && |
2af54a72 | 1292 | (locked_page || (info_caps & cache_caps))) |
31c542a1 YZ |
1293 | fill_inline = true; |
1294 | } | |
1295 | ||
719a2514 YZ |
1296 | if (cap_fmode >= 0) { |
1297 | if (!info_caps) | |
38d46409 XL |
1298 | pr_warn_client(cl, "mds issued no caps on %llx.%llx\n", |
1299 | ceph_vinop(inode)); | |
719a2514 YZ |
1300 | __ceph_touch_fmode(ci, mdsc, cap_fmode); |
1301 | } | |
1302 | ||
be655596 | 1303 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 1304 | |
400e1286 JL |
1305 | ceph_fscache_register_inode_cookie(inode); |
1306 | ||
31c542a1 | 1307 | if (fill_inline) |
01deead0 | 1308 | ceph_fill_inline_data(inode, locked_page, |
31c542a1 YZ |
1309 | iinfo->inline_data, iinfo->inline_len); |
1310 | ||
d9df2783 YZ |
1311 | if (wake) |
1312 | wake_up_all(&ci->i_cap_wq); | |
1313 | ||
355da1eb SW |
1314 | /* queue truncate if we saw i_size decrease */ |
1315 | if (queue_trunc) | |
3c6f6b79 | 1316 | ceph_queue_vmtruncate(inode); |
355da1eb SW |
1317 | |
1318 | /* populate frag tree */ | |
3e7fbe9c YZ |
1319 | if (S_ISDIR(inode->i_mode)) |
1320 | ceph_fill_fragtree(inode, &info->fragtree, dirinfo); | |
355da1eb SW |
1321 | |
1322 | /* update delegation info? */ | |
1323 | if (dirinfo) | |
1324 | ceph_fill_dirfrag(inode, dirinfo); | |
1325 | ||
1326 | err = 0; | |
355da1eb | 1327 | out: |
d9df2783 YZ |
1328 | if (new_cap) |
1329 | ceph_put_cap(mdsc, new_cap); | |
af8a85a4 LH |
1330 | ceph_buffer_put(old_blob); |
1331 | ceph_buffer_put(xattr_blob); | |
779fe0fb | 1332 | ceph_put_string(pool_ns); |
355da1eb SW |
1333 | return err; |
1334 | } | |
1335 | ||
1336 | /* | |
543212b3 | 1337 | * caller should hold session s_mutex and dentry->d_lock. |
355da1eb | 1338 | */ |
543212b3 YZ |
1339 | static void __update_dentry_lease(struct inode *dir, struct dentry *dentry, |
1340 | struct ceph_mds_reply_lease *lease, | |
1341 | struct ceph_mds_session *session, | |
1342 | unsigned long from_time, | |
1343 | struct ceph_mds_session **old_lease_session) | |
355da1eb | 1344 | { |
38d46409 | 1345 | struct ceph_client *cl = ceph_inode_to_client(dir); |
355da1eb | 1346 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
f5e17aed | 1347 | unsigned mask = le16_to_cpu(lease->mask); |
355da1eb SW |
1348 | long unsigned duration = le32_to_cpu(lease->duration_ms); |
1349 | long unsigned ttl = from_time + (duration * HZ) / 1000; | |
1350 | long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; | |
f5d55f03 | 1351 | |
38d46409 | 1352 | doutc(cl, "%p duration %lu ms ttl %lu\n", dentry, duration, ttl); |
355da1eb | 1353 | |
18fc8abd AV |
1354 | /* only track leases on regular dentries */ |
1355 | if (ceph_snap(dir) != CEPH_NOSNAP) | |
543212b3 | 1356 | return; |
18fc8abd | 1357 | |
f5e17aed JL |
1358 | if (mask & CEPH_LEASE_PRIMARY_LINK) |
1359 | di->flags |= CEPH_DENTRY_PRIMARY_LINK; | |
1360 | else | |
1361 | di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; | |
1362 | ||
97aeb6bf | 1363 | di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen); |
f5e17aed | 1364 | if (!(mask & CEPH_LEASE_VALID)) { |
37c4efc1 | 1365 | __ceph_dentry_dir_lease_touch(di); |
543212b3 | 1366 | return; |
37c4efc1 | 1367 | } |
355da1eb | 1368 | |
52d60f8e | 1369 | if (di->lease_gen == atomic_read(&session->s_cap_gen) && |
9b16f03c | 1370 | time_before(ttl, di->time)) |
543212b3 | 1371 | return; /* we already have a newer lease. */ |
355da1eb | 1372 | |
481f001f | 1373 | if (di->lease_session && di->lease_session != session) { |
543212b3 | 1374 | *old_lease_session = di->lease_session; |
481f001f YZ |
1375 | di->lease_session = NULL; |
1376 | } | |
355da1eb | 1377 | |
355da1eb SW |
1378 | if (!di->lease_session) |
1379 | di->lease_session = ceph_get_mds_session(session); | |
52d60f8e | 1380 | di->lease_gen = atomic_read(&session->s_cap_gen); |
355da1eb SW |
1381 | di->lease_seq = le32_to_cpu(lease->seq); |
1382 | di->lease_renew_after = half_ttl; | |
1383 | di->lease_renew_from = 0; | |
9b16f03c | 1384 | di->time = ttl; |
37c4efc1 YZ |
1385 | |
1386 | __ceph_dentry_lease_touch(di); | |
543212b3 YZ |
1387 | } |
1388 | ||
1389 | static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry, | |
1390 | struct ceph_mds_reply_lease *lease, | |
1391 | struct ceph_mds_session *session, | |
1392 | unsigned long from_time) | |
1393 | { | |
1394 | struct ceph_mds_session *old_lease_session = NULL; | |
1395 | spin_lock(&dentry->d_lock); | |
1396 | __update_dentry_lease(dir, dentry, lease, session, from_time, | |
1397 | &old_lease_session); | |
1398 | spin_unlock(&dentry->d_lock); | |
7e65624d | 1399 | ceph_put_mds_session(old_lease_session); |
543212b3 YZ |
1400 | } |
1401 | ||
1402 | /* | |
1403 | * update dentry lease without having parent inode locked | |
1404 | */ | |
1405 | static void update_dentry_lease_careful(struct dentry *dentry, | |
1406 | struct ceph_mds_reply_lease *lease, | |
1407 | struct ceph_mds_session *session, | |
1408 | unsigned long from_time, | |
1409 | char *dname, u32 dname_len, | |
1410 | struct ceph_vino *pdvino, | |
1411 | struct ceph_vino *ptvino) | |
1412 | ||
1413 | { | |
1414 | struct inode *dir; | |
1415 | struct ceph_mds_session *old_lease_session = NULL; | |
1416 | ||
1417 | spin_lock(&dentry->d_lock); | |
1418 | /* make sure dentry's name matches target */ | |
1419 | if (dentry->d_name.len != dname_len || | |
1420 | memcmp(dentry->d_name.name, dname, dname_len)) | |
1421 | goto out_unlock; | |
1422 | ||
1423 | dir = d_inode(dentry->d_parent); | |
1424 | /* make sure parent matches dvino */ | |
1425 | if (!ceph_ino_compare(dir, pdvino)) | |
1426 | goto out_unlock; | |
1427 | ||
1428 | /* make sure dentry's inode matches target. NULL ptvino means that | |
1429 | * we expect a negative dentry */ | |
1430 | if (ptvino) { | |
1431 | if (d_really_is_negative(dentry)) | |
1432 | goto out_unlock; | |
1433 | if (!ceph_ino_compare(d_inode(dentry), ptvino)) | |
1434 | goto out_unlock; | |
1435 | } else { | |
1436 | if (d_really_is_positive(dentry)) | |
1437 | goto out_unlock; | |
1438 | } | |
1439 | ||
1440 | __update_dentry_lease(dir, dentry, lease, session, | |
1441 | from_time, &old_lease_session); | |
355da1eb SW |
1442 | out_unlock: |
1443 | spin_unlock(&dentry->d_lock); | |
7e65624d | 1444 | ceph_put_mds_session(old_lease_session); |
355da1eb SW |
1445 | } |
1446 | ||
1447 | /* | |
1448 | * splice a dentry to an inode. | |
810313c5 | 1449 | * caller must hold directory i_rwsem for this to be safe. |
355da1eb | 1450 | */ |
2bf996ac | 1451 | static int splice_dentry(struct dentry **pdn, struct inode *in) |
355da1eb | 1452 | { |
38d46409 | 1453 | struct ceph_client *cl = ceph_inode_to_client(in); |
2bf996ac | 1454 | struct dentry *dn = *pdn; |
355da1eb SW |
1455 | struct dentry *realdn; |
1456 | ||
2b0143b5 | 1457 | BUG_ON(d_inode(dn)); |
1cd3935b | 1458 | |
5495c2d0 YZ |
1459 | if (S_ISDIR(in->i_mode)) { |
1460 | /* If inode is directory, d_splice_alias() below will remove | |
1461 | * 'realdn' from its origin parent. We need to ensure that | |
1462 | * origin parent's readdir cache will not reference 'realdn' | |
1463 | */ | |
1464 | realdn = d_find_any_alias(in); | |
1465 | if (realdn) { | |
1466 | struct ceph_dentry_info *di = ceph_dentry(realdn); | |
1467 | spin_lock(&realdn->d_lock); | |
1468 | ||
1469 | realdn->d_op->d_prune(realdn); | |
1470 | ||
1471 | di->time = jiffies; | |
1472 | di->lease_shared_gen = 0; | |
1473 | di->offset = 0; | |
1474 | ||
1475 | spin_unlock(&realdn->d_lock); | |
1476 | dput(realdn); | |
1477 | } | |
1478 | } | |
1479 | ||
355da1eb SW |
1480 | /* dn must be unhashed */ |
1481 | if (!d_unhashed(dn)) | |
1482 | d_drop(dn); | |
41d28bca | 1483 | realdn = d_splice_alias(in, dn); |
355da1eb | 1484 | if (IS_ERR(realdn)) { |
38d46409 XL |
1485 | pr_err_client(cl, "error %ld %p inode %p ino %llx.%llx\n", |
1486 | PTR_ERR(realdn), dn, in, ceph_vinop(in)); | |
2bf996ac YZ |
1487 | return PTR_ERR(realdn); |
1488 | } | |
1489 | ||
1490 | if (realdn) { | |
38d46409 XL |
1491 | doutc(cl, "dn %p (%d) spliced with %p (%d) inode %p ino %llx.%llx\n", |
1492 | dn, d_count(dn), realdn, d_count(realdn), | |
1493 | d_inode(realdn), ceph_vinop(d_inode(realdn))); | |
355da1eb | 1494 | dput(dn); |
2bf996ac | 1495 | *pdn = realdn; |
355da1eb SW |
1496 | } else { |
1497 | BUG_ON(!ceph_dentry(dn)); | |
38d46409 XL |
1498 | doutc(cl, "dn %p attached to %p ino %llx.%llx\n", dn, |
1499 | d_inode(dn), ceph_vinop(d_inode(dn))); | |
355da1eb | 1500 | } |
2bf996ac | 1501 | return 0; |
355da1eb SW |
1502 | } |
1503 | ||
1504 | /* | |
1505 | * Incorporate results into the local cache. This is either just | |
1506 | * one inode, or a directory, dentry, and possibly linked-to inode (e.g., | |
1507 | * after a lookup). | |
1508 | * | |
1509 | * A reply may contain | |
1510 | * a directory inode along with a dentry. | |
1511 | * and/or a target inode | |
1512 | * | |
1513 | * Called with snap_rwsem (read). | |
1514 | */ | |
f5a03b08 | 1515 | int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req) |
355da1eb | 1516 | { |
f5a03b08 | 1517 | struct ceph_mds_session *session = req->r_session; |
355da1eb SW |
1518 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
1519 | struct inode *in = NULL; | |
f5d55f03 | 1520 | struct ceph_vino tvino, dvino; |
5995d90d | 1521 | struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb); |
38d46409 | 1522 | struct ceph_client *cl = fsc->client; |
355da1eb SW |
1523 | int err = 0; |
1524 | ||
38d46409 XL |
1525 | doutc(cl, "%p is_dentry %d is_target %d\n", req, |
1526 | rinfo->head->is_dentry, rinfo->head->is_target); | |
355da1eb | 1527 | |
355da1eb | 1528 | if (!rinfo->head->is_target && !rinfo->head->is_dentry) { |
38d46409 | 1529 | doutc(cl, "reply is empty!\n"); |
3dd69aab | 1530 | if (rinfo->head->result == 0 && req->r_parent) |
167c9e35 | 1531 | ceph_invalidate_dir_request(req); |
355da1eb SW |
1532 | return 0; |
1533 | } | |
1534 | ||
1535 | if (rinfo->head->is_dentry) { | |
3dd69aab | 1536 | struct inode *dir = req->r_parent; |
5b1daecd | 1537 | |
6c5e50fa | 1538 | if (dir) { |
966c7160 JL |
1539 | err = ceph_fill_inode(dir, NULL, &rinfo->diri, |
1540 | rinfo->dirfrag, session, -1, | |
1541 | &req->r_caps_reservation); | |
6c5e50fa | 1542 | if (err < 0) |
19913b4e | 1543 | goto done; |
6c5e50fa SW |
1544 | } else { |
1545 | WARN_ON_ONCE(1); | |
1546 | } | |
19913b4e | 1547 | |
74c9e6bf YZ |
1548 | if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME && |
1549 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && | |
1550 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { | |
85529096 | 1551 | bool is_nokey = false; |
19913b4e YZ |
1552 | struct qstr dname; |
1553 | struct dentry *dn, *parent; | |
85529096 JL |
1554 | struct fscrypt_str oname = FSTR_INIT(NULL, 0); |
1555 | struct ceph_fname fname = { .dir = dir, | |
1556 | .name = rinfo->dname, | |
1557 | .ctext = rinfo->altname, | |
1558 | .name_len = rinfo->dname_len, | |
1559 | .ctext_len = rinfo->altname_len }; | |
19913b4e YZ |
1560 | |
1561 | BUG_ON(!rinfo->head->is_target); | |
1562 | BUG_ON(req->r_dentry); | |
1563 | ||
1564 | parent = d_find_any_alias(dir); | |
1565 | BUG_ON(!parent); | |
1566 | ||
85529096 JL |
1567 | err = ceph_fname_alloc_buffer(dir, &oname); |
1568 | if (err < 0) { | |
1569 | dput(parent); | |
1570 | goto done; | |
1571 | } | |
1572 | ||
1573 | err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey); | |
1574 | if (err < 0) { | |
1575 | dput(parent); | |
1576 | ceph_fname_free_buffer(dir, &oname); | |
1577 | goto done; | |
1578 | } | |
1579 | dname.name = oname.name; | |
1580 | dname.len = oname.len; | |
8387ff25 | 1581 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
f5d55f03 JL |
1582 | tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); |
1583 | tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); | |
19913b4e YZ |
1584 | retry_lookup: |
1585 | dn = d_lookup(parent, &dname); | |
38d46409 XL |
1586 | doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n", |
1587 | parent, dname.len, dname.name, dn); | |
19913b4e YZ |
1588 | |
1589 | if (!dn) { | |
1590 | dn = d_alloc(parent, &dname); | |
38d46409 XL |
1591 | doutc(cl, "d_alloc %p '%.*s' = %p\n", parent, |
1592 | dname.len, dname.name, dn); | |
d37b1d99 | 1593 | if (!dn) { |
19913b4e | 1594 | dput(parent); |
85529096 | 1595 | ceph_fname_free_buffer(dir, &oname); |
19913b4e YZ |
1596 | err = -ENOMEM; |
1597 | goto done; | |
1598 | } | |
85529096 JL |
1599 | if (is_nokey) { |
1600 | spin_lock(&dn->d_lock); | |
1601 | dn->d_flags |= DCACHE_NOKEY_NAME; | |
1602 | spin_unlock(&dn->d_lock); | |
1603 | } | |
ad5cb123 | 1604 | err = 0; |
2b0143b5 | 1605 | } else if (d_really_is_positive(dn) && |
f5d55f03 JL |
1606 | (ceph_ino(d_inode(dn)) != tvino.ino || |
1607 | ceph_snap(d_inode(dn)) != tvino.snap)) { | |
38d46409 XL |
1608 | doutc(cl, " dn %p points to wrong inode %p\n", |
1609 | dn, d_inode(dn)); | |
933ad2c9 | 1610 | ceph_dir_clear_ordered(dir); |
19913b4e YZ |
1611 | d_delete(dn); |
1612 | dput(dn); | |
1613 | goto retry_lookup; | |
1614 | } | |
85529096 | 1615 | ceph_fname_free_buffer(dir, &oname); |
19913b4e YZ |
1616 | |
1617 | req->r_dentry = dn; | |
1618 | dput(parent); | |
1619 | } | |
5b1daecd SW |
1620 | } |
1621 | ||
86b58d13 | 1622 | if (rinfo->head->is_target) { |
bca9fc14 JL |
1623 | /* Should be filled in by handle_reply */ |
1624 | BUG_ON(!req->r_target_inode); | |
86b58d13 | 1625 | |
bca9fc14 | 1626 | in = req->r_target_inode; |
966c7160 JL |
1627 | err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti, |
1628 | NULL, session, | |
bc2de10d | 1629 | (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && |
3bb48b41 | 1630 | !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && |
57c21994 | 1631 | rinfo->head->result == 0) ? req->r_fmode : -1, |
86b58d13 YZ |
1632 | &req->r_caps_reservation); |
1633 | if (err < 0) { | |
38d46409 XL |
1634 | pr_err_client(cl, "badness %p %llx.%llx\n", in, |
1635 | ceph_vinop(in)); | |
bca9fc14 | 1636 | req->r_target_inode = NULL; |
893e456b JL |
1637 | if (in->i_state & I_NEW) |
1638 | discard_new_inode(in); | |
68cbb805 JL |
1639 | else |
1640 | iput(in); | |
86b58d13 YZ |
1641 | goto done; |
1642 | } | |
893e456b JL |
1643 | if (in->i_state & I_NEW) |
1644 | unlock_new_inode(in); | |
86b58d13 YZ |
1645 | } |
1646 | ||
9358c6d4 SW |
1647 | /* |
1648 | * ignore null lease/binding on snapdir ENOENT, or else we | |
1649 | * will have trouble splicing in the virtual snapdir later | |
1650 | */ | |
3dd69aab JL |
1651 | if (rinfo->head->is_dentry && |
1652 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) && | |
1653 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && | |
9358c6d4 | 1654 | (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, |
3d14c5d2 | 1655 | fsc->mount_options->snapdir_name, |
9358c6d4 | 1656 | req->r_dentry->d_name.len))) { |
355da1eb SW |
1657 | /* |
1658 | * lookup link rename : null -> possibly existing inode | |
1659 | * mknod symlink mkdir : null -> new inode | |
1660 | * unlink : linked -> null | |
1661 | */ | |
3dd69aab | 1662 | struct inode *dir = req->r_parent; |
355da1eb SW |
1663 | struct dentry *dn = req->r_dentry; |
1664 | bool have_dir_cap, have_lease; | |
1665 | ||
1666 | BUG_ON(!dn); | |
1667 | BUG_ON(!dir); | |
2b0143b5 | 1668 | BUG_ON(d_inode(dn->d_parent) != dir); |
f5d55f03 JL |
1669 | |
1670 | dvino.ino = le64_to_cpu(rinfo->diri.in->ino); | |
1671 | dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); | |
1672 | ||
1673 | BUG_ON(ceph_ino(dir) != dvino.ino); | |
1674 | BUG_ON(ceph_snap(dir) != dvino.snap); | |
355da1eb | 1675 | |
355da1eb SW |
1676 | /* do we have a lease on the whole dir? */ |
1677 | have_dir_cap = | |
1678 | (le32_to_cpu(rinfo->diri.in->cap.caps) & | |
1679 | CEPH_CAP_FILE_SHARED); | |
1680 | ||
1681 | /* do we have a dn lease? */ | |
1682 | have_lease = have_dir_cap || | |
2f90b852 | 1683 | le32_to_cpu(rinfo->dlease->duration_ms); |
355da1eb | 1684 | if (!have_lease) |
38d46409 | 1685 | doutc(cl, "no dentry lease or dir cap\n"); |
355da1eb SW |
1686 | |
1687 | /* rename? */ | |
1688 | if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { | |
0a8a70f9 YZ |
1689 | struct inode *olddir = req->r_old_dentry_dir; |
1690 | BUG_ON(!olddir); | |
1691 | ||
38d46409 XL |
1692 | doutc(cl, " src %p '%pd' dst %p '%pd'\n", |
1693 | req->r_old_dentry, req->r_old_dentry, dn, dn); | |
1694 | doutc(cl, "doing d_move %p -> %p\n", req->r_old_dentry, dn); | |
c10f5e12 | 1695 | |
fdd4e158 YZ |
1696 | /* d_move screws up sibling dentries' offsets */ |
1697 | ceph_dir_clear_ordered(dir); | |
1698 | ceph_dir_clear_ordered(olddir); | |
1699 | ||
355da1eb | 1700 | d_move(req->r_old_dentry, dn); |
38d46409 XL |
1701 | doutc(cl, " src %p '%pd' dst %p '%pd'\n", |
1702 | req->r_old_dentry, req->r_old_dentry, dn, dn); | |
81a6cf2d | 1703 | |
c4a29f26 SW |
1704 | /* ensure target dentry is invalidated, despite |
1705 | rehashing bug in vfs_rename_dir */ | |
81a6cf2d SW |
1706 | ceph_invalidate_dentry_lease(dn); |
1707 | ||
38d46409 XL |
1708 | doutc(cl, "dn %p gets new offset %lld\n", |
1709 | req->r_old_dentry, | |
1710 | ceph_dentry(req->r_old_dentry)->offset); | |
81a6cf2d | 1711 | |
2bf996ac YZ |
1712 | /* swap r_dentry and r_old_dentry in case that |
1713 | * splice_dentry() gets called later. This is safe | |
1714 | * because no other place will use them */ | |
1715 | req->r_dentry = req->r_old_dentry; | |
1716 | req->r_old_dentry = dn; | |
1717 | dn = req->r_dentry; | |
355da1eb SW |
1718 | } |
1719 | ||
1720 | /* null dentry? */ | |
1721 | if (!rinfo->head->is_target) { | |
38d46409 | 1722 | doutc(cl, "null dentry\n"); |
2b0143b5 | 1723 | if (d_really_is_positive(dn)) { |
38d46409 | 1724 | doutc(cl, "d_delete %p\n", dn); |
5495c2d0 | 1725 | ceph_dir_clear_ordered(dir); |
355da1eb | 1726 | d_delete(dn); |
80d025ff JL |
1727 | } else if (have_lease) { |
1728 | if (d_unhashed(dn)) | |
f8b31710 | 1729 | d_add(dn, NULL); |
7ffe4fce XL |
1730 | } |
1731 | ||
1732 | if (!d_unhashed(dn) && have_lease) | |
543212b3 YZ |
1733 | update_dentry_lease(dir, dn, |
1734 | rinfo->dlease, session, | |
1735 | req->r_request_started); | |
355da1eb SW |
1736 | goto done; |
1737 | } | |
1738 | ||
1739 | /* attach proper inode */ | |
2b0143b5 | 1740 | if (d_really_is_negative(dn)) { |
70db4f36 | 1741 | ceph_dir_clear_ordered(dir); |
86b58d13 | 1742 | ihold(in); |
2bf996ac YZ |
1743 | err = splice_dentry(&req->r_dentry, in); |
1744 | if (err < 0) | |
355da1eb | 1745 | goto done; |
2bf996ac | 1746 | dn = req->r_dentry; /* may have spliced */ |
2b0143b5 | 1747 | } else if (d_really_is_positive(dn) && d_inode(dn) != in) { |
38d46409 XL |
1748 | doutc(cl, " %p links to %p %llx.%llx, not %llx.%llx\n", |
1749 | dn, d_inode(dn), ceph_vinop(d_inode(dn)), | |
1750 | ceph_vinop(in)); | |
200fd27c | 1751 | d_invalidate(dn); |
355da1eb | 1752 | have_lease = false; |
355da1eb SW |
1753 | } |
1754 | ||
f5d55f03 | 1755 | if (have_lease) { |
543212b3 YZ |
1756 | update_dentry_lease(dir, dn, |
1757 | rinfo->dlease, session, | |
1758 | req->r_request_started); | |
f5d55f03 | 1759 | } |
38d46409 | 1760 | doutc(cl, " final dn %p\n", dn); |
bc2de10d JL |
1761 | } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP || |
1762 | req->r_op == CEPH_MDS_OP_MKSNAP) && | |
1f08529c | 1763 | test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) && |
bc2de10d | 1764 | !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
3dd69aab | 1765 | struct inode *dir = req->r_parent; |
355da1eb SW |
1766 | |
1767 | /* fill out a snapdir LOOKUPSNAP dentry */ | |
0a8a70f9 YZ |
1768 | BUG_ON(!dir); |
1769 | BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); | |
2bf996ac | 1770 | BUG_ON(!req->r_dentry); |
38d46409 XL |
1771 | doutc(cl, " linking snapped dir %p to dn %p\n", in, |
1772 | req->r_dentry); | |
70db4f36 | 1773 | ceph_dir_clear_ordered(dir); |
86b58d13 | 1774 | ihold(in); |
2bf996ac YZ |
1775 | err = splice_dentry(&req->r_dentry, in); |
1776 | if (err < 0) | |
355da1eb | 1777 | goto done; |
543212b3 YZ |
1778 | } else if (rinfo->head->is_dentry && req->r_dentry) { |
1779 | /* parent inode is not locked, be carefull */ | |
cdde7c43 | 1780 | struct ceph_vino *ptvino = NULL; |
543212b3 YZ |
1781 | dvino.ino = le64_to_cpu(rinfo->diri.in->ino); |
1782 | dvino.snap = le64_to_cpu(rinfo->diri.in->snapid); | |
1783 | if (rinfo->head->is_target) { | |
1784 | tvino.ino = le64_to_cpu(rinfo->targeti.in->ino); | |
1785 | tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid); | |
1786 | ptvino = &tvino; | |
cdde7c43 | 1787 | } |
543212b3 YZ |
1788 | update_dentry_lease_careful(req->r_dentry, rinfo->dlease, |
1789 | session, req->r_request_started, | |
1790 | rinfo->dname, rinfo->dname_len, | |
1791 | &dvino, ptvino); | |
355da1eb | 1792 | } |
355da1eb | 1793 | done: |
38d46409 | 1794 | doutc(cl, "done err=%d\n", err); |
355da1eb SW |
1795 | return err; |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * Prepopulate our cache with readdir results, leases, etc. | |
1800 | */ | |
79f9f99a SW |
1801 | static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, |
1802 | struct ceph_mds_session *session) | |
1803 | { | |
1804 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | |
38d46409 | 1805 | struct ceph_client *cl = session->s_mdsc->fsc->client; |
79f9f99a SW |
1806 | int i, err = 0; |
1807 | ||
1808 | for (i = 0; i < rinfo->dir_nr; i++) { | |
2a5beea3 | 1809 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
79f9f99a SW |
1810 | struct ceph_vino vino; |
1811 | struct inode *in; | |
1812 | int rc; | |
1813 | ||
2a5beea3 YZ |
1814 | vino.ino = le64_to_cpu(rde->inode.in->ino); |
1815 | vino.snap = le64_to_cpu(rde->inode.in->snapid); | |
79f9f99a | 1816 | |
ec9595c0 | 1817 | in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL); |
79f9f99a SW |
1818 | if (IS_ERR(in)) { |
1819 | err = PTR_ERR(in); | |
38d46409 | 1820 | doutc(cl, "badness got %d\n", err); |
79f9f99a SW |
1821 | continue; |
1822 | } | |
966c7160 JL |
1823 | rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, |
1824 | -1, &req->r_caps_reservation); | |
79f9f99a | 1825 | if (rc < 0) { |
38d46409 XL |
1826 | pr_err_client(cl, "inode badness on %p got %d\n", in, |
1827 | rc); | |
79f9f99a | 1828 | err = rc; |
893e456b JL |
1829 | if (in->i_state & I_NEW) { |
1830 | ihold(in); | |
1831 | discard_new_inode(in); | |
1832 | } | |
1833 | } else if (in->i_state & I_NEW) { | |
1834 | unlock_new_inode(in); | |
79f9f99a | 1835 | } |
893e456b | 1836 | |
23c2c76e | 1837 | iput(in); |
79f9f99a SW |
1838 | } |
1839 | ||
1840 | return err; | |
1841 | } | |
1842 | ||
fdd4e158 YZ |
1843 | void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) |
1844 | { | |
1845 | if (ctl->page) { | |
1846 | kunmap(ctl->page); | |
09cbfeaf | 1847 | put_page(ctl->page); |
fdd4e158 YZ |
1848 | ctl->page = NULL; |
1849 | } | |
1850 | } | |
1851 | ||
1852 | static int fill_readdir_cache(struct inode *dir, struct dentry *dn, | |
1853 | struct ceph_readdir_cache_control *ctl, | |
1854 | struct ceph_mds_request *req) | |
1855 | { | |
38d46409 | 1856 | struct ceph_client *cl = ceph_inode_to_client(dir); |
fdd4e158 | 1857 | struct ceph_inode_info *ci = ceph_inode(dir); |
09cbfeaf | 1858 | unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); |
fdd4e158 YZ |
1859 | unsigned idx = ctl->index % nsize; |
1860 | pgoff_t pgoff = ctl->index / nsize; | |
1861 | ||
1862 | if (!ctl->page || pgoff != page_index(ctl->page)) { | |
1863 | ceph_readdir_cache_release(ctl); | |
af5e5eb5 YZ |
1864 | if (idx == 0) |
1865 | ctl->page = grab_cache_page(&dir->i_data, pgoff); | |
1866 | else | |
1867 | ctl->page = find_lock_page(&dir->i_data, pgoff); | |
fdd4e158 YZ |
1868 | if (!ctl->page) { |
1869 | ctl->index = -1; | |
af5e5eb5 | 1870 | return idx == 0 ? -ENOMEM : 0; |
fdd4e158 YZ |
1871 | } |
1872 | /* reading/filling the cache are serialized by | |
810313c5 | 1873 | * i_rwsem, no need to use page lock */ |
fdd4e158 YZ |
1874 | unlock_page(ctl->page); |
1875 | ctl->dentries = kmap(ctl->page); | |
af5e5eb5 | 1876 | if (idx == 0) |
09cbfeaf | 1877 | memset(ctl->dentries, 0, PAGE_SIZE); |
fdd4e158 YZ |
1878 | } |
1879 | ||
1880 | if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && | |
1881 | req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { | |
38d46409 | 1882 | doutc(cl, "dn %p idx %d\n", dn, ctl->index); |
fdd4e158 YZ |
1883 | ctl->dentries[idx] = dn; |
1884 | ctl->index++; | |
1885 | } else { | |
38d46409 | 1886 | doutc(cl, "disable readdir cache\n"); |
fdd4e158 YZ |
1887 | ctl->index = -1; |
1888 | } | |
1889 | return 0; | |
1890 | } | |
1891 | ||
355da1eb SW |
1892 | int ceph_readdir_prepopulate(struct ceph_mds_request *req, |
1893 | struct ceph_mds_session *session) | |
1894 | { | |
1895 | struct dentry *parent = req->r_dentry; | |
af9ffa6d XL |
1896 | struct inode *inode = d_inode(parent); |
1897 | struct ceph_inode_info *ci = ceph_inode(inode); | |
355da1eb | 1898 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
38d46409 | 1899 | struct ceph_client *cl = session->s_mdsc->fsc->client; |
355da1eb SW |
1900 | struct qstr dname; |
1901 | struct dentry *dn; | |
1902 | struct inode *in; | |
315f2408 | 1903 | int err = 0, skipped = 0, ret, i; |
0f51a983 | 1904 | u32 frag = le32_to_cpu(req->r_args.readdir.frag); |
f3c4ebe6 YZ |
1905 | u32 last_hash = 0; |
1906 | u32 fpos_offset; | |
fdd4e158 YZ |
1907 | struct ceph_readdir_cache_control cache_ctl = {}; |
1908 | ||
bc2de10d | 1909 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) |
fdd4e158 | 1910 | return readdir_prepopulate_inodes_only(req, session); |
81c6aea5 | 1911 | |
79162547 YZ |
1912 | if (rinfo->hash_order) { |
1913 | if (req->r_path2) { | |
1914 | last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, | |
1915 | req->r_path2, | |
1916 | strlen(req->r_path2)); | |
1917 | last_hash = ceph_frag_value(last_hash); | |
1918 | } else if (rinfo->offset_hash) { | |
1919 | /* mds understands offset_hash */ | |
1920 | WARN_ON_ONCE(req->r_readdir_offset != 2); | |
0f51a983 | 1921 | last_hash = le32_to_cpu(req->r_args.readdir.offset_hash); |
79162547 | 1922 | } |
f3c4ebe6 YZ |
1923 | } |
1924 | ||
81c6aea5 YZ |
1925 | if (rinfo->dir_dir && |
1926 | le32_to_cpu(rinfo->dir_dir->frag) != frag) { | |
38d46409 XL |
1927 | doutc(cl, "got new frag %x -> %x\n", frag, |
1928 | le32_to_cpu(rinfo->dir_dir->frag)); | |
81c6aea5 | 1929 | frag = le32_to_cpu(rinfo->dir_dir->frag); |
f3c4ebe6 | 1930 | if (!rinfo->hash_order) |
fdd4e158 | 1931 | req->r_readdir_offset = 2; |
81c6aea5 | 1932 | } |
355da1eb SW |
1933 | |
1934 | if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { | |
38d46409 XL |
1935 | doutc(cl, "%d items under SNAPDIR dn %p\n", |
1936 | rinfo->dir_nr, parent); | |
355da1eb | 1937 | } else { |
38d46409 | 1938 | doutc(cl, "%d items under dn %p\n", rinfo->dir_nr, parent); |
355da1eb | 1939 | if (rinfo->dir_dir) |
2b0143b5 | 1940 | ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); |
355da1eb | 1941 | |
8d45b911 YZ |
1942 | if (ceph_frag_is_leftmost(frag) && |
1943 | req->r_readdir_offset == 2 && | |
1944 | !(rinfo->hash_order && last_hash)) { | |
1945 | /* note dir version at start of readdir so we can | |
1946 | * tell if any dentries get dropped */ | |
1947 | req->r_dir_release_cnt = | |
1948 | atomic64_read(&ci->i_release_count); | |
1949 | req->r_dir_ordered_cnt = | |
1950 | atomic64_read(&ci->i_ordered_count); | |
1951 | req->r_readdir_cache_idx = 0; | |
1952 | } | |
fdd4e158 YZ |
1953 | } |
1954 | ||
1955 | cache_ctl.index = req->r_readdir_cache_idx; | |
f3c4ebe6 | 1956 | fpos_offset = req->r_readdir_offset; |
fdd4e158 | 1957 | |
86b58d13 | 1958 | /* FIXME: release caps/leases if error occurs */ |
355da1eb | 1959 | for (i = 0; i < rinfo->dir_nr; i++) { |
2a5beea3 | 1960 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
543212b3 | 1961 | struct ceph_vino tvino; |
355da1eb | 1962 | |
2a5beea3 YZ |
1963 | dname.name = rde->name; |
1964 | dname.len = rde->name_len; | |
8387ff25 | 1965 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
355da1eb | 1966 | |
f5d55f03 JL |
1967 | tvino.ino = le64_to_cpu(rde->inode.in->ino); |
1968 | tvino.snap = le64_to_cpu(rde->inode.in->snapid); | |
355da1eb | 1969 | |
f3c4ebe6 | 1970 | if (rinfo->hash_order) { |
af9ffa6d | 1971 | u32 hash = ceph_frag_value(rde->raw_hash); |
f3c4ebe6 YZ |
1972 | if (hash != last_hash) |
1973 | fpos_offset = 2; | |
1974 | last_hash = hash; | |
1975 | rde->offset = ceph_make_fpos(hash, fpos_offset++, true); | |
1976 | } else { | |
1977 | rde->offset = ceph_make_fpos(frag, fpos_offset++, false); | |
1978 | } | |
355da1eb SW |
1979 | |
1980 | retry_lookup: | |
1981 | dn = d_lookup(parent, &dname); | |
38d46409 XL |
1982 | doutc(cl, "d_lookup on parent=%p name=%.*s got %p\n", |
1983 | parent, dname.len, dname.name, dn); | |
355da1eb SW |
1984 | |
1985 | if (!dn) { | |
1986 | dn = d_alloc(parent, &dname); | |
38d46409 XL |
1987 | doutc(cl, "d_alloc %p '%.*s' = %p\n", parent, |
1988 | dname.len, dname.name, dn); | |
d37b1d99 | 1989 | if (!dn) { |
38d46409 | 1990 | doutc(cl, "d_alloc badness\n"); |
355da1eb SW |
1991 | err = -ENOMEM; |
1992 | goto out; | |
1993 | } | |
af9ffa6d XL |
1994 | if (rde->is_nokey) { |
1995 | spin_lock(&dn->d_lock); | |
1996 | dn->d_flags |= DCACHE_NOKEY_NAME; | |
1997 | spin_unlock(&dn->d_lock); | |
1998 | } | |
2b0143b5 | 1999 | } else if (d_really_is_positive(dn) && |
f5d55f03 JL |
2000 | (ceph_ino(d_inode(dn)) != tvino.ino || |
2001 | ceph_snap(d_inode(dn)) != tvino.snap)) { | |
5495c2d0 | 2002 | struct ceph_dentry_info *di = ceph_dentry(dn); |
38d46409 XL |
2003 | doutc(cl, " dn %p points to wrong inode %p\n", |
2004 | dn, d_inode(dn)); | |
5495c2d0 YZ |
2005 | |
2006 | spin_lock(&dn->d_lock); | |
2007 | if (di->offset > 0 && | |
2008 | di->lease_shared_gen == | |
2009 | atomic_read(&ci->i_shared_gen)) { | |
2010 | __ceph_dir_clear_ordered(ci); | |
2011 | di->offset = 0; | |
2012 | } | |
2013 | spin_unlock(&dn->d_lock); | |
2014 | ||
355da1eb SW |
2015 | d_delete(dn); |
2016 | dput(dn); | |
2017 | goto retry_lookup; | |
355da1eb SW |
2018 | } |
2019 | ||
355da1eb | 2020 | /* inode */ |
2b0143b5 DH |
2021 | if (d_really_is_positive(dn)) { |
2022 | in = d_inode(dn); | |
355da1eb | 2023 | } else { |
ec9595c0 | 2024 | in = ceph_get_inode(parent->d_sb, tvino, NULL); |
ac1f12ef | 2025 | if (IS_ERR(in)) { |
38d46409 | 2026 | doutc(cl, "new_inode badness\n"); |
2744c171 | 2027 | d_drop(dn); |
355da1eb | 2028 | dput(dn); |
ac1f12ef | 2029 | err = PTR_ERR(in); |
355da1eb SW |
2030 | goto out; |
2031 | } | |
355da1eb SW |
2032 | } |
2033 | ||
966c7160 JL |
2034 | ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session, |
2035 | -1, &req->r_caps_reservation); | |
fdd4e158 | 2036 | if (ret < 0) { |
38d46409 XL |
2037 | pr_err_client(cl, "badness on %p %llx.%llx\n", in, |
2038 | ceph_vinop(in)); | |
3e1d0452 | 2039 | if (d_really_is_negative(dn)) { |
893e456b JL |
2040 | if (in->i_state & I_NEW) { |
2041 | ihold(in); | |
2042 | discard_new_inode(in); | |
2043 | } | |
23c2c76e | 2044 | iput(in); |
3e1d0452 | 2045 | } |
86b58d13 | 2046 | d_drop(dn); |
fdd4e158 | 2047 | err = ret; |
d69ed05a | 2048 | goto next_item; |
355da1eb | 2049 | } |
893e456b JL |
2050 | if (in->i_state & I_NEW) |
2051 | unlock_new_inode(in); | |
86b58d13 | 2052 | |
2b0143b5 | 2053 | if (d_really_is_negative(dn)) { |
315f2408 | 2054 | if (ceph_security_xattr_deadlock(in)) { |
38d46409 XL |
2055 | doutc(cl, " skip splicing dn %p to inode %p" |
2056 | " (security xattr deadlock)\n", dn, in); | |
23c2c76e | 2057 | iput(in); |
315f2408 YZ |
2058 | skipped++; |
2059 | goto next_item; | |
2060 | } | |
2061 | ||
2bf996ac YZ |
2062 | err = splice_dentry(&dn, in); |
2063 | if (err < 0) | |
86b58d13 | 2064 | goto next_item; |
86b58d13 YZ |
2065 | } |
2066 | ||
f3c4ebe6 | 2067 | ceph_dentry(dn)->offset = rde->offset; |
86b58d13 | 2068 | |
543212b3 YZ |
2069 | update_dentry_lease(d_inode(parent), dn, |
2070 | rde->lease, req->r_session, | |
2071 | req->r_request_started); | |
fdd4e158 | 2072 | |
315f2408 | 2073 | if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { |
fdd4e158 YZ |
2074 | ret = fill_readdir_cache(d_inode(parent), dn, |
2075 | &cache_ctl, req); | |
2076 | if (ret < 0) | |
2077 | err = ret; | |
2078 | } | |
d69ed05a | 2079 | next_item: |
2bf996ac | 2080 | dput(dn); |
355da1eb | 2081 | } |
355da1eb | 2082 | out: |
315f2408 | 2083 | if (err == 0 && skipped == 0) { |
bc2de10d | 2084 | set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags); |
fdd4e158 YZ |
2085 | req->r_readdir_cache_idx = cache_ctl.index; |
2086 | } | |
2087 | ceph_readdir_cache_release(&cache_ctl); | |
38d46409 | 2088 | doutc(cl, "done\n"); |
355da1eb SW |
2089 | return err; |
2090 | } | |
2091 | ||
efb0ca76 | 2092 | bool ceph_inode_set_size(struct inode *inode, loff_t size) |
355da1eb | 2093 | { |
38d46409 | 2094 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb | 2095 | struct ceph_inode_info *ci = ceph_inode(inode); |
efb0ca76 | 2096 | bool ret; |
355da1eb | 2097 | |
be655596 | 2098 | spin_lock(&ci->i_ceph_lock); |
38d46409 | 2099 | doutc(cl, "set_size %p %llu -> %llu\n", inode, i_size_read(inode), size); |
99c88e69 | 2100 | i_size_write(inode, size); |
400e1286 | 2101 | ceph_fscache_update(inode); |
224a7542 | 2102 | inode->i_blocks = calc_inode_blocks(size); |
355da1eb | 2103 | |
efb0ca76 | 2104 | ret = __ceph_should_report_size(ci); |
355da1eb | 2105 | |
be655596 | 2106 | spin_unlock(&ci->i_ceph_lock); |
400e1286 | 2107 | |
355da1eb SW |
2108 | return ret; |
2109 | } | |
2110 | ||
64f28c62 | 2111 | void ceph_queue_inode_work(struct inode *inode, int work_bit) |
355da1eb | 2112 | { |
5995d90d | 2113 | struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); |
38d46409 | 2114 | struct ceph_client *cl = fsc->client; |
1cf89a8d | 2115 | struct ceph_inode_info *ci = ceph_inode(inode); |
64f28c62 | 2116 | set_bit(work_bit, &ci->i_work_mask); |
1cf89a8d YZ |
2117 | |
2118 | ihold(inode); | |
64f28c62 | 2119 | if (queue_work(fsc->inode_wq, &ci->i_work)) { |
38d46409 XL |
2120 | doutc(cl, "%p %llx.%llx mask=%lx\n", inode, |
2121 | ceph_vinop(inode), ci->i_work_mask); | |
1cf89a8d | 2122 | } else { |
38d46409 XL |
2123 | doutc(cl, "%p %llx.%llx already queued, mask=%lx\n", |
2124 | inode, ceph_vinop(inode), ci->i_work_mask); | |
1cf89a8d YZ |
2125 | iput(inode); |
2126 | } | |
2127 | } | |
2128 | ||
2129 | static void ceph_do_invalidate_pages(struct inode *inode) | |
2130 | { | |
38d46409 | 2131 | struct ceph_client *cl = ceph_inode_to_client(inode); |
1cf89a8d | 2132 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb SW |
2133 | u32 orig_gen; |
2134 | int check = 0; | |
2135 | ||
400e1286 JL |
2136 | ceph_fscache_invalidate(inode, false); |
2137 | ||
b0d7c223 | 2138 | mutex_lock(&ci->i_truncate_mutex); |
6c93df5d | 2139 | |
5d6451b1 | 2140 | if (ceph_inode_is_shutdown(inode)) { |
38d46409 XL |
2141 | pr_warn_ratelimited_client(cl, |
2142 | "%p %llx.%llx is shut down\n", inode, | |
2143 | ceph_vinop(inode)); | |
6c93df5d YZ |
2144 | mapping_set_error(inode->i_mapping, -EIO); |
2145 | truncate_pagecache(inode, 0); | |
2146 | mutex_unlock(&ci->i_truncate_mutex); | |
2147 | goto out; | |
2148 | } | |
2149 | ||
be655596 | 2150 | spin_lock(&ci->i_ceph_lock); |
38d46409 XL |
2151 | doutc(cl, "%p %llx.%llx gen %d revoking %d\n", inode, |
2152 | ceph_vinop(inode), ci->i_rdcache_gen, ci->i_rdcache_revoking); | |
cd045cb4 | 2153 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
9563f88c YZ |
2154 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
2155 | check = 1; | |
be655596 | 2156 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 2157 | mutex_unlock(&ci->i_truncate_mutex); |
355da1eb SW |
2158 | goto out; |
2159 | } | |
2160 | orig_gen = ci->i_rdcache_gen; | |
be655596 | 2161 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 2162 | |
9abd4db7 | 2163 | if (invalidate_inode_pages2(inode->i_mapping) < 0) { |
38d46409 XL |
2164 | pr_err_client(cl, "invalidate_inode_pages2 %llx.%llx failed\n", |
2165 | ceph_vinop(inode)); | |
9abd4db7 | 2166 | } |
355da1eb | 2167 | |
be655596 | 2168 | spin_lock(&ci->i_ceph_lock); |
cd045cb4 SW |
2169 | if (orig_gen == ci->i_rdcache_gen && |
2170 | orig_gen == ci->i_rdcache_revoking) { | |
38d46409 XL |
2171 | doutc(cl, "%p %llx.%llx gen %d successful\n", inode, |
2172 | ceph_vinop(inode), ci->i_rdcache_gen); | |
cd045cb4 | 2173 | ci->i_rdcache_revoking--; |
355da1eb SW |
2174 | check = 1; |
2175 | } else { | |
38d46409 XL |
2176 | doutc(cl, "%p %llx.%llx gen %d raced, now %d revoking %d\n", |
2177 | inode, ceph_vinop(inode), orig_gen, ci->i_rdcache_gen, | |
2178 | ci->i_rdcache_revoking); | |
9563f88c YZ |
2179 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
2180 | check = 1; | |
355da1eb | 2181 | } |
be655596 | 2182 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 2183 | mutex_unlock(&ci->i_truncate_mutex); |
9563f88c | 2184 | out: |
355da1eb | 2185 | if (check) |
e4b731cc | 2186 | ceph_check_caps(ci, 0); |
3c6f6b79 SW |
2187 | } |
2188 | ||
355da1eb | 2189 | /* |
355da1eb SW |
2190 | * Make sure any pending truncation is applied before doing anything |
2191 | * that may depend on it. | |
2192 | */ | |
b415bf4f | 2193 | void __ceph_do_pending_vmtruncate(struct inode *inode) |
355da1eb | 2194 | { |
38d46409 | 2195 | struct ceph_client *cl = ceph_inode_to_client(inode); |
355da1eb SW |
2196 | struct ceph_inode_info *ci = ceph_inode(inode); |
2197 | u64 to; | |
a85f50b6 | 2198 | int wrbuffer_refs, finish = 0; |
355da1eb | 2199 | |
b0d7c223 | 2200 | mutex_lock(&ci->i_truncate_mutex); |
355da1eb | 2201 | retry: |
be655596 | 2202 | spin_lock(&ci->i_ceph_lock); |
355da1eb | 2203 | if (ci->i_truncate_pending == 0) { |
38d46409 XL |
2204 | doutc(cl, "%p %llx.%llx none pending\n", inode, |
2205 | ceph_vinop(inode)); | |
be655596 | 2206 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 2207 | mutex_unlock(&ci->i_truncate_mutex); |
355da1eb SW |
2208 | return; |
2209 | } | |
2210 | ||
2211 | /* | |
2212 | * make sure any dirty snapped pages are flushed before we | |
2213 | * possibly truncate them.. so write AND block! | |
2214 | */ | |
2215 | if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { | |
c8fd0d37 | 2216 | spin_unlock(&ci->i_ceph_lock); |
38d46409 XL |
2217 | doutc(cl, "%p %llx.%llx flushing snaps first\n", inode, |
2218 | ceph_vinop(inode)); | |
355da1eb SW |
2219 | filemap_write_and_wait_range(&inode->i_data, 0, |
2220 | inode->i_sb->s_maxbytes); | |
2221 | goto retry; | |
2222 | } | |
2223 | ||
b0d7c223 YZ |
2224 | /* there should be no reader or writer */ |
2225 | WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); | |
2226 | ||
5c64737d | 2227 | to = ci->i_truncate_pagecache_size; |
355da1eb | 2228 | wrbuffer_refs = ci->i_wrbuffer_ref; |
38d46409 XL |
2229 | doutc(cl, "%p %llx.%llx (%d) to %lld\n", inode, ceph_vinop(inode), |
2230 | ci->i_truncate_pending, to); | |
be655596 | 2231 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 2232 | |
400e1286 | 2233 | ceph_fscache_resize(inode, to); |
4e217b5d | 2234 | truncate_pagecache(inode, to); |
355da1eb | 2235 | |
be655596 | 2236 | spin_lock(&ci->i_ceph_lock); |
5c64737d | 2237 | if (to == ci->i_truncate_pagecache_size) { |
a85f50b6 YZ |
2238 | ci->i_truncate_pending = 0; |
2239 | finish = 1; | |
2240 | } | |
be655596 | 2241 | spin_unlock(&ci->i_ceph_lock); |
a85f50b6 YZ |
2242 | if (!finish) |
2243 | goto retry; | |
355da1eb | 2244 | |
b0d7c223 YZ |
2245 | mutex_unlock(&ci->i_truncate_mutex); |
2246 | ||
355da1eb | 2247 | if (wrbuffer_refs == 0) |
e4b731cc | 2248 | ceph_check_caps(ci, 0); |
a85f50b6 YZ |
2249 | |
2250 | wake_up_all(&ci->i_cap_wq); | |
355da1eb SW |
2251 | } |
2252 | ||
1cf89a8d YZ |
2253 | static void ceph_inode_work(struct work_struct *work) |
2254 | { | |
2255 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | |
2256 | i_work); | |
874c8ca1 | 2257 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 2258 | struct ceph_client *cl = ceph_inode_to_client(inode); |
1cf89a8d YZ |
2259 | |
2260 | if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) { | |
38d46409 | 2261 | doutc(cl, "writeback %p %llx.%llx\n", inode, ceph_vinop(inode)); |
1cf89a8d YZ |
2262 | filemap_fdatawrite(&inode->i_data); |
2263 | } | |
2264 | if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask)) | |
2265 | ceph_do_invalidate_pages(inode); | |
2266 | ||
2267 | if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask)) | |
2268 | __ceph_do_pending_vmtruncate(inode); | |
2269 | ||
a8810cdc | 2270 | if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask)) |
e4b731cc | 2271 | ceph_check_caps(ci, 0); |
a8810cdc JL |
2272 | |
2273 | if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask)) | |
2274 | ceph_flush_snaps(ci, NULL); | |
2275 | ||
1cf89a8d YZ |
2276 | iput(inode); |
2277 | } | |
2278 | ||
79f2f6ad JL |
2279 | static const char *ceph_encrypted_get_link(struct dentry *dentry, |
2280 | struct inode *inode, | |
2281 | struct delayed_call *done) | |
2282 | { | |
2283 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2284 | ||
2285 | if (!dentry) | |
2286 | return ERR_PTR(-ECHILD); | |
2287 | ||
2288 | return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode), | |
2289 | done); | |
2290 | } | |
2291 | ||
2292 | static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap, | |
2293 | const struct path *path, | |
2294 | struct kstat *stat, u32 request_mask, | |
2295 | unsigned int query_flags) | |
2296 | { | |
2297 | int ret; | |
2298 | ||
2299 | ret = ceph_getattr(idmap, path, stat, request_mask, query_flags); | |
2300 | if (ret) | |
2301 | return ret; | |
2302 | return fscrypt_symlink_getattr(path, stat); | |
2303 | } | |
2304 | ||
355da1eb SW |
2305 | /* |
2306 | * symlinks | |
2307 | */ | |
355da1eb | 2308 | static const struct inode_operations ceph_symlink_iops = { |
6b255391 | 2309 | .get_link = simple_get_link, |
0b932672 YZ |
2310 | .setattr = ceph_setattr, |
2311 | .getattr = ceph_getattr, | |
0b932672 | 2312 | .listxattr = ceph_listxattr, |
355da1eb SW |
2313 | }; |
2314 | ||
79f2f6ad JL |
2315 | static const struct inode_operations ceph_encrypted_symlink_iops = { |
2316 | .get_link = ceph_encrypted_get_link, | |
2317 | .setattr = ceph_setattr, | |
2318 | .getattr = ceph_encrypted_symlink_getattr, | |
2319 | .listxattr = ceph_listxattr, | |
2320 | }; | |
2321 | ||
5c64737d XL |
2322 | /* |
2323 | * Transfer the encrypted last block to the MDS and the MDS | |
2324 | * will help update it when truncating a smaller size. | |
2325 | * | |
2326 | * We don't support a PAGE_SIZE that is smaller than the | |
2327 | * CEPH_FSCRYPT_BLOCK_SIZE. | |
2328 | */ | |
2329 | static int fill_fscrypt_truncate(struct inode *inode, | |
2330 | struct ceph_mds_request *req, | |
2331 | struct iattr *attr) | |
2332 | { | |
38d46409 | 2333 | struct ceph_client *cl = ceph_inode_to_client(inode); |
5c64737d XL |
2334 | struct ceph_inode_info *ci = ceph_inode(inode); |
2335 | int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE; | |
2336 | loff_t pos, orig_pos = round_down(attr->ia_size, | |
2337 | CEPH_FSCRYPT_BLOCK_SIZE); | |
2338 | u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT; | |
2339 | struct ceph_pagelist *pagelist = NULL; | |
2340 | struct kvec iov = {0}; | |
2341 | struct iov_iter iter; | |
2342 | struct page *page = NULL; | |
2343 | struct ceph_fscrypt_truncate_size_header header; | |
2344 | int retry_op = 0; | |
2345 | int len = CEPH_FSCRYPT_BLOCK_SIZE; | |
2346 | loff_t i_size = i_size_read(inode); | |
2347 | int got, ret, issued; | |
2348 | u64 objver; | |
2349 | ||
2350 | ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got); | |
2351 | if (ret < 0) | |
2352 | return ret; | |
2353 | ||
2354 | issued = __ceph_caps_issued(ci, NULL); | |
2355 | ||
38d46409 XL |
2356 | doutc(cl, "size %lld -> %lld got cap refs on %s, issued %s\n", |
2357 | i_size, attr->ia_size, ceph_cap_string(got), | |
2358 | ceph_cap_string(issued)); | |
5c64737d XL |
2359 | |
2360 | /* Try to writeback the dirty pagecaches */ | |
2361 | if (issued & (CEPH_CAP_FILE_BUFFER)) { | |
2362 | loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SHIFT - 1; | |
2363 | ||
2364 | ret = filemap_write_and_wait_range(inode->i_mapping, | |
2365 | orig_pos, lend); | |
2366 | if (ret < 0) | |
2367 | goto out; | |
2368 | } | |
2369 | ||
2370 | page = __page_cache_alloc(GFP_KERNEL); | |
2371 | if (page == NULL) { | |
2372 | ret = -ENOMEM; | |
2373 | goto out; | |
2374 | } | |
2375 | ||
2376 | pagelist = ceph_pagelist_alloc(GFP_KERNEL); | |
2377 | if (!pagelist) { | |
2378 | ret = -ENOMEM; | |
2379 | goto out; | |
2380 | } | |
2381 | ||
2382 | iov.iov_base = kmap_local_page(page); | |
2383 | iov.iov_len = len; | |
2384 | iov_iter_kvec(&iter, READ, &iov, 1, len); | |
2385 | ||
2386 | pos = orig_pos; | |
2387 | ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver); | |
2388 | if (ret < 0) | |
2389 | goto out; | |
2390 | ||
2391 | /* Insert the header first */ | |
2392 | header.ver = 1; | |
2393 | header.compat = 1; | |
2394 | header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode)); | |
2395 | ||
2396 | /* | |
2397 | * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE, | |
2398 | * because in MDS it may need this to do the truncate. | |
2399 | */ | |
2400 | header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE); | |
2401 | ||
2402 | /* | |
2403 | * If we hit a hole here, we should just skip filling | |
2404 | * the fscrypt for the request, because once the fscrypt | |
2405 | * is enabled, the file will be split into many blocks | |
2406 | * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there | |
2407 | * has a hole, the hole size should be multiple of block | |
2408 | * size. | |
2409 | * | |
2410 | * If the Rados object doesn't exist, it will be set to 0. | |
2411 | */ | |
2412 | if (!objver) { | |
38d46409 | 2413 | doutc(cl, "hit hole, ppos %lld < size %lld\n", pos, i_size); |
5c64737d XL |
2414 | |
2415 | header.data_len = cpu_to_le32(8 + 8 + 4); | |
2416 | header.file_offset = 0; | |
2417 | ret = 0; | |
2418 | } else { | |
2419 | header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE); | |
2420 | header.file_offset = cpu_to_le64(orig_pos); | |
2421 | ||
38d46409 XL |
2422 | doutc(cl, "encrypt block boff/bsize %d/%lu\n", boff, |
2423 | CEPH_FSCRYPT_BLOCK_SIZE); | |
295fc4aa | 2424 | |
5c64737d XL |
2425 | /* truncate and zero out the extra contents for the last block */ |
2426 | memset(iov.iov_base + boff, 0, PAGE_SIZE - boff); | |
2427 | ||
2428 | /* encrypt the last block */ | |
2429 | ret = ceph_fscrypt_encrypt_block_inplace(inode, page, | |
2430 | CEPH_FSCRYPT_BLOCK_SIZE, | |
2431 | 0, block, | |
2432 | GFP_KERNEL); | |
2433 | if (ret) | |
2434 | goto out; | |
2435 | } | |
2436 | ||
2437 | /* Insert the header */ | |
2438 | ret = ceph_pagelist_append(pagelist, &header, sizeof(header)); | |
2439 | if (ret) | |
2440 | goto out; | |
2441 | ||
2442 | if (header.block_size) { | |
2443 | /* Append the last block contents to pagelist */ | |
2444 | ret = ceph_pagelist_append(pagelist, iov.iov_base, | |
2445 | CEPH_FSCRYPT_BLOCK_SIZE); | |
2446 | if (ret) | |
2447 | goto out; | |
2448 | } | |
2449 | req->r_pagelist = pagelist; | |
2450 | out: | |
38d46409 XL |
2451 | doutc(cl, "%p %llx.%llx size dropping cap refs on %s\n", inode, |
2452 | ceph_vinop(inode), ceph_cap_string(got)); | |
5c64737d XL |
2453 | ceph_put_cap_refs(ci, got); |
2454 | if (iov.iov_base) | |
2455 | kunmap_local(iov.iov_base); | |
2456 | if (page) | |
2457 | __free_pages(page, 0); | |
2458 | if (ret && pagelist) | |
2459 | ceph_pagelist_release(pagelist); | |
2460 | return ret; | |
2461 | } | |
2462 | ||
79c66a0c AM |
2463 | int __ceph_setattr(struct mnt_idmap *idmap, struct inode *inode, |
2464 | struct iattr *attr, struct ceph_iattr *cia) | |
355da1eb | 2465 | { |
355da1eb | 2466 | struct ceph_inode_info *ci = ceph_inode(inode); |
c62498d7 | 2467 | unsigned int ia_valid = attr->ia_valid; |
355da1eb | 2468 | struct ceph_mds_request *req; |
5995d90d | 2469 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 2470 | struct ceph_client *cl = ceph_inode_to_client(inode); |
f66fd9f0 | 2471 | struct ceph_cap_flush *prealloc_cf; |
5c64737d | 2472 | loff_t isize = i_size_read(inode); |
355da1eb SW |
2473 | int issued; |
2474 | int release = 0, dirtied = 0; | |
2475 | int mask = 0; | |
2476 | int err = 0; | |
fca65b4a | 2477 | int inode_dirty_flags = 0; |
604d1b02 | 2478 | bool lock_snap_rwsem = false; |
5c64737d XL |
2479 | bool fill_fscrypt; |
2480 | int truncate_retry = 20; /* The RMW will take around 50ms */ | |
355da1eb | 2481 | |
5c64737d | 2482 | retry: |
f66fd9f0 YZ |
2483 | prealloc_cf = ceph_alloc_cap_flush(); |
2484 | if (!prealloc_cf) | |
2485 | return -ENOMEM; | |
2486 | ||
355da1eb SW |
2487 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, |
2488 | USE_AUTH_MDS); | |
f66fd9f0 YZ |
2489 | if (IS_ERR(req)) { |
2490 | ceph_free_cap_flush(prealloc_cf); | |
355da1eb | 2491 | return PTR_ERR(req); |
f66fd9f0 | 2492 | } |
355da1eb | 2493 | |
5c64737d | 2494 | fill_fscrypt = false; |
be655596 | 2495 | spin_lock(&ci->i_ceph_lock); |
355da1eb | 2496 | issued = __ceph_caps_issued(ci, NULL); |
604d1b02 YZ |
2497 | |
2498 | if (!ci->i_head_snapc && | |
2499 | (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { | |
2500 | lock_snap_rwsem = true; | |
2501 | if (!down_read_trylock(&mdsc->snap_rwsem)) { | |
2502 | spin_unlock(&ci->i_ceph_lock); | |
2503 | down_read(&mdsc->snap_rwsem); | |
2504 | spin_lock(&ci->i_ceph_lock); | |
2505 | issued = __ceph_caps_issued(ci, NULL); | |
2506 | } | |
2507 | } | |
2508 | ||
38d46409 XL |
2509 | doutc(cl, "%p %llx.%llx issued %s\n", inode, ceph_vinop(inode), |
2510 | ceph_cap_string(issued)); | |
2d332d5b JL |
2511 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
2512 | if (cia && cia->fscrypt_auth) { | |
2513 | u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth); | |
2514 | ||
2515 | if (len > sizeof(*cia->fscrypt_auth)) { | |
2516 | err = -EINVAL; | |
2517 | spin_unlock(&ci->i_ceph_lock); | |
2518 | goto out; | |
2519 | } | |
2520 | ||
38d46409 XL |
2521 | doutc(cl, "%p %llx.%llx fscrypt_auth len %u to %u)\n", inode, |
2522 | ceph_vinop(inode), ci->fscrypt_auth_len, len); | |
2d332d5b JL |
2523 | |
2524 | /* It should never be re-set once set */ | |
2525 | WARN_ON_ONCE(ci->fscrypt_auth); | |
2526 | ||
2527 | if (issued & CEPH_CAP_AUTH_EXCL) { | |
2528 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
2529 | kfree(ci->fscrypt_auth); | |
2530 | ci->fscrypt_auth = (u8 *)cia->fscrypt_auth; | |
2531 | ci->fscrypt_auth_len = len; | |
2532 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
2533 | ci->fscrypt_auth_len != len || | |
2534 | memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) { | |
2535 | req->r_fscrypt_auth = cia->fscrypt_auth; | |
2536 | mask |= CEPH_SETATTR_FSCRYPT_AUTH; | |
2537 | release |= CEPH_CAP_AUTH_SHARED; | |
2538 | } | |
2539 | cia->fscrypt_auth = NULL; | |
2540 | } | |
2541 | #else | |
2542 | if (cia && cia->fscrypt_auth) { | |
2543 | err = -EINVAL; | |
2544 | spin_unlock(&ci->i_ceph_lock); | |
2545 | goto out; | |
2546 | } | |
2547 | #endif /* CONFIG_FS_ENCRYPTION */ | |
355da1eb SW |
2548 | |
2549 | if (ia_valid & ATTR_UID) { | |
a04aff25 CB |
2550 | kuid_t fsuid = from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid); |
2551 | ||
38d46409 XL |
2552 | doutc(cl, "%p %llx.%llx uid %d -> %d\n", inode, |
2553 | ceph_vinop(inode), | |
2554 | from_kuid(&init_user_ns, inode->i_uid), | |
2555 | from_kuid(&init_user_ns, attr->ia_uid)); | |
355da1eb | 2556 | if (issued & CEPH_CAP_AUTH_EXCL) { |
a04aff25 | 2557 | inode->i_uid = fsuid; |
355da1eb SW |
2558 | dirtied |= CEPH_CAP_AUTH_EXCL; |
2559 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
a04aff25 | 2560 | !uid_eq(fsuid, inode->i_uid)) { |
ab871b90 | 2561 | req->r_args.setattr.uid = cpu_to_le32( |
a04aff25 | 2562 | from_kuid(&init_user_ns, fsuid)); |
355da1eb SW |
2563 | mask |= CEPH_SETATTR_UID; |
2564 | release |= CEPH_CAP_AUTH_SHARED; | |
2565 | } | |
2566 | } | |
2567 | if (ia_valid & ATTR_GID) { | |
a04aff25 CB |
2568 | kgid_t fsgid = from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid); |
2569 | ||
38d46409 XL |
2570 | doutc(cl, "%p %llx.%llx gid %d -> %d\n", inode, |
2571 | ceph_vinop(inode), | |
2572 | from_kgid(&init_user_ns, inode->i_gid), | |
2573 | from_kgid(&init_user_ns, attr->ia_gid)); | |
355da1eb | 2574 | if (issued & CEPH_CAP_AUTH_EXCL) { |
a04aff25 | 2575 | inode->i_gid = fsgid; |
355da1eb SW |
2576 | dirtied |= CEPH_CAP_AUTH_EXCL; |
2577 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
a04aff25 | 2578 | !gid_eq(fsgid, inode->i_gid)) { |
ab871b90 | 2579 | req->r_args.setattr.gid = cpu_to_le32( |
a04aff25 | 2580 | from_kgid(&init_user_ns, fsgid)); |
355da1eb SW |
2581 | mask |= CEPH_SETATTR_GID; |
2582 | release |= CEPH_CAP_AUTH_SHARED; | |
2583 | } | |
2584 | } | |
2585 | if (ia_valid & ATTR_MODE) { | |
38d46409 XL |
2586 | doutc(cl, "%p %llx.%llx mode 0%o -> 0%o\n", inode, |
2587 | ceph_vinop(inode), inode->i_mode, attr->ia_mode); | |
355da1eb SW |
2588 | if (issued & CEPH_CAP_AUTH_EXCL) { |
2589 | inode->i_mode = attr->ia_mode; | |
2590 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
2591 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
2592 | attr->ia_mode != inode->i_mode) { | |
7221fe4c | 2593 | inode->i_mode = attr->ia_mode; |
355da1eb SW |
2594 | req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); |
2595 | mask |= CEPH_SETATTR_MODE; | |
2596 | release |= CEPH_CAP_AUTH_SHARED; | |
2597 | } | |
2598 | } | |
2599 | ||
2600 | if (ia_valid & ATTR_ATIME) { | |
c453bdb5 JL |
2601 | struct timespec64 atime = inode_get_atime(inode); |
2602 | ||
e21165bf LT |
2603 | doutc(cl, "%p %llx.%llx atime %lld.%09ld -> %lld.%09ld\n", |
2604 | inode, ceph_vinop(inode), | |
2605 | atime.tv_sec, atime.tv_nsec, | |
2606 | attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); | |
355da1eb SW |
2607 | if (issued & CEPH_CAP_FILE_EXCL) { |
2608 | ci->i_time_warp_seq++; | |
c453bdb5 | 2609 | inode_set_atime_to_ts(inode, attr->ia_atime); |
355da1eb SW |
2610 | dirtied |= CEPH_CAP_FILE_EXCL; |
2611 | } else if ((issued & CEPH_CAP_FILE_WR) && | |
c453bdb5 JL |
2612 | timespec64_compare(&atime, |
2613 | &attr->ia_atime) < 0) { | |
2614 | inode_set_atime_to_ts(inode, attr->ia_atime); | |
355da1eb SW |
2615 | dirtied |= CEPH_CAP_FILE_WR; |
2616 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | |
c453bdb5 | 2617 | !timespec64_equal(&atime, &attr->ia_atime)) { |
9bbeab41 AB |
2618 | ceph_encode_timespec64(&req->r_args.setattr.atime, |
2619 | &attr->ia_atime); | |
355da1eb | 2620 | mask |= CEPH_SETATTR_ATIME; |
be70489e YZ |
2621 | release |= CEPH_CAP_FILE_SHARED | |
2622 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; | |
355da1eb SW |
2623 | } |
2624 | } | |
c62498d7 | 2625 | if (ia_valid & ATTR_SIZE) { |
38d46409 XL |
2626 | doutc(cl, "%p %llx.%llx size %lld -> %lld\n", inode, |
2627 | ceph_vinop(inode), isize, attr->ia_size); | |
5c64737d XL |
2628 | /* |
2629 | * Only when the new size is smaller and not aligned to | |
2630 | * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed. | |
2631 | */ | |
2632 | if (IS_ENCRYPTED(inode) && attr->ia_size < isize && | |
2633 | (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) { | |
2634 | mask |= CEPH_SETATTR_SIZE; | |
2635 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | | |
2636 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; | |
2637 | set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags); | |
2638 | mask |= CEPH_SETATTR_FSCRYPT_FILE; | |
2639 | req->r_args.setattr.size = | |
2640 | cpu_to_le64(round_up(attr->ia_size, | |
2641 | CEPH_FSCRYPT_BLOCK_SIZE)); | |
2642 | req->r_args.setattr.old_size = | |
2643 | cpu_to_le64(round_up(isize, | |
2644 | CEPH_FSCRYPT_BLOCK_SIZE)); | |
2645 | req->r_fscrypt_file = attr->ia_size; | |
2646 | fill_fscrypt = true; | |
2647 | } else if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) { | |
e90334e8 XL |
2648 | if (attr->ia_size > isize) { |
2649 | i_size_write(inode, attr->ia_size); | |
2650 | inode->i_blocks = calc_inode_blocks(attr->ia_size); | |
2651 | ci->i_reported_size = attr->ia_size; | |
2652 | dirtied |= CEPH_CAP_FILE_EXCL; | |
2653 | ia_valid |= ATTR_MTIME; | |
2654 | } | |
c62498d7 | 2655 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || |
2d6795fb | 2656 | attr->ia_size != isize) { |
c62498d7 JL |
2657 | mask |= CEPH_SETATTR_SIZE; |
2658 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | | |
2659 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; | |
16be62fc JL |
2660 | if (IS_ENCRYPTED(inode) && attr->ia_size) { |
2661 | set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags); | |
2662 | mask |= CEPH_SETATTR_FSCRYPT_FILE; | |
2663 | req->r_args.setattr.size = | |
2664 | cpu_to_le64(round_up(attr->ia_size, | |
2665 | CEPH_FSCRYPT_BLOCK_SIZE)); | |
2666 | req->r_args.setattr.old_size = | |
2667 | cpu_to_le64(round_up(isize, | |
2668 | CEPH_FSCRYPT_BLOCK_SIZE)); | |
2669 | req->r_fscrypt_file = attr->ia_size; | |
16be62fc JL |
2670 | } else { |
2671 | req->r_args.setattr.size = cpu_to_le64(attr->ia_size); | |
2672 | req->r_args.setattr.old_size = cpu_to_le64(isize); | |
2673 | req->r_fscrypt_file = 0; | |
2674 | } | |
c62498d7 JL |
2675 | } |
2676 | } | |
355da1eb | 2677 | if (ia_valid & ATTR_MTIME) { |
c453bdb5 JL |
2678 | struct timespec64 mtime = inode_get_mtime(inode); |
2679 | ||
e21165bf LT |
2680 | doutc(cl, "%p %llx.%llx mtime %lld.%09ld -> %lld.%09ld\n", |
2681 | inode, ceph_vinop(inode), | |
2682 | mtime.tv_sec, mtime.tv_nsec, | |
2683 | attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); | |
355da1eb SW |
2684 | if (issued & CEPH_CAP_FILE_EXCL) { |
2685 | ci->i_time_warp_seq++; | |
c453bdb5 | 2686 | inode_set_mtime_to_ts(inode, attr->ia_mtime); |
355da1eb SW |
2687 | dirtied |= CEPH_CAP_FILE_EXCL; |
2688 | } else if ((issued & CEPH_CAP_FILE_WR) && | |
c453bdb5 JL |
2689 | timespec64_compare(&mtime, &attr->ia_mtime) < 0) { |
2690 | inode_set_mtime_to_ts(inode, attr->ia_mtime); | |
355da1eb SW |
2691 | dirtied |= CEPH_CAP_FILE_WR; |
2692 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | |
c453bdb5 | 2693 | !timespec64_equal(&mtime, &attr->ia_mtime)) { |
9bbeab41 AB |
2694 | ceph_encode_timespec64(&req->r_args.setattr.mtime, |
2695 | &attr->ia_mtime); | |
355da1eb | 2696 | mask |= CEPH_SETATTR_MTIME; |
be70489e YZ |
2697 | release |= CEPH_CAP_FILE_SHARED | |
2698 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR; | |
355da1eb SW |
2699 | } |
2700 | } | |
355da1eb SW |
2701 | |
2702 | /* these do nothing */ | |
2703 | if (ia_valid & ATTR_CTIME) { | |
2704 | bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| | |
2705 | ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; | |
e21165bf LT |
2706 | doutc(cl, "%p %llx.%llx ctime %lld.%09ld -> %lld.%09ld (%s)\n", |
2707 | inode, ceph_vinop(inode), | |
2708 | inode_get_ctime_sec(inode), | |
2709 | inode_get_ctime_nsec(inode), | |
38d46409 XL |
2710 | attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, |
2711 | only ? "ctime only" : "ignored"); | |
355da1eb SW |
2712 | if (only) { |
2713 | /* | |
2714 | * if kernel wants to dirty ctime but nothing else, | |
2715 | * we need to choose a cap to dirty under, or do | |
2716 | * a almost-no-op setattr | |
2717 | */ | |
2718 | if (issued & CEPH_CAP_AUTH_EXCL) | |
2719 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
2720 | else if (issued & CEPH_CAP_FILE_EXCL) | |
2721 | dirtied |= CEPH_CAP_FILE_EXCL; | |
2722 | else if (issued & CEPH_CAP_XATTR_EXCL) | |
2723 | dirtied |= CEPH_CAP_XATTR_EXCL; | |
2724 | else | |
2725 | mask |= CEPH_SETATTR_CTIME; | |
2726 | } | |
2727 | } | |
2728 | if (ia_valid & ATTR_FILE) | |
38d46409 XL |
2729 | doutc(cl, "%p %llx.%llx ATTR_FILE ... hrm!\n", inode, |
2730 | ceph_vinop(inode)); | |
355da1eb SW |
2731 | |
2732 | if (dirtied) { | |
f66fd9f0 YZ |
2733 | inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, |
2734 | &prealloc_cf); | |
7795aef0 | 2735 | inode_set_ctime_to_ts(inode, attr->ia_ctime); |
b4b924c7 | 2736 | inode_inc_iversion_raw(inode); |
355da1eb SW |
2737 | } |
2738 | ||
2739 | release &= issued; | |
be655596 | 2740 | spin_unlock(&ci->i_ceph_lock); |
5c64737d | 2741 | if (lock_snap_rwsem) { |
604d1b02 | 2742 | up_read(&mdsc->snap_rwsem); |
5c64737d XL |
2743 | lock_snap_rwsem = false; |
2744 | } | |
355da1eb | 2745 | |
fca65b4a SW |
2746 | if (inode_dirty_flags) |
2747 | __mark_inode_dirty(inode, inode_dirty_flags); | |
2748 | ||
355da1eb | 2749 | if (mask) { |
70b666c3 SW |
2750 | req->r_inode = inode; |
2751 | ihold(inode); | |
355da1eb SW |
2752 | req->r_inode_drop = release; |
2753 | req->r_args.setattr.mask = cpu_to_le32(mask); | |
2754 | req->r_num_caps = 1; | |
0ed1e90a | 2755 | req->r_stamp = attr->ia_ctime; |
5c64737d XL |
2756 | if (fill_fscrypt) { |
2757 | err = fill_fscrypt_truncate(inode, req, attr); | |
2758 | if (err) | |
2759 | goto out; | |
2760 | } | |
2761 | ||
2762 | /* | |
2763 | * The truncate request will return -EAGAIN when the | |
2764 | * last block has been updated just before the MDS | |
2765 | * successfully gets the xlock for the FILE lock. To | |
2766 | * avoid corrupting the file contents we need to retry | |
2767 | * it. | |
2768 | */ | |
752c8bdc | 2769 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
5c64737d | 2770 | if (err == -EAGAIN && truncate_retry--) { |
38d46409 XL |
2771 | doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote), retry it!\n", |
2772 | inode, ceph_vinop(inode), err, | |
2773 | ceph_cap_string(dirtied), mask); | |
5c64737d XL |
2774 | ceph_mdsc_put_request(req); |
2775 | ceph_free_cap_flush(prealloc_cf); | |
2776 | goto retry; | |
2777 | } | |
355da1eb | 2778 | } |
2d332d5b | 2779 | out: |
38d46409 XL |
2780 | doutc(cl, "%p %llx.%llx result=%d (%s locally, %d remote)\n", inode, |
2781 | ceph_vinop(inode), err, ceph_cap_string(dirtied), mask); | |
355da1eb | 2782 | |
355da1eb | 2783 | ceph_mdsc_put_request(req); |
f66fd9f0 | 2784 | ceph_free_cap_flush(prealloc_cf); |
8179a101 YZ |
2785 | |
2786 | if (err >= 0 && (mask & CEPH_SETATTR_SIZE)) | |
2787 | __ceph_do_pending_vmtruncate(inode); | |
2788 | ||
355da1eb SW |
2789 | return err; |
2790 | } | |
2791 | ||
a26fecca AG |
2792 | /* |
2793 | * setattr | |
2794 | */ | |
c1632a0f | 2795 | int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
549c7297 | 2796 | struct iattr *attr) |
a26fecca | 2797 | { |
fd5472ed | 2798 | struct inode *inode = d_inode(dentry); |
5995d90d | 2799 | struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); |
fd5472ed JK |
2800 | int err; |
2801 | ||
2802 | if (ceph_snap(inode) != CEPH_NOSNAP) | |
2803 | return -EROFS; | |
2804 | ||
5d6451b1 JL |
2805 | if (ceph_inode_is_shutdown(inode)) |
2806 | return -ESTALE; | |
2807 | ||
94af0470 JL |
2808 | err = fscrypt_prepare_setattr(dentry, attr); |
2809 | if (err) | |
2810 | return err; | |
2811 | ||
a04aff25 | 2812 | err = setattr_prepare(idmap, dentry, attr); |
fd5472ed JK |
2813 | if (err != 0) |
2814 | return err; | |
2815 | ||
36a4c72d | 2816 | if ((attr->ia_valid & ATTR_SIZE) && |
2d6795fb | 2817 | attr->ia_size > max(i_size_read(inode), fsc->max_file_size)) |
36a4c72d CX |
2818 | return -EFBIG; |
2819 | ||
2b83845f LH |
2820 | if ((attr->ia_valid & ATTR_SIZE) && |
2821 | ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size)) | |
2822 | return -EDQUOT; | |
2823 | ||
79c66a0c | 2824 | err = __ceph_setattr(idmap, inode, attr, NULL); |
8179a101 YZ |
2825 | |
2826 | if (err >= 0 && (attr->ia_valid & ATTR_MODE)) | |
a04aff25 | 2827 | err = posix_acl_chmod(idmap, dentry, attr->ia_mode); |
8179a101 YZ |
2828 | |
2829 | return err; | |
a26fecca AG |
2830 | } |
2831 | ||
5eed80fb XL |
2832 | int ceph_try_to_choose_auth_mds(struct inode *inode, int mask) |
2833 | { | |
2834 | int issued = ceph_caps_issued(ceph_inode(inode)); | |
2835 | ||
2836 | /* | |
2837 | * If any 'x' caps is issued we can just choose the auth MDS | |
2838 | * instead of the random replica MDSes. Because only when the | |
2839 | * Locker is in LOCK_EXEC state will the loner client could | |
2840 | * get the 'x' caps. And if we send the getattr requests to | |
2841 | * any replica MDS it must auth pin and tries to rdlock from | |
2842 | * the auth MDS, and then the auth MDS need to do the Locker | |
2843 | * state transition to LOCK_SYNC. And after that the lock state | |
2844 | * will change back. | |
2845 | * | |
2846 | * This cost much when doing the Locker state transition and | |
2847 | * usually will need to revoke caps from clients. | |
8266c4d7 XL |
2848 | * |
2849 | * And for the 'Xs' caps for getxattr we will also choose the | |
2850 | * auth MDS, because the MDS side code is buggy due to setxattr | |
2851 | * won't notify the replica MDSes when the values changed and | |
2852 | * the replica MDS will return the old values. Though we will | |
2853 | * fix it in MDS code, but this still makes sense for old ceph. | |
5eed80fb XL |
2854 | */ |
2855 | if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL)) | |
8266c4d7 | 2856 | || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR))) |
5eed80fb XL |
2857 | return USE_AUTH_MDS; |
2858 | else | |
2859 | return USE_ANY_MDS; | |
2860 | } | |
2861 | ||
355da1eb SW |
2862 | /* |
2863 | * Verify that we have a lease on the given mask. If not, | |
2864 | * do a getattr against an mds. | |
2865 | */ | |
01deead0 YZ |
2866 | int __ceph_do_getattr(struct inode *inode, struct page *locked_page, |
2867 | int mask, bool force) | |
355da1eb | 2868 | { |
5995d90d | 2869 | struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb); |
38d46409 | 2870 | struct ceph_client *cl = fsc->client; |
3d14c5d2 | 2871 | struct ceph_mds_client *mdsc = fsc->mdsc; |
355da1eb | 2872 | struct ceph_mds_request *req; |
49a9f4f6 | 2873 | int mode; |
355da1eb SW |
2874 | int err; |
2875 | ||
2876 | if (ceph_snap(inode) == CEPH_SNAPDIR) { | |
38d46409 XL |
2877 | doutc(cl, "inode %p %llx.%llx SNAPDIR\n", inode, |
2878 | ceph_vinop(inode)); | |
355da1eb SW |
2879 | return 0; |
2880 | } | |
2881 | ||
38d46409 XL |
2882 | doutc(cl, "inode %p %llx.%llx mask %s mode 0%o\n", inode, |
2883 | ceph_vinop(inode), ceph_cap_string(mask), inode->i_mode); | |
1af16d54 XL |
2884 | if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1)) |
2885 | return 0; | |
355da1eb | 2886 | |
5eed80fb | 2887 | mode = ceph_try_to_choose_auth_mds(inode, mask); |
49a9f4f6 | 2888 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode); |
355da1eb SW |
2889 | if (IS_ERR(req)) |
2890 | return PTR_ERR(req); | |
70b666c3 SW |
2891 | req->r_inode = inode; |
2892 | ihold(inode); | |
355da1eb SW |
2893 | req->r_num_caps = 1; |
2894 | req->r_args.getattr.mask = cpu_to_le32(mask); | |
01deead0 | 2895 | req->r_locked_page = locked_page; |
355da1eb | 2896 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
01deead0 YZ |
2897 | if (locked_page && err == 0) { |
2898 | u64 inline_version = req->r_reply_info.targeti.inline_version; | |
2899 | if (inline_version == 0) { | |
2900 | /* the reply is supposed to contain inline data */ | |
2901 | err = -EINVAL; | |
48490776 XL |
2902 | } else if (inline_version == CEPH_INLINE_NONE || |
2903 | inline_version == 1) { | |
01deead0 YZ |
2904 | err = -ENODATA; |
2905 | } else { | |
2906 | err = req->r_reply_info.targeti.inline_len; | |
2907 | } | |
2908 | } | |
355da1eb | 2909 | ceph_mdsc_put_request(req); |
38d46409 | 2910 | doutc(cl, "result=%d\n", err); |
355da1eb SW |
2911 | return err; |
2912 | } | |
2913 | ||
6ddf5f16 MC |
2914 | int ceph_do_getvxattr(struct inode *inode, const char *name, void *value, |
2915 | size_t size) | |
2916 | { | |
5995d90d | 2917 | struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb); |
38d46409 | 2918 | struct ceph_client *cl = fsc->client; |
6ddf5f16 MC |
2919 | struct ceph_mds_client *mdsc = fsc->mdsc; |
2920 | struct ceph_mds_request *req; | |
2921 | int mode = USE_AUTH_MDS; | |
2922 | int err; | |
2923 | char *xattr_value; | |
2924 | size_t xattr_value_len; | |
2925 | ||
2926 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode); | |
2927 | if (IS_ERR(req)) { | |
2928 | err = -ENOMEM; | |
2929 | goto out; | |
2930 | } | |
2931 | ||
6eb06c46 | 2932 | req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR; |
6ddf5f16 MC |
2933 | req->r_path2 = kstrdup(name, GFP_NOFS); |
2934 | if (!req->r_path2) { | |
2935 | err = -ENOMEM; | |
2936 | goto put; | |
2937 | } | |
2938 | ||
2939 | ihold(inode); | |
2940 | req->r_inode = inode; | |
2941 | err = ceph_mdsc_do_request(mdsc, NULL, req); | |
2942 | if (err < 0) | |
2943 | goto put; | |
2944 | ||
2945 | xattr_value = req->r_reply_info.xattr_info.xattr_value; | |
2946 | xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len; | |
2947 | ||
38d46409 | 2948 | doutc(cl, "xattr_value_len:%zu, size:%zu\n", xattr_value_len, size); |
6ddf5f16 MC |
2949 | |
2950 | err = (int)xattr_value_len; | |
2951 | if (size == 0) | |
2952 | goto put; | |
2953 | ||
2954 | if (xattr_value_len > size) { | |
2955 | err = -ERANGE; | |
2956 | goto put; | |
2957 | } | |
2958 | ||
2959 | memcpy(value, xattr_value, xattr_value_len); | |
2960 | put: | |
2961 | ceph_mdsc_put_request(req); | |
2962 | out: | |
38d46409 | 2963 | doutc(cl, "result=%d\n", err); |
6ddf5f16 MC |
2964 | return err; |
2965 | } | |
2966 | ||
355da1eb SW |
2967 | |
2968 | /* | |
2969 | * Check inode permissions. We verify we have a valid value for | |
2970 | * the AUTH cap, then call the generic handler. | |
2971 | */ | |
4609e1f1 | 2972 | int ceph_permission(struct mnt_idmap *idmap, struct inode *inode, |
549c7297 | 2973 | int mask) |
355da1eb | 2974 | { |
b74c79e9 NP |
2975 | int err; |
2976 | ||
10556cb2 | 2977 | if (mask & MAY_NOT_BLOCK) |
b74c79e9 NP |
2978 | return -ECHILD; |
2979 | ||
508b32d8 | 2980 | err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); |
355da1eb SW |
2981 | |
2982 | if (!err) | |
8995375f | 2983 | err = generic_permission(idmap, inode, mask); |
355da1eb SW |
2984 | return err; |
2985 | } | |
2986 | ||
428bb68a | 2987 | /* Craft a mask of needed caps given a set of requested statx attrs. */ |
04fabb11 | 2988 | static int statx_to_caps(u32 want, umode_t mode) |
428bb68a JL |
2989 | { |
2990 | int mask = 0; | |
2991 | ||
f6102994 | 2992 | if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE)) |
428bb68a JL |
2993 | mask |= CEPH_CAP_AUTH_SHARED; |
2994 | ||
f6102994 | 2995 | if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) { |
04fabb11 JL |
2996 | /* |
2997 | * The link count for directories depends on inode->i_subdirs, | |
2998 | * and that is only updated when Fs caps are held. | |
2999 | */ | |
3000 | if (S_ISDIR(mode)) | |
3001 | mask |= CEPH_CAP_FILE_SHARED; | |
3002 | else | |
3003 | mask |= CEPH_CAP_LINK_SHARED; | |
3004 | } | |
428bb68a | 3005 | |
f6102994 | 3006 | if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE)) |
428bb68a JL |
3007 | mask |= CEPH_CAP_FILE_SHARED; |
3008 | ||
f6102994 | 3009 | if (want & (STATX_CTIME|STATX_CHANGE_COOKIE)) |
428bb68a JL |
3010 | mask |= CEPH_CAP_XATTR_SHARED; |
3011 | ||
3012 | return mask; | |
3013 | } | |
3014 | ||
355da1eb | 3015 | /* |
428bb68a JL |
3016 | * Get all the attributes. If we have sufficient caps for the requested attrs, |
3017 | * then we can avoid talking to the MDS at all. | |
355da1eb | 3018 | */ |
b74d24f7 | 3019 | int ceph_getattr(struct mnt_idmap *idmap, const struct path *path, |
549c7297 | 3020 | struct kstat *stat, u32 request_mask, unsigned int flags) |
355da1eb | 3021 | { |
a528d35e | 3022 | struct inode *inode = d_inode(path->dentry); |
aa87052d | 3023 | struct super_block *sb = inode->i_sb; |
232d4b01 | 3024 | struct ceph_inode_info *ci = ceph_inode(inode); |
58981784 | 3025 | u32 valid_mask = STATX_BASIC_STATS; |
428bb68a | 3026 | int err = 0; |
355da1eb | 3027 | |
5d6451b1 JL |
3028 | if (ceph_inode_is_shutdown(inode)) |
3029 | return -ESTALE; | |
3030 | ||
428bb68a | 3031 | /* Skip the getattr altogether if we're asked not to sync */ |
261998c3 | 3032 | if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) { |
04fabb11 JL |
3033 | err = ceph_do_getattr(inode, |
3034 | statx_to_caps(request_mask, inode->i_mode), | |
3035 | flags & AT_STATX_FORCE_SYNC); | |
428bb68a JL |
3036 | if (err) |
3037 | return err; | |
3038 | } | |
3039 | ||
0513043e | 3040 | generic_fillattr(idmap, request_mask, inode, stat); |
ebce3eb2 | 3041 | stat->ino = ceph_present_inode(inode); |
58981784 JL |
3042 | |
3043 | /* | |
3044 | * btime on newly-allocated inodes is 0, so if this is still set to | |
3045 | * that, then assume that it's not valid. | |
3046 | */ | |
3047 | if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) { | |
3048 | stat->btime = ci->i_btime; | |
3049 | valid_mask |= STATX_BTIME; | |
3050 | } | |
3051 | ||
f6102994 JL |
3052 | if (request_mask & STATX_CHANGE_COOKIE) { |
3053 | stat->change_cookie = inode_peek_iversion_raw(inode); | |
3054 | valid_mask |= STATX_CHANGE_COOKIE; | |
3055 | } | |
3056 | ||
428bb68a | 3057 | if (ceph_snap(inode) == CEPH_NOSNAP) |
aa87052d | 3058 | stat->dev = sb->s_dev; |
428bb68a JL |
3059 | else |
3060 | stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0; | |
3061 | ||
3062 | if (S_ISDIR(inode->i_mode)) { | |
5995d90d | 3063 | if (ceph_test_mount_opt(ceph_sb_to_fs_client(sb), RBYTES)) { |
428bb68a | 3064 | stat->size = ci->i_rbytes; |
aa87052d XL |
3065 | } else if (ceph_snap(inode) == CEPH_SNAPDIR) { |
3066 | struct ceph_inode_info *pci; | |
3067 | struct ceph_snap_realm *realm; | |
3068 | struct inode *parent; | |
3069 | ||
3070 | parent = ceph_lookup_inode(sb, ceph_ino(inode)); | |
f86a4866 | 3071 | if (IS_ERR(parent)) |
aa87052d XL |
3072 | return PTR_ERR(parent); |
3073 | ||
3074 | pci = ceph_inode(parent); | |
3075 | spin_lock(&pci->i_ceph_lock); | |
3076 | realm = pci->i_snap_realm; | |
3077 | if (realm) | |
3078 | stat->size = realm->num_snaps; | |
3079 | else | |
3080 | stat->size = 0; | |
3081 | spin_unlock(&pci->i_ceph_lock); | |
3082 | iput(parent); | |
3083 | } else { | |
428bb68a | 3084 | stat->size = ci->i_files + ci->i_subdirs; |
aa87052d | 3085 | } |
428bb68a JL |
3086 | stat->blocks = 0; |
3087 | stat->blksize = 65536; | |
3088 | /* | |
3089 | * Some applications rely on the number of st_nlink | |
3090 | * value on directories to be either 0 (if unlinked) | |
3091 | * or 2 + number of subdirectories. | |
3092 | */ | |
3093 | if (stat->nlink == 1) | |
3094 | /* '.' + '..' + subdirs */ | |
3095 | stat->nlink = 1 + 1 + ci->i_subdirs; | |
355da1eb | 3096 | } |
428bb68a | 3097 | |
f6102994 | 3098 | stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC; |
94af0470 JL |
3099 | if (IS_ENCRYPTED(inode)) |
3100 | stat->attributes |= STATX_ATTR_ENCRYPTED; | |
3101 | stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC | | |
3102 | STATX_ATTR_ENCRYPTED); | |
3103 | ||
58981784 | 3104 | stat->result_mask = request_mask & valid_mask; |
355da1eb SW |
3105 | return err; |
3106 | } | |
5d6451b1 JL |
3107 | |
3108 | void ceph_inode_shutdown(struct inode *inode) | |
3109 | { | |
3110 | struct ceph_inode_info *ci = ceph_inode(inode); | |
3111 | struct rb_node *p; | |
3112 | int iputs = 0; | |
3113 | bool invalidate = false; | |
3114 | ||
3115 | spin_lock(&ci->i_ceph_lock); | |
3116 | ci->i_ceph_flags |= CEPH_I_SHUTDOWN; | |
3117 | p = rb_first(&ci->i_caps); | |
3118 | while (p) { | |
3119 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); | |
3120 | ||
3121 | p = rb_next(p); | |
3122 | iputs += ceph_purge_inode_cap(inode, cap, &invalidate); | |
3123 | } | |
3124 | spin_unlock(&ci->i_ceph_lock); | |
3125 | ||
3126 | if (invalidate) | |
3127 | ceph_queue_invalidate(inode); | |
3128 | while (iputs--) | |
3129 | iput(inode); | |
3130 | } |