]>
Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
355da1eb SW |
2 | |
3 | #include <linux/module.h> | |
4 | #include <linux/fs.h> | |
355da1eb SW |
5 | #include <linux/slab.h> |
6 | #include <linux/string.h> | |
7 | #include <linux/uaccess.h> | |
8 | #include <linux/kernel.h> | |
355da1eb SW |
9 | #include <linux/writeback.h> |
10 | #include <linux/vmalloc.h> | |
4db658ea | 11 | #include <linux/posix_acl.h> |
3e7fbe9c | 12 | #include <linux/random.h> |
a407846e | 13 | #include <linux/sort.h> |
355da1eb SW |
14 | |
15 | #include "super.h" | |
3d14c5d2 | 16 | #include "mds_client.h" |
99ccbd22 | 17 | #include "cache.h" |
3d14c5d2 | 18 | #include <linux/ceph/decode.h> |
355da1eb SW |
19 | |
20 | /* | |
21 | * Ceph inode operations | |
22 | * | |
23 | * Implement basic inode helpers (get, alloc) and inode ops (getattr, | |
24 | * setattr, etc.), xattr helpers, and helpers for assimilating | |
25 | * metadata returned by the MDS into our cache. | |
26 | * | |
27 | * Also define helpers for doing asynchronous writeback, invalidation, | |
28 | * and truncation for the benefit of those who can't afford to block | |
29 | * (typically because they are in the message handler path). | |
30 | */ | |
31 | ||
32 | static const struct inode_operations ceph_symlink_iops; | |
33 | ||
3c6f6b79 SW |
34 | static void ceph_invalidate_work(struct work_struct *work); |
35 | static void ceph_writeback_work(struct work_struct *work); | |
36 | static void ceph_vmtruncate_work(struct work_struct *work); | |
355da1eb SW |
37 | |
38 | /* | |
39 | * find or create an inode, given the ceph ino number | |
40 | */ | |
ad1fee96 YS |
41 | static int ceph_set_ino_cb(struct inode *inode, void *data) |
42 | { | |
43 | ceph_inode(inode)->i_vino = *(struct ceph_vino *)data; | |
44 | inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data); | |
45 | return 0; | |
46 | } | |
47 | ||
355da1eb SW |
48 | struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino) |
49 | { | |
50 | struct inode *inode; | |
51 | ino_t t = ceph_vino_to_ino(vino); | |
52 | ||
53 | inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino); | |
54 | if (inode == NULL) | |
55 | return ERR_PTR(-ENOMEM); | |
56 | if (inode->i_state & I_NEW) { | |
57 | dout("get_inode created new inode %p %llx.%llx ino %llx\n", | |
58 | inode, ceph_vinop(inode), (u64)inode->i_ino); | |
59 | unlock_new_inode(inode); | |
60 | } | |
61 | ||
62 | dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino, | |
63 | vino.snap, inode); | |
64 | return inode; | |
65 | } | |
66 | ||
67 | /* | |
68 | * get/constuct snapdir inode for a given directory | |
69 | */ | |
70 | struct inode *ceph_get_snapdir(struct inode *parent) | |
71 | { | |
72 | struct ceph_vino vino = { | |
73 | .ino = ceph_ino(parent), | |
74 | .snap = CEPH_SNAPDIR, | |
75 | }; | |
76 | struct inode *inode = ceph_get_inode(parent->i_sb, vino); | |
b377ff13 | 77 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb SW |
78 | |
79 | BUG_ON(!S_ISDIR(parent->i_mode)); | |
80 | if (IS_ERR(inode)) | |
7e34bc52 | 81 | return inode; |
355da1eb SW |
82 | inode->i_mode = parent->i_mode; |
83 | inode->i_uid = parent->i_uid; | |
84 | inode->i_gid = parent->i_gid; | |
38c48b5f YZ |
85 | inode->i_op = &ceph_snapdir_iops; |
86 | inode->i_fop = &ceph_snapdir_fops; | |
b377ff13 SW |
87 | ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ |
88 | ci->i_rbytes = 0; | |
355da1eb SW |
89 | return inode; |
90 | } | |
91 | ||
92 | const struct inode_operations ceph_file_iops = { | |
93 | .permission = ceph_permission, | |
94 | .setattr = ceph_setattr, | |
95 | .getattr = ceph_getattr, | |
96 | .setxattr = ceph_setxattr, | |
97 | .getxattr = ceph_getxattr, | |
98 | .listxattr = ceph_listxattr, | |
99 | .removexattr = ceph_removexattr, | |
7221fe4c | 100 | .get_acl = ceph_get_acl, |
72466d0b | 101 | .set_acl = ceph_set_acl, |
355da1eb SW |
102 | }; |
103 | ||
104 | ||
105 | /* | |
106 | * We use a 'frag tree' to keep track of the MDS's directory fragments | |
107 | * for a given inode (usually there is just a single fragment). We | |
108 | * need to know when a child frag is delegated to a new MDS, or when | |
109 | * it is flagged as replicated, so we can direct our requests | |
110 | * accordingly. | |
111 | */ | |
112 | ||
113 | /* | |
114 | * find/create a frag in the tree | |
115 | */ | |
116 | static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, | |
117 | u32 f) | |
118 | { | |
119 | struct rb_node **p; | |
120 | struct rb_node *parent = NULL; | |
121 | struct ceph_inode_frag *frag; | |
122 | int c; | |
123 | ||
124 | p = &ci->i_fragtree.rb_node; | |
125 | while (*p) { | |
126 | parent = *p; | |
127 | frag = rb_entry(parent, struct ceph_inode_frag, node); | |
128 | c = ceph_frag_compare(f, frag->frag); | |
129 | if (c < 0) | |
130 | p = &(*p)->rb_left; | |
131 | else if (c > 0) | |
132 | p = &(*p)->rb_right; | |
133 | else | |
134 | return frag; | |
135 | } | |
136 | ||
137 | frag = kmalloc(sizeof(*frag), GFP_NOFS); | |
138 | if (!frag) { | |
139 | pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx " | |
140 | "frag %x\n", &ci->vfs_inode, | |
141 | ceph_vinop(&ci->vfs_inode), f); | |
142 | return ERR_PTR(-ENOMEM); | |
143 | } | |
144 | frag->frag = f; | |
145 | frag->split_by = 0; | |
146 | frag->mds = -1; | |
147 | frag->ndist = 0; | |
148 | ||
149 | rb_link_node(&frag->node, parent, p); | |
150 | rb_insert_color(&frag->node, &ci->i_fragtree); | |
151 | ||
152 | dout("get_or_create_frag added %llx.%llx frag %x\n", | |
153 | ceph_vinop(&ci->vfs_inode), f); | |
154 | return frag; | |
155 | } | |
156 | ||
157 | /* | |
158 | * find a specific frag @f | |
159 | */ | |
160 | struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) | |
161 | { | |
162 | struct rb_node *n = ci->i_fragtree.rb_node; | |
163 | ||
164 | while (n) { | |
165 | struct ceph_inode_frag *frag = | |
166 | rb_entry(n, struct ceph_inode_frag, node); | |
167 | int c = ceph_frag_compare(f, frag->frag); | |
168 | if (c < 0) | |
169 | n = n->rb_left; | |
170 | else if (c > 0) | |
171 | n = n->rb_right; | |
172 | else | |
173 | return frag; | |
174 | } | |
175 | return NULL; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Choose frag containing the given value @v. If @pfrag is | |
180 | * specified, copy the frag delegation info to the caller if | |
181 | * it is present. | |
182 | */ | |
3e7fbe9c YZ |
183 | static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
184 | struct ceph_inode_frag *pfrag, int *found) | |
355da1eb SW |
185 | { |
186 | u32 t = ceph_frag_make(0, 0); | |
187 | struct ceph_inode_frag *frag; | |
188 | unsigned nway, i; | |
189 | u32 n; | |
190 | ||
191 | if (found) | |
192 | *found = 0; | |
193 | ||
355da1eb SW |
194 | while (1) { |
195 | WARN_ON(!ceph_frag_contains_value(t, v)); | |
196 | frag = __ceph_find_frag(ci, t); | |
197 | if (!frag) | |
198 | break; /* t is a leaf */ | |
199 | if (frag->split_by == 0) { | |
200 | if (pfrag) | |
201 | memcpy(pfrag, frag, sizeof(*pfrag)); | |
202 | if (found) | |
203 | *found = 1; | |
204 | break; | |
205 | } | |
206 | ||
207 | /* choose child */ | |
208 | nway = 1 << frag->split_by; | |
209 | dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t, | |
210 | frag->split_by, nway); | |
211 | for (i = 0; i < nway; i++) { | |
212 | n = ceph_frag_make_child(t, frag->split_by, i); | |
213 | if (ceph_frag_contains_value(n, v)) { | |
214 | t = n; | |
215 | break; | |
216 | } | |
217 | } | |
218 | BUG_ON(i == nway); | |
219 | } | |
220 | dout("choose_frag(%x) = %x\n", v, t); | |
221 | ||
355da1eb SW |
222 | return t; |
223 | } | |
224 | ||
3e7fbe9c YZ |
225 | u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, |
226 | struct ceph_inode_frag *pfrag, int *found) | |
227 | { | |
228 | u32 ret; | |
229 | mutex_lock(&ci->i_fragtree_mutex); | |
230 | ret = __ceph_choose_frag(ci, v, pfrag, found); | |
231 | mutex_unlock(&ci->i_fragtree_mutex); | |
232 | return ret; | |
233 | } | |
234 | ||
355da1eb SW |
235 | /* |
236 | * Process dirfrag (delegation) info from the mds. Include leaf | |
237 | * fragment in tree ONLY if ndist > 0. Otherwise, only | |
238 | * branches/splits are included in i_fragtree) | |
239 | */ | |
240 | static int ceph_fill_dirfrag(struct inode *inode, | |
241 | struct ceph_mds_reply_dirfrag *dirinfo) | |
242 | { | |
243 | struct ceph_inode_info *ci = ceph_inode(inode); | |
244 | struct ceph_inode_frag *frag; | |
245 | u32 id = le32_to_cpu(dirinfo->frag); | |
246 | int mds = le32_to_cpu(dirinfo->auth); | |
247 | int ndist = le32_to_cpu(dirinfo->ndist); | |
8d08503c | 248 | int diri_auth = -1; |
355da1eb SW |
249 | int i; |
250 | int err = 0; | |
251 | ||
8d08503c YZ |
252 | spin_lock(&ci->i_ceph_lock); |
253 | if (ci->i_auth_cap) | |
254 | diri_auth = ci->i_auth_cap->mds; | |
255 | spin_unlock(&ci->i_ceph_lock); | |
256 | ||
42172119 YZ |
257 | if (mds == -1) /* CDIR_AUTH_PARENT */ |
258 | mds = diri_auth; | |
259 | ||
355da1eb | 260 | mutex_lock(&ci->i_fragtree_mutex); |
8d08503c | 261 | if (ndist == 0 && mds == diri_auth) { |
355da1eb SW |
262 | /* no delegation info needed. */ |
263 | frag = __ceph_find_frag(ci, id); | |
264 | if (!frag) | |
265 | goto out; | |
266 | if (frag->split_by == 0) { | |
267 | /* tree leaf, remove */ | |
268 | dout("fill_dirfrag removed %llx.%llx frag %x" | |
269 | " (no ref)\n", ceph_vinop(inode), id); | |
270 | rb_erase(&frag->node, &ci->i_fragtree); | |
271 | kfree(frag); | |
272 | } else { | |
273 | /* tree branch, keep and clear */ | |
274 | dout("fill_dirfrag cleared %llx.%llx frag %x" | |
275 | " referral\n", ceph_vinop(inode), id); | |
276 | frag->mds = -1; | |
277 | frag->ndist = 0; | |
278 | } | |
279 | goto out; | |
280 | } | |
281 | ||
282 | ||
283 | /* find/add this frag to store mds delegation info */ | |
284 | frag = __get_or_create_frag(ci, id); | |
285 | if (IS_ERR(frag)) { | |
286 | /* this is not the end of the world; we can continue | |
287 | with bad/inaccurate delegation info */ | |
288 | pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n", | |
289 | ceph_vinop(inode), le32_to_cpu(dirinfo->frag)); | |
290 | err = -ENOMEM; | |
291 | goto out; | |
292 | } | |
293 | ||
294 | frag->mds = mds; | |
295 | frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); | |
296 | for (i = 0; i < frag->ndist; i++) | |
297 | frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); | |
298 | dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n", | |
299 | ceph_vinop(inode), frag->frag, frag->ndist); | |
300 | ||
301 | out: | |
302 | mutex_unlock(&ci->i_fragtree_mutex); | |
303 | return err; | |
304 | } | |
305 | ||
a407846e YZ |
306 | static int frag_tree_split_cmp(const void *l, const void *r) |
307 | { | |
308 | struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l; | |
309 | struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r; | |
310 | return ceph_frag_compare(ls->frag, rs->frag); | |
311 | } | |
312 | ||
a4b7431f YZ |
313 | static bool is_frag_child(u32 f, struct ceph_inode_frag *frag) |
314 | { | |
315 | if (!frag) | |
316 | return f == ceph_frag_make(0, 0); | |
317 | if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by) | |
318 | return false; | |
319 | return ceph_frag_contains_value(frag->frag, ceph_frag_value(f)); | |
320 | } | |
321 | ||
3e7fbe9c YZ |
322 | static int ceph_fill_fragtree(struct inode *inode, |
323 | struct ceph_frag_tree_head *fragtree, | |
324 | struct ceph_mds_reply_dirfrag *dirinfo) | |
325 | { | |
326 | struct ceph_inode_info *ci = ceph_inode(inode); | |
a4b7431f | 327 | struct ceph_inode_frag *frag, *prev_frag = NULL; |
3e7fbe9c YZ |
328 | struct rb_node *rb_node; |
329 | int i; | |
330 | u32 id, nsplits; | |
331 | bool update = false; | |
332 | ||
333 | mutex_lock(&ci->i_fragtree_mutex); | |
334 | nsplits = le32_to_cpu(fragtree->nsplits); | |
335 | if (nsplits) { | |
336 | i = prandom_u32() % nsplits; | |
337 | id = le32_to_cpu(fragtree->splits[i].frag); | |
338 | if (!__ceph_find_frag(ci, id)) | |
339 | update = true; | |
340 | } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) { | |
341 | rb_node = rb_first(&ci->i_fragtree); | |
342 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
343 | if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node)) | |
344 | update = true; | |
345 | } | |
346 | if (!update && dirinfo) { | |
347 | id = le32_to_cpu(dirinfo->frag); | |
348 | if (id != __ceph_choose_frag(ci, id, NULL, NULL)) | |
349 | update = true; | |
350 | } | |
351 | if (!update) | |
352 | goto out_unlock; | |
353 | ||
a407846e YZ |
354 | if (nsplits > 1) { |
355 | sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]), | |
356 | frag_tree_split_cmp, NULL); | |
357 | } | |
358 | ||
3e7fbe9c YZ |
359 | dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode)); |
360 | rb_node = rb_first(&ci->i_fragtree); | |
361 | for (i = 0; i < nsplits; i++) { | |
362 | id = le32_to_cpu(fragtree->splits[i].frag); | |
363 | frag = NULL; | |
364 | while (rb_node) { | |
365 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
366 | if (ceph_frag_compare(frag->frag, id) >= 0) { | |
367 | if (frag->frag != id) | |
368 | frag = NULL; | |
369 | else | |
370 | rb_node = rb_next(rb_node); | |
371 | break; | |
372 | } | |
373 | rb_node = rb_next(rb_node); | |
a4b7431f YZ |
374 | /* delete stale split/leaf node */ |
375 | if (frag->split_by > 0 || | |
376 | !is_frag_child(frag->frag, prev_frag)) { | |
377 | rb_erase(&frag->node, &ci->i_fragtree); | |
378 | kfree(frag); | |
379 | } | |
3e7fbe9c YZ |
380 | frag = NULL; |
381 | } | |
382 | if (!frag) { | |
383 | frag = __get_or_create_frag(ci, id); | |
384 | if (IS_ERR(frag)) | |
385 | continue; | |
386 | } | |
387 | frag->split_by = le32_to_cpu(fragtree->splits[i].by); | |
388 | dout(" frag %x split by %d\n", frag->frag, frag->split_by); | |
a4b7431f | 389 | prev_frag = frag; |
3e7fbe9c YZ |
390 | } |
391 | while (rb_node) { | |
392 | frag = rb_entry(rb_node, struct ceph_inode_frag, node); | |
393 | rb_node = rb_next(rb_node); | |
a4b7431f YZ |
394 | /* delete stale split/leaf node */ |
395 | if (frag->split_by > 0 || | |
396 | !is_frag_child(frag->frag, prev_frag)) { | |
397 | rb_erase(&frag->node, &ci->i_fragtree); | |
398 | kfree(frag); | |
399 | } | |
3e7fbe9c YZ |
400 | } |
401 | out_unlock: | |
402 | mutex_unlock(&ci->i_fragtree_mutex); | |
403 | return 0; | |
404 | } | |
355da1eb SW |
405 | |
406 | /* | |
407 | * initialize a newly allocated inode. | |
408 | */ | |
409 | struct inode *ceph_alloc_inode(struct super_block *sb) | |
410 | { | |
411 | struct ceph_inode_info *ci; | |
412 | int i; | |
413 | ||
414 | ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS); | |
415 | if (!ci) | |
416 | return NULL; | |
417 | ||
418 | dout("alloc_inode %p\n", &ci->vfs_inode); | |
419 | ||
be655596 SW |
420 | spin_lock_init(&ci->i_ceph_lock); |
421 | ||
355da1eb | 422 | ci->i_version = 0; |
31c542a1 | 423 | ci->i_inline_version = 0; |
355da1eb SW |
424 | ci->i_time_warp_seq = 0; |
425 | ci->i_ceph_flags = 0; | |
fdd4e158 YZ |
426 | atomic64_set(&ci->i_ordered_count, 1); |
427 | atomic64_set(&ci->i_release_count, 1); | |
428 | atomic64_set(&ci->i_complete_seq[0], 0); | |
429 | atomic64_set(&ci->i_complete_seq[1], 0); | |
355da1eb SW |
430 | ci->i_symlink = NULL; |
431 | ||
6c0f3af7 | 432 | memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout)); |
5ea5c5e0 | 433 | ci->i_pool_ns_len = 0; |
6c0f3af7 | 434 | |
355da1eb SW |
435 | ci->i_fragtree = RB_ROOT; |
436 | mutex_init(&ci->i_fragtree_mutex); | |
437 | ||
438 | ci->i_xattrs.blob = NULL; | |
439 | ci->i_xattrs.prealloc_blob = NULL; | |
440 | ci->i_xattrs.dirty = false; | |
441 | ci->i_xattrs.index = RB_ROOT; | |
442 | ci->i_xattrs.count = 0; | |
443 | ci->i_xattrs.names_size = 0; | |
444 | ci->i_xattrs.vals_size = 0; | |
445 | ci->i_xattrs.version = 0; | |
446 | ci->i_xattrs.index_version = 0; | |
447 | ||
448 | ci->i_caps = RB_ROOT; | |
449 | ci->i_auth_cap = NULL; | |
450 | ci->i_dirty_caps = 0; | |
451 | ci->i_flushing_caps = 0; | |
452 | INIT_LIST_HEAD(&ci->i_dirty_item); | |
453 | INIT_LIST_HEAD(&ci->i_flushing_item); | |
f66fd9f0 | 454 | ci->i_prealloc_cap_flush = NULL; |
553adfd9 | 455 | ci->i_cap_flush_tree = RB_ROOT; |
355da1eb SW |
456 | init_waitqueue_head(&ci->i_cap_wq); |
457 | ci->i_hold_caps_min = 0; | |
458 | ci->i_hold_caps_max = 0; | |
459 | INIT_LIST_HEAD(&ci->i_cap_delay_list); | |
355da1eb SW |
460 | INIT_LIST_HEAD(&ci->i_cap_snaps); |
461 | ci->i_head_snapc = NULL; | |
462 | ci->i_snap_caps = 0; | |
463 | ||
464 | for (i = 0; i < CEPH_FILE_MODE_NUM; i++) | |
465 | ci->i_nr_by_mode[i] = 0; | |
466 | ||
b0d7c223 | 467 | mutex_init(&ci->i_truncate_mutex); |
355da1eb SW |
468 | ci->i_truncate_seq = 0; |
469 | ci->i_truncate_size = 0; | |
470 | ci->i_truncate_pending = 0; | |
471 | ||
472 | ci->i_max_size = 0; | |
473 | ci->i_reported_size = 0; | |
474 | ci->i_wanted_max_size = 0; | |
475 | ci->i_requested_max_size = 0; | |
476 | ||
477 | ci->i_pin_ref = 0; | |
478 | ci->i_rd_ref = 0; | |
479 | ci->i_rdcache_ref = 0; | |
480 | ci->i_wr_ref = 0; | |
d3d0720d | 481 | ci->i_wb_ref = 0; |
355da1eb SW |
482 | ci->i_wrbuffer_ref = 0; |
483 | ci->i_wrbuffer_ref_head = 0; | |
484 | ci->i_shared_gen = 0; | |
485 | ci->i_rdcache_gen = 0; | |
486 | ci->i_rdcache_revoking = 0; | |
487 | ||
488 | INIT_LIST_HEAD(&ci->i_unsafe_writes); | |
489 | INIT_LIST_HEAD(&ci->i_unsafe_dirops); | |
68cd5b4b | 490 | INIT_LIST_HEAD(&ci->i_unsafe_iops); |
355da1eb SW |
491 | spin_lock_init(&ci->i_unsafe_lock); |
492 | ||
493 | ci->i_snap_realm = NULL; | |
494 | INIT_LIST_HEAD(&ci->i_snap_realm_item); | |
495 | INIT_LIST_HEAD(&ci->i_snap_flush_item); | |
496 | ||
3c6f6b79 SW |
497 | INIT_WORK(&ci->i_wb_work, ceph_writeback_work); |
498 | INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work); | |
355da1eb SW |
499 | |
500 | INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work); | |
501 | ||
99ccbd22 MT |
502 | ceph_fscache_inode_init(ci); |
503 | ||
355da1eb SW |
504 | return &ci->vfs_inode; |
505 | } | |
506 | ||
fa0d7e3d NP |
507 | static void ceph_i_callback(struct rcu_head *head) |
508 | { | |
509 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
510 | struct ceph_inode_info *ci = ceph_inode(inode); | |
511 | ||
fa0d7e3d NP |
512 | kmem_cache_free(ceph_inode_cachep, ci); |
513 | } | |
514 | ||
355da1eb SW |
515 | void ceph_destroy_inode(struct inode *inode) |
516 | { | |
517 | struct ceph_inode_info *ci = ceph_inode(inode); | |
518 | struct ceph_inode_frag *frag; | |
519 | struct rb_node *n; | |
520 | ||
521 | dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode)); | |
522 | ||
99ccbd22 MT |
523 | ceph_fscache_unregister_inode_cookie(ci); |
524 | ||
355da1eb SW |
525 | ceph_queue_caps_release(inode); |
526 | ||
8b218b8a SW |
527 | /* |
528 | * we may still have a snap_realm reference if there are stray | |
d9df2783 | 529 | * caps in i_snap_caps. |
8b218b8a SW |
530 | */ |
531 | if (ci->i_snap_realm) { | |
532 | struct ceph_mds_client *mdsc = | |
3d14c5d2 | 533 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
8b218b8a SW |
534 | struct ceph_snap_realm *realm = ci->i_snap_realm; |
535 | ||
536 | dout(" dropping residual ref to snap realm %p\n", realm); | |
537 | spin_lock(&realm->inodes_with_caps_lock); | |
538 | list_del_init(&ci->i_snap_realm_item); | |
539 | spin_unlock(&realm->inodes_with_caps_lock); | |
540 | ceph_put_snap_realm(mdsc, realm); | |
541 | } | |
542 | ||
355da1eb SW |
543 | kfree(ci->i_symlink); |
544 | while ((n = rb_first(&ci->i_fragtree)) != NULL) { | |
545 | frag = rb_entry(n, struct ceph_inode_frag, node); | |
546 | rb_erase(n, &ci->i_fragtree); | |
547 | kfree(frag); | |
548 | } | |
549 | ||
550 | __ceph_destroy_xattrs(ci); | |
b6c1d5b8 SW |
551 | if (ci->i_xattrs.blob) |
552 | ceph_buffer_put(ci->i_xattrs.blob); | |
553 | if (ci->i_xattrs.prealloc_blob) | |
554 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
355da1eb | 555 | |
fa0d7e3d | 556 | call_rcu(&inode->i_rcu, ceph_i_callback); |
355da1eb SW |
557 | } |
558 | ||
9f12bd11 YZ |
559 | int ceph_drop_inode(struct inode *inode) |
560 | { | |
561 | /* | |
562 | * Positve dentry and corresponding inode are always accompanied | |
563 | * in MDS reply. So no need to keep inode in the cache after | |
564 | * dropping all its aliases. | |
565 | */ | |
566 | return 1; | |
567 | } | |
568 | ||
355da1eb SW |
569 | /* |
570 | * Helpers to fill in size, ctime, mtime, and atime. We have to be | |
571 | * careful because either the client or MDS may have more up to date | |
572 | * info, depending on which capabilities are held, and whether | |
573 | * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime | |
574 | * and size are monotonically increasing, except when utimes() or | |
575 | * truncate() increments the corresponding _seq values.) | |
576 | */ | |
577 | int ceph_fill_file_size(struct inode *inode, int issued, | |
578 | u32 truncate_seq, u64 truncate_size, u64 size) | |
579 | { | |
580 | struct ceph_inode_info *ci = ceph_inode(inode); | |
581 | int queue_trunc = 0; | |
582 | ||
583 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || | |
584 | (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) { | |
585 | dout("size %lld -> %llu\n", inode->i_size, size); | |
a3d714c3 YZ |
586 | if (size > 0 && S_ISDIR(inode->i_mode)) { |
587 | pr_err("fill_file_size non-zero size for directory\n"); | |
588 | size = 0; | |
589 | } | |
99c88e69 | 590 | i_size_write(inode, size); |
355da1eb SW |
591 | inode->i_blocks = (size + (1<<9) - 1) >> 9; |
592 | ci->i_reported_size = size; | |
593 | if (truncate_seq != ci->i_truncate_seq) { | |
594 | dout("truncate_seq %u -> %u\n", | |
595 | ci->i_truncate_seq, truncate_seq); | |
596 | ci->i_truncate_seq = truncate_seq; | |
b0d7c223 YZ |
597 | |
598 | /* the MDS should have revoked these caps */ | |
599 | WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL | | |
600 | CEPH_CAP_FILE_RD | | |
601 | CEPH_CAP_FILE_WR | | |
602 | CEPH_CAP_FILE_LAZYIO)); | |
3d497d85 YS |
603 | /* |
604 | * If we hold relevant caps, or in the case where we're | |
605 | * not the only client referencing this file and we | |
606 | * don't hold those caps, then we need to check whether | |
607 | * the file is either opened or mmaped | |
608 | */ | |
b0d7c223 YZ |
609 | if ((issued & (CEPH_CAP_FILE_CACHE| |
610 | CEPH_CAP_FILE_BUFFER)) || | |
3d497d85 YS |
611 | mapping_mapped(inode->i_mapping) || |
612 | __ceph_caps_file_wanted(ci)) { | |
355da1eb SW |
613 | ci->i_truncate_pending++; |
614 | queue_trunc = 1; | |
615 | } | |
616 | } | |
617 | } | |
618 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 && | |
619 | ci->i_truncate_size != truncate_size) { | |
620 | dout("truncate_size %lld -> %llu\n", ci->i_truncate_size, | |
621 | truncate_size); | |
622 | ci->i_truncate_size = truncate_size; | |
623 | } | |
99ccbd22 MT |
624 | |
625 | if (queue_trunc) | |
626 | ceph_fscache_invalidate(inode); | |
627 | ||
355da1eb SW |
628 | return queue_trunc; |
629 | } | |
630 | ||
631 | void ceph_fill_file_time(struct inode *inode, int issued, | |
632 | u64 time_warp_seq, struct timespec *ctime, | |
633 | struct timespec *mtime, struct timespec *atime) | |
634 | { | |
635 | struct ceph_inode_info *ci = ceph_inode(inode); | |
636 | int warn = 0; | |
637 | ||
638 | if (issued & (CEPH_CAP_FILE_EXCL| | |
639 | CEPH_CAP_FILE_WR| | |
d8672d64 SW |
640 | CEPH_CAP_FILE_BUFFER| |
641 | CEPH_CAP_AUTH_EXCL| | |
642 | CEPH_CAP_XATTR_EXCL)) { | |
355da1eb SW |
643 | if (timespec_compare(ctime, &inode->i_ctime) > 0) { |
644 | dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n", | |
645 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, | |
646 | ctime->tv_sec, ctime->tv_nsec); | |
647 | inode->i_ctime = *ctime; | |
648 | } | |
649 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { | |
650 | /* the MDS did a utimes() */ | |
651 | dout("mtime %ld.%09ld -> %ld.%09ld " | |
652 | "tw %d -> %d\n", | |
653 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, | |
654 | mtime->tv_sec, mtime->tv_nsec, | |
655 | ci->i_time_warp_seq, (int)time_warp_seq); | |
656 | ||
657 | inode->i_mtime = *mtime; | |
658 | inode->i_atime = *atime; | |
659 | ci->i_time_warp_seq = time_warp_seq; | |
660 | } else if (time_warp_seq == ci->i_time_warp_seq) { | |
661 | /* nobody did utimes(); take the max */ | |
662 | if (timespec_compare(mtime, &inode->i_mtime) > 0) { | |
663 | dout("mtime %ld.%09ld -> %ld.%09ld inc\n", | |
664 | inode->i_mtime.tv_sec, | |
665 | inode->i_mtime.tv_nsec, | |
666 | mtime->tv_sec, mtime->tv_nsec); | |
667 | inode->i_mtime = *mtime; | |
668 | } | |
669 | if (timespec_compare(atime, &inode->i_atime) > 0) { | |
670 | dout("atime %ld.%09ld -> %ld.%09ld inc\n", | |
671 | inode->i_atime.tv_sec, | |
672 | inode->i_atime.tv_nsec, | |
673 | atime->tv_sec, atime->tv_nsec); | |
674 | inode->i_atime = *atime; | |
675 | } | |
676 | } else if (issued & CEPH_CAP_FILE_EXCL) { | |
677 | /* we did a utimes(); ignore mds values */ | |
678 | } else { | |
679 | warn = 1; | |
680 | } | |
681 | } else { | |
d8672d64 | 682 | /* we have no write|excl caps; whatever the MDS says is true */ |
355da1eb SW |
683 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { |
684 | inode->i_ctime = *ctime; | |
685 | inode->i_mtime = *mtime; | |
686 | inode->i_atime = *atime; | |
687 | ci->i_time_warp_seq = time_warp_seq; | |
688 | } else { | |
689 | warn = 1; | |
690 | } | |
691 | } | |
692 | if (warn) /* time_warp_seq shouldn't go backwards */ | |
693 | dout("%p mds time_warp_seq %llu < %u\n", | |
694 | inode, time_warp_seq, ci->i_time_warp_seq); | |
695 | } | |
696 | ||
697 | /* | |
698 | * Populate an inode based on info from mds. May be called on new or | |
699 | * existing inodes. | |
700 | */ | |
01deead0 | 701 | static int fill_inode(struct inode *inode, struct page *locked_page, |
355da1eb SW |
702 | struct ceph_mds_reply_info_in *iinfo, |
703 | struct ceph_mds_reply_dirfrag *dirinfo, | |
704 | struct ceph_mds_session *session, | |
705 | unsigned long ttl_from, int cap_fmode, | |
706 | struct ceph_cap_reservation *caps_reservation) | |
707 | { | |
d9df2783 | 708 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
355da1eb SW |
709 | struct ceph_mds_reply_inode *info = iinfo->in; |
710 | struct ceph_inode_info *ci = ceph_inode(inode); | |
f98a128a | 711 | int issued = 0, implemented, new_issued; |
355da1eb | 712 | struct timespec mtime, atime, ctime; |
355da1eb | 713 | struct ceph_buffer *xattr_blob = NULL; |
d9df2783 | 714 | struct ceph_cap *new_cap = NULL; |
355da1eb | 715 | int err = 0; |
d9df2783 | 716 | bool wake = false; |
f98a128a YZ |
717 | bool queue_trunc = false; |
718 | bool new_version = false; | |
31c542a1 | 719 | bool fill_inline = false; |
355da1eb SW |
720 | |
721 | dout("fill_inode %p ino %llx.%llx v %llu had %llu\n", | |
722 | inode, ceph_vinop(inode), le64_to_cpu(info->version), | |
723 | ci->i_version); | |
724 | ||
d9df2783 YZ |
725 | /* prealloc new cap struct */ |
726 | if (info->cap.caps && ceph_snap(inode) == CEPH_NOSNAP) | |
727 | new_cap = ceph_get_cap(mdsc, caps_reservation); | |
728 | ||
355da1eb SW |
729 | /* |
730 | * prealloc xattr data, if it looks like we'll need it. only | |
731 | * if len > 4 (meaning there are actually xattrs; the first 4 | |
732 | * bytes are the xattr count). | |
733 | */ | |
734 | if (iinfo->xattr_len > 4) { | |
b6c1d5b8 | 735 | xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); |
355da1eb SW |
736 | if (!xattr_blob) |
737 | pr_err("fill_inode ENOMEM xattr blob %d bytes\n", | |
738 | iinfo->xattr_len); | |
739 | } | |
740 | ||
be655596 | 741 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
742 | |
743 | /* | |
744 | * provided version will be odd if inode value is projected, | |
8bd59e01 SW |
745 | * even if stable. skip the update if we have newer stable |
746 | * info (ours>=theirs, e.g. due to racing mds replies), unless | |
747 | * we are getting projected (unstable) info (in which case the | |
748 | * version is odd, and we want ours>theirs). | |
749 | * us them | |
750 | * 2 2 skip | |
751 | * 3 2 skip | |
752 | * 3 3 update | |
355da1eb | 753 | */ |
f98a128a YZ |
754 | if (ci->i_version == 0 || |
755 | ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && | |
756 | le64_to_cpu(info->version) > (ci->i_version & ~1))) | |
757 | new_version = true; | |
758 | ||
355da1eb SW |
759 | issued = __ceph_caps_issued(ci, &implemented); |
760 | issued |= implemented | __ceph_caps_dirty(ci); | |
f98a128a | 761 | new_issued = ~issued & le32_to_cpu(info->cap.caps); |
355da1eb SW |
762 | |
763 | /* update inode */ | |
764 | ci->i_version = le64_to_cpu(info->version); | |
765 | inode->i_version++; | |
766 | inode->i_rdev = le32_to_cpu(info->rdev); | |
f98a128a | 767 | inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; |
355da1eb | 768 | |
f98a128a YZ |
769 | if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) && |
770 | (issued & CEPH_CAP_AUTH_EXCL) == 0) { | |
355da1eb | 771 | inode->i_mode = le32_to_cpu(info->mode); |
ab871b90 EB |
772 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid)); |
773 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid)); | |
355da1eb | 774 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, |
bd2bae6a EB |
775 | from_kuid(&init_user_ns, inode->i_uid), |
776 | from_kgid(&init_user_ns, inode->i_gid)); | |
355da1eb SW |
777 | } |
778 | ||
f98a128a YZ |
779 | if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) && |
780 | (issued & CEPH_CAP_LINK_EXCL) == 0) | |
bfe86848 | 781 | set_nlink(inode, le32_to_cpu(info->nlink)); |
355da1eb | 782 | |
f98a128a YZ |
783 | if (new_version || (new_issued & CEPH_CAP_ANY_RD)) { |
784 | /* be careful with mtime, atime, size */ | |
785 | ceph_decode_timespec(&atime, &info->atime); | |
786 | ceph_decode_timespec(&mtime, &info->mtime); | |
787 | ceph_decode_timespec(&ctime, &info->ctime); | |
788 | ceph_fill_file_time(inode, issued, | |
789 | le32_to_cpu(info->time_warp_seq), | |
790 | &ctime, &mtime, &atime); | |
791 | } | |
792 | ||
793 | if (new_version || | |
794 | (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { | |
10183a69 YZ |
795 | if (ci->i_layout.fl_pg_pool != info->layout.fl_pg_pool) |
796 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; | |
f98a128a | 797 | ci->i_layout = info->layout; |
5ea5c5e0 | 798 | ci->i_pool_ns_len = iinfo->pool_ns_len; |
10183a69 | 799 | |
f98a128a YZ |
800 | queue_trunc = ceph_fill_file_size(inode, issued, |
801 | le32_to_cpu(info->truncate_seq), | |
802 | le64_to_cpu(info->truncate_size), | |
803 | le64_to_cpu(info->size)); | |
804 | /* only update max_size on auth cap */ | |
805 | if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) && | |
806 | ci->i_max_size != le64_to_cpu(info->max_size)) { | |
807 | dout("max_size %lld -> %llu\n", ci->i_max_size, | |
808 | le64_to_cpu(info->max_size)); | |
809 | ci->i_max_size = le64_to_cpu(info->max_size); | |
810 | } | |
811 | } | |
355da1eb SW |
812 | |
813 | /* xattrs */ | |
814 | /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ | |
508b32d8 | 815 | if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) && |
355da1eb SW |
816 | le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { |
817 | if (ci->i_xattrs.blob) | |
818 | ceph_buffer_put(ci->i_xattrs.blob); | |
819 | ci->i_xattrs.blob = xattr_blob; | |
820 | if (xattr_blob) | |
821 | memcpy(ci->i_xattrs.blob->vec.iov_base, | |
822 | iinfo->xattr_data, iinfo->xattr_len); | |
823 | ci->i_xattrs.version = le64_to_cpu(info->xattr_version); | |
7221fe4c | 824 | ceph_forget_all_cached_acls(inode); |
a6424e48 | 825 | xattr_blob = NULL; |
355da1eb SW |
826 | } |
827 | ||
828 | inode->i_mapping->a_ops = &ceph_aops; | |
355da1eb SW |
829 | |
830 | switch (inode->i_mode & S_IFMT) { | |
831 | case S_IFIFO: | |
832 | case S_IFBLK: | |
833 | case S_IFCHR: | |
834 | case S_IFSOCK: | |
835 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | |
836 | inode->i_op = &ceph_file_iops; | |
837 | break; | |
838 | case S_IFREG: | |
839 | inode->i_op = &ceph_file_iops; | |
840 | inode->i_fop = &ceph_file_fops; | |
841 | break; | |
842 | case S_IFLNK: | |
843 | inode->i_op = &ceph_symlink_iops; | |
844 | if (!ci->i_symlink) { | |
810339ec | 845 | u32 symlen = iinfo->symlink_len; |
355da1eb SW |
846 | char *sym; |
847 | ||
be655596 | 848 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 849 | |
810339ec | 850 | err = -EINVAL; |
99c88e69 | 851 | if (WARN_ON(symlen != i_size_read(inode))) |
810339ec XW |
852 | goto out; |
853 | ||
355da1eb | 854 | err = -ENOMEM; |
810339ec | 855 | sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS); |
355da1eb SW |
856 | if (!sym) |
857 | goto out; | |
355da1eb | 858 | |
be655596 | 859 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
860 | if (!ci->i_symlink) |
861 | ci->i_symlink = sym; | |
862 | else | |
863 | kfree(sym); /* lost a race */ | |
864 | } | |
ac194dcc | 865 | inode->i_link = ci->i_symlink; |
355da1eb SW |
866 | break; |
867 | case S_IFDIR: | |
868 | inode->i_op = &ceph_dir_iops; | |
869 | inode->i_fop = &ceph_dir_fops; | |
870 | ||
14303d20 SW |
871 | ci->i_dir_layout = iinfo->dir_layout; |
872 | ||
355da1eb SW |
873 | ci->i_files = le64_to_cpu(info->files); |
874 | ci->i_subdirs = le64_to_cpu(info->subdirs); | |
875 | ci->i_rbytes = le64_to_cpu(info->rbytes); | |
876 | ci->i_rfiles = le64_to_cpu(info->rfiles); | |
877 | ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); | |
878 | ceph_decode_timespec(&ci->i_rctime, &info->rctime); | |
355da1eb SW |
879 | break; |
880 | default: | |
881 | pr_err("fill_inode %llx.%llx BAD mode 0%o\n", | |
882 | ceph_vinop(inode), inode->i_mode); | |
883 | } | |
884 | ||
355da1eb SW |
885 | /* were we issued a capability? */ |
886 | if (info->cap.caps) { | |
887 | if (ceph_snap(inode) == CEPH_NOSNAP) { | |
2f92b3d0 | 888 | unsigned caps = le32_to_cpu(info->cap.caps); |
355da1eb SW |
889 | ceph_add_cap(inode, session, |
890 | le64_to_cpu(info->cap.cap_id), | |
2f92b3d0 | 891 | cap_fmode, caps, |
355da1eb SW |
892 | le32_to_cpu(info->cap.wanted), |
893 | le32_to_cpu(info->cap.seq), | |
894 | le32_to_cpu(info->cap.mseq), | |
895 | le64_to_cpu(info->cap.realm), | |
d9df2783 | 896 | info->cap.flags, &new_cap); |
2f92b3d0 YZ |
897 | |
898 | /* set dir completion flag? */ | |
899 | if (S_ISDIR(inode->i_mode) && | |
900 | ci->i_files == 0 && ci->i_subdirs == 0 && | |
901 | (caps & CEPH_CAP_FILE_SHARED) && | |
902 | (issued & CEPH_CAP_FILE_EXCL) == 0 && | |
903 | !__ceph_dir_is_complete(ci)) { | |
904 | dout(" marking %p complete (empty)\n", inode); | |
fdd4e158 | 905 | i_size_write(inode, 0); |
2f92b3d0 | 906 | __ceph_dir_set_complete(ci, |
fdd4e158 YZ |
907 | atomic64_read(&ci->i_release_count), |
908 | atomic64_read(&ci->i_ordered_count)); | |
2f92b3d0 YZ |
909 | } |
910 | ||
d9df2783 | 911 | wake = true; |
355da1eb | 912 | } else { |
355da1eb SW |
913 | dout(" %p got snap_caps %s\n", inode, |
914 | ceph_cap_string(le32_to_cpu(info->cap.caps))); | |
915 | ci->i_snap_caps |= le32_to_cpu(info->cap.caps); | |
916 | if (cap_fmode >= 0) | |
917 | __ceph_get_fmode(ci, cap_fmode); | |
355da1eb | 918 | } |
04d000eb | 919 | } else if (cap_fmode >= 0) { |
f3ae1b97 | 920 | pr_warn("mds issued no caps on %llx.%llx\n", |
04d000eb SW |
921 | ceph_vinop(inode)); |
922 | __ceph_get_fmode(ci, cap_fmode); | |
355da1eb | 923 | } |
31c542a1 YZ |
924 | |
925 | if (iinfo->inline_version > 0 && | |
926 | iinfo->inline_version >= ci->i_inline_version) { | |
927 | int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; | |
928 | ci->i_inline_version = iinfo->inline_version; | |
929 | if (ci->i_inline_version != CEPH_INLINE_NONE && | |
01deead0 YZ |
930 | (locked_page || |
931 | (le32_to_cpu(info->cap.caps) & cache_caps))) | |
31c542a1 YZ |
932 | fill_inline = true; |
933 | } | |
934 | ||
be655596 | 935 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 936 | |
31c542a1 | 937 | if (fill_inline) |
01deead0 | 938 | ceph_fill_inline_data(inode, locked_page, |
31c542a1 YZ |
939 | iinfo->inline_data, iinfo->inline_len); |
940 | ||
d9df2783 YZ |
941 | if (wake) |
942 | wake_up_all(&ci->i_cap_wq); | |
943 | ||
355da1eb SW |
944 | /* queue truncate if we saw i_size decrease */ |
945 | if (queue_trunc) | |
3c6f6b79 | 946 | ceph_queue_vmtruncate(inode); |
355da1eb SW |
947 | |
948 | /* populate frag tree */ | |
3e7fbe9c YZ |
949 | if (S_ISDIR(inode->i_mode)) |
950 | ceph_fill_fragtree(inode, &info->fragtree, dirinfo); | |
355da1eb SW |
951 | |
952 | /* update delegation info? */ | |
953 | if (dirinfo) | |
954 | ceph_fill_dirfrag(inode, dirinfo); | |
955 | ||
956 | err = 0; | |
355da1eb | 957 | out: |
d9df2783 YZ |
958 | if (new_cap) |
959 | ceph_put_cap(mdsc, new_cap); | |
b6c1d5b8 SW |
960 | if (xattr_blob) |
961 | ceph_buffer_put(xattr_blob); | |
355da1eb SW |
962 | return err; |
963 | } | |
964 | ||
965 | /* | |
966 | * caller should hold session s_mutex. | |
967 | */ | |
968 | static void update_dentry_lease(struct dentry *dentry, | |
969 | struct ceph_mds_reply_lease *lease, | |
970 | struct ceph_mds_session *session, | |
971 | unsigned long from_time) | |
972 | { | |
973 | struct ceph_dentry_info *di = ceph_dentry(dentry); | |
974 | long unsigned duration = le32_to_cpu(lease->duration_ms); | |
975 | long unsigned ttl = from_time + (duration * HZ) / 1000; | |
976 | long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; | |
977 | struct inode *dir; | |
978 | ||
979 | /* only track leases on regular dentries */ | |
980 | if (dentry->d_op != &ceph_dentry_ops) | |
981 | return; | |
982 | ||
983 | spin_lock(&dentry->d_lock); | |
2f90b852 SW |
984 | dout("update_dentry_lease %p duration %lu ms ttl %lu\n", |
985 | dentry, duration, ttl); | |
355da1eb SW |
986 | |
987 | /* make lease_rdcache_gen match directory */ | |
2b0143b5 | 988 | dir = d_inode(dentry->d_parent); |
355da1eb SW |
989 | di->lease_shared_gen = ceph_inode(dir)->i_shared_gen; |
990 | ||
2f90b852 | 991 | if (duration == 0) |
355da1eb SW |
992 | goto out_unlock; |
993 | ||
994 | if (di->lease_gen == session->s_cap_gen && | |
995 | time_before(ttl, dentry->d_time)) | |
996 | goto out_unlock; /* we already have a newer lease. */ | |
997 | ||
998 | if (di->lease_session && di->lease_session != session) | |
999 | goto out_unlock; | |
1000 | ||
1001 | ceph_dentry_lru_touch(dentry); | |
1002 | ||
1003 | if (!di->lease_session) | |
1004 | di->lease_session = ceph_get_mds_session(session); | |
1005 | di->lease_gen = session->s_cap_gen; | |
1006 | di->lease_seq = le32_to_cpu(lease->seq); | |
1007 | di->lease_renew_after = half_ttl; | |
1008 | di->lease_renew_from = 0; | |
1009 | dentry->d_time = ttl; | |
1010 | out_unlock: | |
1011 | spin_unlock(&dentry->d_lock); | |
1012 | return; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * splice a dentry to an inode. | |
1017 | * caller must hold directory i_mutex for this to be safe. | |
355da1eb | 1018 | */ |
f7380af0 | 1019 | static struct dentry *splice_dentry(struct dentry *dn, struct inode *in) |
355da1eb SW |
1020 | { |
1021 | struct dentry *realdn; | |
1022 | ||
2b0143b5 | 1023 | BUG_ON(d_inode(dn)); |
1cd3935b | 1024 | |
355da1eb SW |
1025 | /* dn must be unhashed */ |
1026 | if (!d_unhashed(dn)) | |
1027 | d_drop(dn); | |
41d28bca | 1028 | realdn = d_splice_alias(in, dn); |
355da1eb | 1029 | if (IS_ERR(realdn)) { |
d69ed05a SW |
1030 | pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n", |
1031 | PTR_ERR(realdn), dn, in, ceph_vinop(in)); | |
355da1eb SW |
1032 | dn = realdn; /* note realdn contains the error */ |
1033 | goto out; | |
1034 | } else if (realdn) { | |
1035 | dout("dn %p (%d) spliced with %p (%d) " | |
1036 | "inode %p ino %llx.%llx\n", | |
84d08fa8 AV |
1037 | dn, d_count(dn), |
1038 | realdn, d_count(realdn), | |
2b0143b5 | 1039 | d_inode(realdn), ceph_vinop(d_inode(realdn))); |
355da1eb SW |
1040 | dput(dn); |
1041 | dn = realdn; | |
1042 | } else { | |
1043 | BUG_ON(!ceph_dentry(dn)); | |
355da1eb | 1044 | dout("dn %p attached to %p ino %llx.%llx\n", |
2b0143b5 | 1045 | dn, d_inode(dn), ceph_vinop(d_inode(dn))); |
355da1eb | 1046 | } |
355da1eb SW |
1047 | out: |
1048 | return dn; | |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * Incorporate results into the local cache. This is either just | |
1053 | * one inode, or a directory, dentry, and possibly linked-to inode (e.g., | |
1054 | * after a lookup). | |
1055 | * | |
1056 | * A reply may contain | |
1057 | * a directory inode along with a dentry. | |
1058 | * and/or a target inode | |
1059 | * | |
1060 | * Called with snap_rwsem (read). | |
1061 | */ | |
1062 | int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req, | |
1063 | struct ceph_mds_session *session) | |
1064 | { | |
1065 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | |
1066 | struct inode *in = NULL; | |
355da1eb | 1067 | struct ceph_vino vino; |
3d14c5d2 | 1068 | struct ceph_fs_client *fsc = ceph_sb_to_client(sb); |
355da1eb SW |
1069 | int err = 0; |
1070 | ||
1071 | dout("fill_trace %p is_dentry %d is_target %d\n", req, | |
1072 | rinfo->head->is_dentry, rinfo->head->is_target); | |
1073 | ||
1074 | #if 0 | |
1075 | /* | |
1076 | * Debugging hook: | |
1077 | * | |
1078 | * If we resend completed ops to a recovering mds, we get no | |
1079 | * trace. Since that is very rare, pretend this is the case | |
1080 | * to ensure the 'no trace' handlers in the callers behave. | |
1081 | * | |
1082 | * Fill in inodes unconditionally to avoid breaking cap | |
1083 | * invariants. | |
1084 | */ | |
1085 | if (rinfo->head->op & CEPH_MDS_OP_WRITE) { | |
1086 | pr_info("fill_trace faking empty trace on %lld %s\n", | |
1087 | req->r_tid, ceph_mds_op_name(rinfo->head->op)); | |
1088 | if (rinfo->head->is_dentry) { | |
1089 | rinfo->head->is_dentry = 0; | |
1090 | err = fill_inode(req->r_locked_dir, | |
1091 | &rinfo->diri, rinfo->dirfrag, | |
1092 | session, req->r_request_started, -1); | |
1093 | } | |
1094 | if (rinfo->head->is_target) { | |
1095 | rinfo->head->is_target = 0; | |
1096 | ininfo = rinfo->targeti.in; | |
1097 | vino.ino = le64_to_cpu(ininfo->ino); | |
1098 | vino.snap = le64_to_cpu(ininfo->snapid); | |
1099 | in = ceph_get_inode(sb, vino); | |
1100 | err = fill_inode(in, &rinfo->targeti, NULL, | |
1101 | session, req->r_request_started, | |
1102 | req->r_fmode); | |
1103 | iput(in); | |
1104 | } | |
1105 | } | |
1106 | #endif | |
1107 | ||
1108 | if (!rinfo->head->is_target && !rinfo->head->is_dentry) { | |
1109 | dout("fill_trace reply is empty!\n"); | |
167c9e35 SW |
1110 | if (rinfo->head->result == 0 && req->r_locked_dir) |
1111 | ceph_invalidate_dir_request(req); | |
355da1eb SW |
1112 | return 0; |
1113 | } | |
1114 | ||
1115 | if (rinfo->head->is_dentry) { | |
5b1daecd SW |
1116 | struct inode *dir = req->r_locked_dir; |
1117 | ||
6c5e50fa | 1118 | if (dir) { |
01deead0 YZ |
1119 | err = fill_inode(dir, NULL, |
1120 | &rinfo->diri, rinfo->dirfrag, | |
6c5e50fa SW |
1121 | session, req->r_request_started, -1, |
1122 | &req->r_caps_reservation); | |
1123 | if (err < 0) | |
19913b4e | 1124 | goto done; |
6c5e50fa SW |
1125 | } else { |
1126 | WARN_ON_ONCE(1); | |
1127 | } | |
19913b4e YZ |
1128 | |
1129 | if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME) { | |
1130 | struct qstr dname; | |
1131 | struct dentry *dn, *parent; | |
1132 | ||
1133 | BUG_ON(!rinfo->head->is_target); | |
1134 | BUG_ON(req->r_dentry); | |
1135 | ||
1136 | parent = d_find_any_alias(dir); | |
1137 | BUG_ON(!parent); | |
1138 | ||
1139 | dname.name = rinfo->dname; | |
1140 | dname.len = rinfo->dname_len; | |
1141 | dname.hash = full_name_hash(dname.name, dname.len); | |
1142 | vino.ino = le64_to_cpu(rinfo->targeti.in->ino); | |
1143 | vino.snap = le64_to_cpu(rinfo->targeti.in->snapid); | |
1144 | retry_lookup: | |
1145 | dn = d_lookup(parent, &dname); | |
1146 | dout("d_lookup on parent=%p name=%.*s got %p\n", | |
1147 | parent, dname.len, dname.name, dn); | |
1148 | ||
1149 | if (!dn) { | |
1150 | dn = d_alloc(parent, &dname); | |
1151 | dout("d_alloc %p '%.*s' = %p\n", parent, | |
1152 | dname.len, dname.name, dn); | |
1153 | if (dn == NULL) { | |
1154 | dput(parent); | |
1155 | err = -ENOMEM; | |
1156 | goto done; | |
1157 | } | |
1158 | err = ceph_init_dentry(dn); | |
1159 | if (err < 0) { | |
1160 | dput(dn); | |
1161 | dput(parent); | |
1162 | goto done; | |
1163 | } | |
2b0143b5 DH |
1164 | } else if (d_really_is_positive(dn) && |
1165 | (ceph_ino(d_inode(dn)) != vino.ino || | |
1166 | ceph_snap(d_inode(dn)) != vino.snap)) { | |
19913b4e | 1167 | dout(" dn %p points to wrong inode %p\n", |
2b0143b5 | 1168 | dn, d_inode(dn)); |
19913b4e YZ |
1169 | d_delete(dn); |
1170 | dput(dn); | |
1171 | goto retry_lookup; | |
1172 | } | |
1173 | ||
1174 | req->r_dentry = dn; | |
1175 | dput(parent); | |
1176 | } | |
5b1daecd SW |
1177 | } |
1178 | ||
86b58d13 YZ |
1179 | if (rinfo->head->is_target) { |
1180 | vino.ino = le64_to_cpu(rinfo->targeti.in->ino); | |
1181 | vino.snap = le64_to_cpu(rinfo->targeti.in->snapid); | |
1182 | ||
1183 | in = ceph_get_inode(sb, vino); | |
1184 | if (IS_ERR(in)) { | |
1185 | err = PTR_ERR(in); | |
1186 | goto done; | |
1187 | } | |
1188 | req->r_target_inode = in; | |
1189 | ||
01deead0 | 1190 | err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL, |
86b58d13 | 1191 | session, req->r_request_started, |
48193012 | 1192 | (!req->r_aborted && rinfo->head->result == 0) ? |
86b58d13 YZ |
1193 | req->r_fmode : -1, |
1194 | &req->r_caps_reservation); | |
1195 | if (err < 0) { | |
1196 | pr_err("fill_inode badness %p %llx.%llx\n", | |
1197 | in, ceph_vinop(in)); | |
1198 | goto done; | |
1199 | } | |
1200 | } | |
1201 | ||
9358c6d4 SW |
1202 | /* |
1203 | * ignore null lease/binding on snapdir ENOENT, or else we | |
1204 | * will have trouble splicing in the virtual snapdir later | |
1205 | */ | |
1206 | if (rinfo->head->is_dentry && !req->r_aborted && | |
6c5e50fa | 1207 | req->r_locked_dir && |
9358c6d4 | 1208 | (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name, |
3d14c5d2 | 1209 | fsc->mount_options->snapdir_name, |
9358c6d4 | 1210 | req->r_dentry->d_name.len))) { |
355da1eb SW |
1211 | /* |
1212 | * lookup link rename : null -> possibly existing inode | |
1213 | * mknod symlink mkdir : null -> new inode | |
1214 | * unlink : linked -> null | |
1215 | */ | |
1216 | struct inode *dir = req->r_locked_dir; | |
1217 | struct dentry *dn = req->r_dentry; | |
1218 | bool have_dir_cap, have_lease; | |
1219 | ||
1220 | BUG_ON(!dn); | |
1221 | BUG_ON(!dir); | |
2b0143b5 | 1222 | BUG_ON(d_inode(dn->d_parent) != dir); |
355da1eb SW |
1223 | BUG_ON(ceph_ino(dir) != |
1224 | le64_to_cpu(rinfo->diri.in->ino)); | |
1225 | BUG_ON(ceph_snap(dir) != | |
1226 | le64_to_cpu(rinfo->diri.in->snapid)); | |
1227 | ||
355da1eb SW |
1228 | /* do we have a lease on the whole dir? */ |
1229 | have_dir_cap = | |
1230 | (le32_to_cpu(rinfo->diri.in->cap.caps) & | |
1231 | CEPH_CAP_FILE_SHARED); | |
1232 | ||
1233 | /* do we have a dn lease? */ | |
1234 | have_lease = have_dir_cap || | |
2f90b852 | 1235 | le32_to_cpu(rinfo->dlease->duration_ms); |
355da1eb SW |
1236 | if (!have_lease) |
1237 | dout("fill_trace no dentry lease or dir cap\n"); | |
1238 | ||
1239 | /* rename? */ | |
1240 | if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { | |
0a8a70f9 YZ |
1241 | struct inode *olddir = req->r_old_dentry_dir; |
1242 | BUG_ON(!olddir); | |
1243 | ||
a455589f | 1244 | dout(" src %p '%pd' dst %p '%pd'\n", |
355da1eb | 1245 | req->r_old_dentry, |
a455589f AV |
1246 | req->r_old_dentry, |
1247 | dn, dn); | |
355da1eb SW |
1248 | dout("fill_trace doing d_move %p -> %p\n", |
1249 | req->r_old_dentry, dn); | |
c10f5e12 | 1250 | |
fdd4e158 YZ |
1251 | /* d_move screws up sibling dentries' offsets */ |
1252 | ceph_dir_clear_ordered(dir); | |
1253 | ceph_dir_clear_ordered(olddir); | |
1254 | ||
355da1eb | 1255 | d_move(req->r_old_dentry, dn); |
a455589f AV |
1256 | dout(" src %p '%pd' dst %p '%pd'\n", |
1257 | req->r_old_dentry, | |
355da1eb | 1258 | req->r_old_dentry, |
a455589f | 1259 | dn, dn); |
81a6cf2d | 1260 | |
c4a29f26 SW |
1261 | /* ensure target dentry is invalidated, despite |
1262 | rehashing bug in vfs_rename_dir */ | |
81a6cf2d SW |
1263 | ceph_invalidate_dentry_lease(dn); |
1264 | ||
99ccbd22 | 1265 | dout("dn %p gets new offset %lld\n", req->r_old_dentry, |
1cd3935b | 1266 | ceph_dentry(req->r_old_dentry)->offset); |
81a6cf2d | 1267 | |
355da1eb | 1268 | dn = req->r_old_dentry; /* use old_dentry */ |
355da1eb SW |
1269 | } |
1270 | ||
1271 | /* null dentry? */ | |
1272 | if (!rinfo->head->is_target) { | |
1273 | dout("fill_trace null dentry\n"); | |
2b0143b5 | 1274 | if (d_really_is_positive(dn)) { |
70db4f36 | 1275 | ceph_dir_clear_ordered(dir); |
355da1eb SW |
1276 | dout("d_delete %p\n", dn); |
1277 | d_delete(dn); | |
1278 | } else { | |
355da1eb | 1279 | if (have_lease && d_unhashed(dn)) |
f8b31710 | 1280 | d_add(dn, NULL); |
355da1eb SW |
1281 | update_dentry_lease(dn, rinfo->dlease, |
1282 | session, | |
1283 | req->r_request_started); | |
1284 | } | |
1285 | goto done; | |
1286 | } | |
1287 | ||
1288 | /* attach proper inode */ | |
2b0143b5 | 1289 | if (d_really_is_negative(dn)) { |
70db4f36 | 1290 | ceph_dir_clear_ordered(dir); |
86b58d13 | 1291 | ihold(in); |
f7380af0 | 1292 | dn = splice_dentry(dn, in); |
355da1eb SW |
1293 | if (IS_ERR(dn)) { |
1294 | err = PTR_ERR(dn); | |
1295 | goto done; | |
1296 | } | |
1297 | req->r_dentry = dn; /* may have spliced */ | |
2b0143b5 | 1298 | } else if (d_really_is_positive(dn) && d_inode(dn) != in) { |
355da1eb | 1299 | dout(" %p links to %p %llx.%llx, not %llx.%llx\n", |
2b0143b5 | 1300 | dn, d_inode(dn), ceph_vinop(d_inode(dn)), |
86b58d13 | 1301 | ceph_vinop(in)); |
200fd27c | 1302 | d_invalidate(dn); |
355da1eb | 1303 | have_lease = false; |
355da1eb SW |
1304 | } |
1305 | ||
1306 | if (have_lease) | |
1307 | update_dentry_lease(dn, rinfo->dlease, session, | |
1308 | req->r_request_started); | |
1309 | dout(" final dn %p\n", dn); | |
86b58d13 YZ |
1310 | } else if (!req->r_aborted && |
1311 | (req->r_op == CEPH_MDS_OP_LOOKUPSNAP || | |
1312 | req->r_op == CEPH_MDS_OP_MKSNAP)) { | |
355da1eb | 1313 | struct dentry *dn = req->r_dentry; |
0a8a70f9 | 1314 | struct inode *dir = req->r_locked_dir; |
355da1eb SW |
1315 | |
1316 | /* fill out a snapdir LOOKUPSNAP dentry */ | |
1317 | BUG_ON(!dn); | |
0a8a70f9 YZ |
1318 | BUG_ON(!dir); |
1319 | BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR); | |
355da1eb | 1320 | dout(" linking snapped dir %p to dn %p\n", in, dn); |
70db4f36 | 1321 | ceph_dir_clear_ordered(dir); |
86b58d13 | 1322 | ihold(in); |
f7380af0 | 1323 | dn = splice_dentry(dn, in); |
355da1eb SW |
1324 | if (IS_ERR(dn)) { |
1325 | err = PTR_ERR(dn); | |
1326 | goto done; | |
1327 | } | |
1328 | req->r_dentry = dn; /* may have spliced */ | |
355da1eb | 1329 | } |
355da1eb SW |
1330 | done: |
1331 | dout("fill_trace done err=%d\n", err); | |
1332 | return err; | |
1333 | } | |
1334 | ||
1335 | /* | |
1336 | * Prepopulate our cache with readdir results, leases, etc. | |
1337 | */ | |
79f9f99a SW |
1338 | static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req, |
1339 | struct ceph_mds_session *session) | |
1340 | { | |
1341 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | |
1342 | int i, err = 0; | |
1343 | ||
1344 | for (i = 0; i < rinfo->dir_nr; i++) { | |
2a5beea3 | 1345 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
79f9f99a SW |
1346 | struct ceph_vino vino; |
1347 | struct inode *in; | |
1348 | int rc; | |
1349 | ||
2a5beea3 YZ |
1350 | vino.ino = le64_to_cpu(rde->inode.in->ino); |
1351 | vino.snap = le64_to_cpu(rde->inode.in->snapid); | |
79f9f99a SW |
1352 | |
1353 | in = ceph_get_inode(req->r_dentry->d_sb, vino); | |
1354 | if (IS_ERR(in)) { | |
1355 | err = PTR_ERR(in); | |
1356 | dout("new_inode badness got %d\n", err); | |
1357 | continue; | |
1358 | } | |
2a5beea3 | 1359 | rc = fill_inode(in, NULL, &rde->inode, NULL, session, |
79f9f99a SW |
1360 | req->r_request_started, -1, |
1361 | &req->r_caps_reservation); | |
1362 | if (rc < 0) { | |
1363 | pr_err("fill_inode badness on %p got %d\n", in, rc); | |
1364 | err = rc; | |
79f9f99a | 1365 | } |
209ae762 | 1366 | iput(in); |
79f9f99a SW |
1367 | } |
1368 | ||
1369 | return err; | |
1370 | } | |
1371 | ||
fdd4e158 YZ |
1372 | void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl) |
1373 | { | |
1374 | if (ctl->page) { | |
1375 | kunmap(ctl->page); | |
09cbfeaf | 1376 | put_page(ctl->page); |
fdd4e158 YZ |
1377 | ctl->page = NULL; |
1378 | } | |
1379 | } | |
1380 | ||
1381 | static int fill_readdir_cache(struct inode *dir, struct dentry *dn, | |
1382 | struct ceph_readdir_cache_control *ctl, | |
1383 | struct ceph_mds_request *req) | |
1384 | { | |
1385 | struct ceph_inode_info *ci = ceph_inode(dir); | |
09cbfeaf | 1386 | unsigned nsize = PAGE_SIZE / sizeof(struct dentry*); |
fdd4e158 YZ |
1387 | unsigned idx = ctl->index % nsize; |
1388 | pgoff_t pgoff = ctl->index / nsize; | |
1389 | ||
1390 | if (!ctl->page || pgoff != page_index(ctl->page)) { | |
1391 | ceph_readdir_cache_release(ctl); | |
af5e5eb5 YZ |
1392 | if (idx == 0) |
1393 | ctl->page = grab_cache_page(&dir->i_data, pgoff); | |
1394 | else | |
1395 | ctl->page = find_lock_page(&dir->i_data, pgoff); | |
fdd4e158 YZ |
1396 | if (!ctl->page) { |
1397 | ctl->index = -1; | |
af5e5eb5 | 1398 | return idx == 0 ? -ENOMEM : 0; |
fdd4e158 YZ |
1399 | } |
1400 | /* reading/filling the cache are serialized by | |
1401 | * i_mutex, no need to use page lock */ | |
1402 | unlock_page(ctl->page); | |
1403 | ctl->dentries = kmap(ctl->page); | |
af5e5eb5 | 1404 | if (idx == 0) |
09cbfeaf | 1405 | memset(ctl->dentries, 0, PAGE_SIZE); |
fdd4e158 YZ |
1406 | } |
1407 | ||
1408 | if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) && | |
1409 | req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) { | |
1410 | dout("readdir cache dn %p idx %d\n", dn, ctl->index); | |
1411 | ctl->dentries[idx] = dn; | |
1412 | ctl->index++; | |
1413 | } else { | |
1414 | dout("disable readdir cache\n"); | |
1415 | ctl->index = -1; | |
1416 | } | |
1417 | return 0; | |
1418 | } | |
1419 | ||
355da1eb SW |
1420 | int ceph_readdir_prepopulate(struct ceph_mds_request *req, |
1421 | struct ceph_mds_session *session) | |
1422 | { | |
1423 | struct dentry *parent = req->r_dentry; | |
f3c4ebe6 | 1424 | struct ceph_inode_info *ci = ceph_inode(d_inode(parent)); |
355da1eb SW |
1425 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
1426 | struct qstr dname; | |
1427 | struct dentry *dn; | |
1428 | struct inode *in; | |
315f2408 | 1429 | int err = 0, skipped = 0, ret, i; |
355da1eb SW |
1430 | struct inode *snapdir = NULL; |
1431 | struct ceph_mds_request_head *rhead = req->r_request->front.iov_base; | |
81c6aea5 | 1432 | u32 frag = le32_to_cpu(rhead->args.readdir.frag); |
f3c4ebe6 YZ |
1433 | u32 last_hash = 0; |
1434 | u32 fpos_offset; | |
fdd4e158 YZ |
1435 | struct ceph_readdir_cache_control cache_ctl = {}; |
1436 | ||
1437 | if (req->r_aborted) | |
1438 | return readdir_prepopulate_inodes_only(req, session); | |
81c6aea5 | 1439 | |
f3c4ebe6 YZ |
1440 | if (rinfo->hash_order && req->r_path2) { |
1441 | last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, | |
1442 | req->r_path2, strlen(req->r_path2)); | |
1443 | last_hash = ceph_frag_value(last_hash); | |
1444 | } | |
1445 | ||
81c6aea5 YZ |
1446 | if (rinfo->dir_dir && |
1447 | le32_to_cpu(rinfo->dir_dir->frag) != frag) { | |
1448 | dout("readdir_prepopulate got new frag %x -> %x\n", | |
1449 | frag, le32_to_cpu(rinfo->dir_dir->frag)); | |
1450 | frag = le32_to_cpu(rinfo->dir_dir->frag); | |
f3c4ebe6 YZ |
1451 | if (!rinfo->hash_order) |
1452 | req->r_readdir_offset = 2; | |
81c6aea5 | 1453 | } |
355da1eb SW |
1454 | |
1455 | if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { | |
2b0143b5 | 1456 | snapdir = ceph_get_snapdir(d_inode(parent)); |
355da1eb SW |
1457 | parent = d_find_alias(snapdir); |
1458 | dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", | |
1459 | rinfo->dir_nr, parent); | |
1460 | } else { | |
1461 | dout("readdir_prepopulate %d items under dn %p\n", | |
1462 | rinfo->dir_nr, parent); | |
1463 | if (rinfo->dir_dir) | |
2b0143b5 | 1464 | ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir); |
355da1eb SW |
1465 | } |
1466 | ||
fdd4e158 YZ |
1467 | if (ceph_frag_is_leftmost(frag) && req->r_readdir_offset == 2) { |
1468 | /* note dir version at start of readdir so we can tell | |
1469 | * if any dentries get dropped */ | |
fdd4e158 YZ |
1470 | req->r_dir_release_cnt = atomic64_read(&ci->i_release_count); |
1471 | req->r_dir_ordered_cnt = atomic64_read(&ci->i_ordered_count); | |
1472 | req->r_readdir_cache_idx = 0; | |
1473 | } | |
1474 | ||
1475 | cache_ctl.index = req->r_readdir_cache_idx; | |
f3c4ebe6 | 1476 | fpos_offset = req->r_readdir_offset; |
fdd4e158 | 1477 | |
86b58d13 | 1478 | /* FIXME: release caps/leases if error occurs */ |
355da1eb | 1479 | for (i = 0; i < rinfo->dir_nr; i++) { |
2a5beea3 | 1480 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
355da1eb SW |
1481 | struct ceph_vino vino; |
1482 | ||
2a5beea3 YZ |
1483 | dname.name = rde->name; |
1484 | dname.len = rde->name_len; | |
355da1eb SW |
1485 | dname.hash = full_name_hash(dname.name, dname.len); |
1486 | ||
2a5beea3 YZ |
1487 | vino.ino = le64_to_cpu(rde->inode.in->ino); |
1488 | vino.snap = le64_to_cpu(rde->inode.in->snapid); | |
355da1eb | 1489 | |
f3c4ebe6 YZ |
1490 | if (rinfo->hash_order) { |
1491 | u32 hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash, | |
1492 | rde->name, rde->name_len); | |
1493 | hash = ceph_frag_value(hash); | |
1494 | if (hash != last_hash) | |
1495 | fpos_offset = 2; | |
1496 | last_hash = hash; | |
1497 | rde->offset = ceph_make_fpos(hash, fpos_offset++, true); | |
1498 | } else { | |
1499 | rde->offset = ceph_make_fpos(frag, fpos_offset++, false); | |
1500 | } | |
1501 | ||
355da1eb SW |
1502 | retry_lookup: |
1503 | dn = d_lookup(parent, &dname); | |
1504 | dout("d_lookup on parent=%p name=%.*s got %p\n", | |
1505 | parent, dname.len, dname.name, dn); | |
1506 | ||
1507 | if (!dn) { | |
1508 | dn = d_alloc(parent, &dname); | |
1509 | dout("d_alloc %p '%.*s' = %p\n", parent, | |
1510 | dname.len, dname.name, dn); | |
1511 | if (dn == NULL) { | |
1512 | dout("d_alloc badness\n"); | |
1513 | err = -ENOMEM; | |
1514 | goto out; | |
1515 | } | |
86b58d13 YZ |
1516 | ret = ceph_init_dentry(dn); |
1517 | if (ret < 0) { | |
8c696737 | 1518 | dput(dn); |
86b58d13 | 1519 | err = ret; |
355da1eb | 1520 | goto out; |
8c696737 | 1521 | } |
2b0143b5 DH |
1522 | } else if (d_really_is_positive(dn) && |
1523 | (ceph_ino(d_inode(dn)) != vino.ino || | |
1524 | ceph_snap(d_inode(dn)) != vino.snap)) { | |
355da1eb | 1525 | dout(" dn %p points to wrong inode %p\n", |
2b0143b5 | 1526 | dn, d_inode(dn)); |
355da1eb SW |
1527 | d_delete(dn); |
1528 | dput(dn); | |
1529 | goto retry_lookup; | |
355da1eb SW |
1530 | } |
1531 | ||
355da1eb | 1532 | /* inode */ |
2b0143b5 DH |
1533 | if (d_really_is_positive(dn)) { |
1534 | in = d_inode(dn); | |
355da1eb SW |
1535 | } else { |
1536 | in = ceph_get_inode(parent->d_sb, vino); | |
ac1f12ef | 1537 | if (IS_ERR(in)) { |
355da1eb | 1538 | dout("new_inode badness\n"); |
2744c171 | 1539 | d_drop(dn); |
355da1eb | 1540 | dput(dn); |
ac1f12ef | 1541 | err = PTR_ERR(in); |
355da1eb SW |
1542 | goto out; |
1543 | } | |
355da1eb SW |
1544 | } |
1545 | ||
2a5beea3 | 1546 | ret = fill_inode(in, NULL, &rde->inode, NULL, session, |
fdd4e158 YZ |
1547 | req->r_request_started, -1, |
1548 | &req->r_caps_reservation); | |
1549 | if (ret < 0) { | |
355da1eb | 1550 | pr_err("fill_inode badness on %p\n", in); |
2b0143b5 | 1551 | if (d_really_is_negative(dn)) |
86b58d13 YZ |
1552 | iput(in); |
1553 | d_drop(dn); | |
fdd4e158 | 1554 | err = ret; |
d69ed05a | 1555 | goto next_item; |
355da1eb | 1556 | } |
86b58d13 | 1557 | |
2b0143b5 | 1558 | if (d_really_is_negative(dn)) { |
315f2408 YZ |
1559 | struct dentry *realdn; |
1560 | ||
1561 | if (ceph_security_xattr_deadlock(in)) { | |
1562 | dout(" skip splicing dn %p to inode %p" | |
1563 | " (security xattr deadlock)\n", dn, in); | |
1564 | iput(in); | |
1565 | skipped++; | |
1566 | goto next_item; | |
1567 | } | |
1568 | ||
1569 | realdn = splice_dentry(dn, in); | |
5cba372c YZ |
1570 | if (IS_ERR(realdn)) { |
1571 | err = PTR_ERR(realdn); | |
1572 | d_drop(dn); | |
86b58d13 YZ |
1573 | dn = NULL; |
1574 | goto next_item; | |
1575 | } | |
5cba372c | 1576 | dn = realdn; |
86b58d13 YZ |
1577 | } |
1578 | ||
f3c4ebe6 | 1579 | ceph_dentry(dn)->offset = rde->offset; |
86b58d13 | 1580 | |
2a5beea3 | 1581 | update_dentry_lease(dn, rde->lease, req->r_session, |
86b58d13 | 1582 | req->r_request_started); |
fdd4e158 | 1583 | |
315f2408 | 1584 | if (err == 0 && skipped == 0 && cache_ctl.index >= 0) { |
fdd4e158 YZ |
1585 | ret = fill_readdir_cache(d_inode(parent), dn, |
1586 | &cache_ctl, req); | |
1587 | if (ret < 0) | |
1588 | err = ret; | |
1589 | } | |
d69ed05a SW |
1590 | next_item: |
1591 | if (dn) | |
1592 | dput(dn); | |
355da1eb | 1593 | } |
355da1eb | 1594 | out: |
315f2408 | 1595 | if (err == 0 && skipped == 0) { |
fdd4e158 YZ |
1596 | req->r_did_prepopulate = true; |
1597 | req->r_readdir_cache_idx = cache_ctl.index; | |
1598 | } | |
1599 | ceph_readdir_cache_release(&cache_ctl); | |
355da1eb SW |
1600 | if (snapdir) { |
1601 | iput(snapdir); | |
1602 | dput(parent); | |
1603 | } | |
1604 | dout("readdir_prepopulate done\n"); | |
1605 | return err; | |
1606 | } | |
1607 | ||
1608 | int ceph_inode_set_size(struct inode *inode, loff_t size) | |
1609 | { | |
1610 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1611 | int ret = 0; | |
1612 | ||
be655596 | 1613 | spin_lock(&ci->i_ceph_lock); |
355da1eb | 1614 | dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size); |
99c88e69 | 1615 | i_size_write(inode, size); |
355da1eb SW |
1616 | inode->i_blocks = (size + (1 << 9) - 1) >> 9; |
1617 | ||
1618 | /* tell the MDS if we are approaching max_size */ | |
1619 | if ((size << 1) >= ci->i_max_size && | |
1620 | (ci->i_reported_size << 1) < ci->i_max_size) | |
1621 | ret = 1; | |
1622 | ||
be655596 | 1623 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
1624 | return ret; |
1625 | } | |
1626 | ||
1627 | /* | |
1628 | * Write back inode data in a worker thread. (This can't be done | |
1629 | * in the message handler context.) | |
1630 | */ | |
3c6f6b79 SW |
1631 | void ceph_queue_writeback(struct inode *inode) |
1632 | { | |
15a2015f | 1633 | ihold(inode); |
3c6f6b79 SW |
1634 | if (queue_work(ceph_inode_to_client(inode)->wb_wq, |
1635 | &ceph_inode(inode)->i_wb_work)) { | |
2c27c9a5 | 1636 | dout("ceph_queue_writeback %p\n", inode); |
3c6f6b79 | 1637 | } else { |
2c27c9a5 | 1638 | dout("ceph_queue_writeback %p failed\n", inode); |
15a2015f | 1639 | iput(inode); |
3c6f6b79 SW |
1640 | } |
1641 | } | |
1642 | ||
1643 | static void ceph_writeback_work(struct work_struct *work) | |
355da1eb SW |
1644 | { |
1645 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | |
1646 | i_wb_work); | |
1647 | struct inode *inode = &ci->vfs_inode; | |
1648 | ||
1649 | dout("writeback %p\n", inode); | |
1650 | filemap_fdatawrite(&inode->i_data); | |
1651 | iput(inode); | |
1652 | } | |
1653 | ||
3c6f6b79 SW |
1654 | /* |
1655 | * queue an async invalidation | |
1656 | */ | |
1657 | void ceph_queue_invalidate(struct inode *inode) | |
1658 | { | |
15a2015f | 1659 | ihold(inode); |
3c6f6b79 SW |
1660 | if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq, |
1661 | &ceph_inode(inode)->i_pg_inv_work)) { | |
1662 | dout("ceph_queue_invalidate %p\n", inode); | |
3c6f6b79 SW |
1663 | } else { |
1664 | dout("ceph_queue_invalidate %p failed\n", inode); | |
15a2015f | 1665 | iput(inode); |
3c6f6b79 SW |
1666 | } |
1667 | } | |
1668 | ||
355da1eb SW |
1669 | /* |
1670 | * Invalidate inode pages in a worker thread. (This can't be done | |
1671 | * in the message handler context.) | |
1672 | */ | |
3c6f6b79 | 1673 | static void ceph_invalidate_work(struct work_struct *work) |
355da1eb SW |
1674 | { |
1675 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | |
1676 | i_pg_inv_work); | |
1677 | struct inode *inode = &ci->vfs_inode; | |
6c93df5d | 1678 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
355da1eb SW |
1679 | u32 orig_gen; |
1680 | int check = 0; | |
1681 | ||
b0d7c223 | 1682 | mutex_lock(&ci->i_truncate_mutex); |
6c93df5d YZ |
1683 | |
1684 | if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { | |
1685 | pr_warn_ratelimited("invalidate_pages %p %lld forced umount\n", | |
1686 | inode, ceph_ino(inode)); | |
1687 | mapping_set_error(inode->i_mapping, -EIO); | |
1688 | truncate_pagecache(inode, 0); | |
1689 | mutex_unlock(&ci->i_truncate_mutex); | |
1690 | goto out; | |
1691 | } | |
1692 | ||
be655596 | 1693 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
1694 | dout("invalidate_pages %p gen %d revoking %d\n", inode, |
1695 | ci->i_rdcache_gen, ci->i_rdcache_revoking); | |
cd045cb4 | 1696 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
9563f88c YZ |
1697 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
1698 | check = 1; | |
be655596 | 1699 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 1700 | mutex_unlock(&ci->i_truncate_mutex); |
355da1eb SW |
1701 | goto out; |
1702 | } | |
1703 | orig_gen = ci->i_rdcache_gen; | |
be655596 | 1704 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 1705 | |
4e217b5d | 1706 | truncate_pagecache(inode, 0); |
355da1eb | 1707 | |
be655596 | 1708 | spin_lock(&ci->i_ceph_lock); |
cd045cb4 SW |
1709 | if (orig_gen == ci->i_rdcache_gen && |
1710 | orig_gen == ci->i_rdcache_revoking) { | |
355da1eb SW |
1711 | dout("invalidate_pages %p gen %d successful\n", inode, |
1712 | ci->i_rdcache_gen); | |
cd045cb4 | 1713 | ci->i_rdcache_revoking--; |
355da1eb SW |
1714 | check = 1; |
1715 | } else { | |
cd045cb4 SW |
1716 | dout("invalidate_pages %p gen %d raced, now %d revoking %d\n", |
1717 | inode, orig_gen, ci->i_rdcache_gen, | |
1718 | ci->i_rdcache_revoking); | |
9563f88c YZ |
1719 | if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE)) |
1720 | check = 1; | |
355da1eb | 1721 | } |
be655596 | 1722 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 1723 | mutex_unlock(&ci->i_truncate_mutex); |
9563f88c | 1724 | out: |
355da1eb SW |
1725 | if (check) |
1726 | ceph_check_caps(ci, 0, NULL); | |
355da1eb SW |
1727 | iput(inode); |
1728 | } | |
1729 | ||
1730 | ||
1731 | /* | |
3f99969f | 1732 | * called by trunc_wq; |
355da1eb SW |
1733 | * |
1734 | * We also truncate in a separate thread as well. | |
1735 | */ | |
3c6f6b79 | 1736 | static void ceph_vmtruncate_work(struct work_struct *work) |
355da1eb SW |
1737 | { |
1738 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | |
1739 | i_vmtruncate_work); | |
1740 | struct inode *inode = &ci->vfs_inode; | |
1741 | ||
1742 | dout("vmtruncate_work %p\n", inode); | |
b415bf4f | 1743 | __ceph_do_pending_vmtruncate(inode); |
355da1eb SW |
1744 | iput(inode); |
1745 | } | |
1746 | ||
3c6f6b79 SW |
1747 | /* |
1748 | * Queue an async vmtruncate. If we fail to queue work, we will handle | |
1749 | * the truncation the next time we call __ceph_do_pending_vmtruncate. | |
1750 | */ | |
1751 | void ceph_queue_vmtruncate(struct inode *inode) | |
1752 | { | |
1753 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1754 | ||
15a2015f | 1755 | ihold(inode); |
99ccbd22 | 1756 | |
640ef79d | 1757 | if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq, |
3c6f6b79 SW |
1758 | &ci->i_vmtruncate_work)) { |
1759 | dout("ceph_queue_vmtruncate %p\n", inode); | |
3c6f6b79 SW |
1760 | } else { |
1761 | dout("ceph_queue_vmtruncate %p failed, pending=%d\n", | |
1762 | inode, ci->i_truncate_pending); | |
15a2015f | 1763 | iput(inode); |
3c6f6b79 SW |
1764 | } |
1765 | } | |
1766 | ||
355da1eb | 1767 | /* |
355da1eb SW |
1768 | * Make sure any pending truncation is applied before doing anything |
1769 | * that may depend on it. | |
1770 | */ | |
b415bf4f | 1771 | void __ceph_do_pending_vmtruncate(struct inode *inode) |
355da1eb SW |
1772 | { |
1773 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1774 | u64 to; | |
a85f50b6 | 1775 | int wrbuffer_refs, finish = 0; |
355da1eb | 1776 | |
b0d7c223 | 1777 | mutex_lock(&ci->i_truncate_mutex); |
355da1eb | 1778 | retry: |
be655596 | 1779 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
1780 | if (ci->i_truncate_pending == 0) { |
1781 | dout("__do_pending_vmtruncate %p none pending\n", inode); | |
be655596 | 1782 | spin_unlock(&ci->i_ceph_lock); |
b0d7c223 | 1783 | mutex_unlock(&ci->i_truncate_mutex); |
355da1eb SW |
1784 | return; |
1785 | } | |
1786 | ||
1787 | /* | |
1788 | * make sure any dirty snapped pages are flushed before we | |
1789 | * possibly truncate them.. so write AND block! | |
1790 | */ | |
1791 | if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { | |
1792 | dout("__do_pending_vmtruncate %p flushing snaps first\n", | |
1793 | inode); | |
be655596 | 1794 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
1795 | filemap_write_and_wait_range(&inode->i_data, 0, |
1796 | inode->i_sb->s_maxbytes); | |
1797 | goto retry; | |
1798 | } | |
1799 | ||
b0d7c223 YZ |
1800 | /* there should be no reader or writer */ |
1801 | WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref); | |
1802 | ||
355da1eb SW |
1803 | to = ci->i_truncate_size; |
1804 | wrbuffer_refs = ci->i_wrbuffer_ref; | |
1805 | dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, | |
1806 | ci->i_truncate_pending, to); | |
be655596 | 1807 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 1808 | |
4e217b5d | 1809 | truncate_pagecache(inode, to); |
355da1eb | 1810 | |
be655596 | 1811 | spin_lock(&ci->i_ceph_lock); |
a85f50b6 YZ |
1812 | if (to == ci->i_truncate_size) { |
1813 | ci->i_truncate_pending = 0; | |
1814 | finish = 1; | |
1815 | } | |
be655596 | 1816 | spin_unlock(&ci->i_ceph_lock); |
a85f50b6 YZ |
1817 | if (!finish) |
1818 | goto retry; | |
355da1eb | 1819 | |
b0d7c223 YZ |
1820 | mutex_unlock(&ci->i_truncate_mutex); |
1821 | ||
355da1eb SW |
1822 | if (wrbuffer_refs == 0) |
1823 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | |
a85f50b6 YZ |
1824 | |
1825 | wake_up_all(&ci->i_cap_wq); | |
355da1eb SW |
1826 | } |
1827 | ||
355da1eb SW |
1828 | /* |
1829 | * symlinks | |
1830 | */ | |
355da1eb SW |
1831 | static const struct inode_operations ceph_symlink_iops = { |
1832 | .readlink = generic_readlink, | |
6b255391 | 1833 | .get_link = simple_get_link, |
0b932672 YZ |
1834 | .setattr = ceph_setattr, |
1835 | .getattr = ceph_getattr, | |
1836 | .setxattr = ceph_setxattr, | |
1837 | .getxattr = ceph_getxattr, | |
1838 | .listxattr = ceph_listxattr, | |
1839 | .removexattr = ceph_removexattr, | |
355da1eb SW |
1840 | }; |
1841 | ||
1842 | /* | |
1843 | * setattr | |
1844 | */ | |
1845 | int ceph_setattr(struct dentry *dentry, struct iattr *attr) | |
1846 | { | |
2b0143b5 | 1847 | struct inode *inode = d_inode(dentry); |
355da1eb | 1848 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb SW |
1849 | const unsigned int ia_valid = attr->ia_valid; |
1850 | struct ceph_mds_request *req; | |
3d14c5d2 | 1851 | struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc; |
f66fd9f0 | 1852 | struct ceph_cap_flush *prealloc_cf; |
355da1eb SW |
1853 | int issued; |
1854 | int release = 0, dirtied = 0; | |
1855 | int mask = 0; | |
1856 | int err = 0; | |
fca65b4a | 1857 | int inode_dirty_flags = 0; |
604d1b02 | 1858 | bool lock_snap_rwsem = false; |
355da1eb SW |
1859 | |
1860 | if (ceph_snap(inode) != CEPH_NOSNAP) | |
1861 | return -EROFS; | |
1862 | ||
355da1eb SW |
1863 | err = inode_change_ok(inode, attr); |
1864 | if (err != 0) | |
1865 | return err; | |
1866 | ||
f66fd9f0 YZ |
1867 | prealloc_cf = ceph_alloc_cap_flush(); |
1868 | if (!prealloc_cf) | |
1869 | return -ENOMEM; | |
1870 | ||
355da1eb SW |
1871 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, |
1872 | USE_AUTH_MDS); | |
f66fd9f0 YZ |
1873 | if (IS_ERR(req)) { |
1874 | ceph_free_cap_flush(prealloc_cf); | |
355da1eb | 1875 | return PTR_ERR(req); |
f66fd9f0 | 1876 | } |
355da1eb | 1877 | |
be655596 | 1878 | spin_lock(&ci->i_ceph_lock); |
355da1eb | 1879 | issued = __ceph_caps_issued(ci, NULL); |
604d1b02 YZ |
1880 | |
1881 | if (!ci->i_head_snapc && | |
1882 | (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) { | |
1883 | lock_snap_rwsem = true; | |
1884 | if (!down_read_trylock(&mdsc->snap_rwsem)) { | |
1885 | spin_unlock(&ci->i_ceph_lock); | |
1886 | down_read(&mdsc->snap_rwsem); | |
1887 | spin_lock(&ci->i_ceph_lock); | |
1888 | issued = __ceph_caps_issued(ci, NULL); | |
1889 | } | |
1890 | } | |
1891 | ||
355da1eb SW |
1892 | dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); |
1893 | ||
1894 | if (ia_valid & ATTR_UID) { | |
1895 | dout("setattr %p uid %d -> %d\n", inode, | |
bd2bae6a EB |
1896 | from_kuid(&init_user_ns, inode->i_uid), |
1897 | from_kuid(&init_user_ns, attr->ia_uid)); | |
355da1eb SW |
1898 | if (issued & CEPH_CAP_AUTH_EXCL) { |
1899 | inode->i_uid = attr->ia_uid; | |
1900 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
1901 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
ab871b90 EB |
1902 | !uid_eq(attr->ia_uid, inode->i_uid)) { |
1903 | req->r_args.setattr.uid = cpu_to_le32( | |
1904 | from_kuid(&init_user_ns, attr->ia_uid)); | |
355da1eb SW |
1905 | mask |= CEPH_SETATTR_UID; |
1906 | release |= CEPH_CAP_AUTH_SHARED; | |
1907 | } | |
1908 | } | |
1909 | if (ia_valid & ATTR_GID) { | |
1910 | dout("setattr %p gid %d -> %d\n", inode, | |
bd2bae6a EB |
1911 | from_kgid(&init_user_ns, inode->i_gid), |
1912 | from_kgid(&init_user_ns, attr->ia_gid)); | |
355da1eb SW |
1913 | if (issued & CEPH_CAP_AUTH_EXCL) { |
1914 | inode->i_gid = attr->ia_gid; | |
1915 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
1916 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
ab871b90 EB |
1917 | !gid_eq(attr->ia_gid, inode->i_gid)) { |
1918 | req->r_args.setattr.gid = cpu_to_le32( | |
1919 | from_kgid(&init_user_ns, attr->ia_gid)); | |
355da1eb SW |
1920 | mask |= CEPH_SETATTR_GID; |
1921 | release |= CEPH_CAP_AUTH_SHARED; | |
1922 | } | |
1923 | } | |
1924 | if (ia_valid & ATTR_MODE) { | |
1925 | dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, | |
1926 | attr->ia_mode); | |
1927 | if (issued & CEPH_CAP_AUTH_EXCL) { | |
1928 | inode->i_mode = attr->ia_mode; | |
1929 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
1930 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | |
1931 | attr->ia_mode != inode->i_mode) { | |
7221fe4c | 1932 | inode->i_mode = attr->ia_mode; |
355da1eb SW |
1933 | req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); |
1934 | mask |= CEPH_SETATTR_MODE; | |
1935 | release |= CEPH_CAP_AUTH_SHARED; | |
1936 | } | |
1937 | } | |
1938 | ||
1939 | if (ia_valid & ATTR_ATIME) { | |
1940 | dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode, | |
1941 | inode->i_atime.tv_sec, inode->i_atime.tv_nsec, | |
1942 | attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); | |
1943 | if (issued & CEPH_CAP_FILE_EXCL) { | |
1944 | ci->i_time_warp_seq++; | |
1945 | inode->i_atime = attr->ia_atime; | |
1946 | dirtied |= CEPH_CAP_FILE_EXCL; | |
1947 | } else if ((issued & CEPH_CAP_FILE_WR) && | |
1948 | timespec_compare(&inode->i_atime, | |
1949 | &attr->ia_atime) < 0) { | |
1950 | inode->i_atime = attr->ia_atime; | |
1951 | dirtied |= CEPH_CAP_FILE_WR; | |
1952 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | |
1953 | !timespec_equal(&inode->i_atime, &attr->ia_atime)) { | |
1954 | ceph_encode_timespec(&req->r_args.setattr.atime, | |
1955 | &attr->ia_atime); | |
1956 | mask |= CEPH_SETATTR_ATIME; | |
1957 | release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD | | |
1958 | CEPH_CAP_FILE_WR; | |
1959 | } | |
1960 | } | |
1961 | if (ia_valid & ATTR_MTIME) { | |
1962 | dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode, | |
1963 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, | |
1964 | attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); | |
1965 | if (issued & CEPH_CAP_FILE_EXCL) { | |
1966 | ci->i_time_warp_seq++; | |
1967 | inode->i_mtime = attr->ia_mtime; | |
1968 | dirtied |= CEPH_CAP_FILE_EXCL; | |
1969 | } else if ((issued & CEPH_CAP_FILE_WR) && | |
1970 | timespec_compare(&inode->i_mtime, | |
1971 | &attr->ia_mtime) < 0) { | |
1972 | inode->i_mtime = attr->ia_mtime; | |
1973 | dirtied |= CEPH_CAP_FILE_WR; | |
1974 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | |
1975 | !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) { | |
1976 | ceph_encode_timespec(&req->r_args.setattr.mtime, | |
1977 | &attr->ia_mtime); | |
1978 | mask |= CEPH_SETATTR_MTIME; | |
1979 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD | | |
1980 | CEPH_CAP_FILE_WR; | |
1981 | } | |
1982 | } | |
1983 | if (ia_valid & ATTR_SIZE) { | |
1984 | dout("setattr %p size %lld -> %lld\n", inode, | |
1985 | inode->i_size, attr->ia_size); | |
355da1eb SW |
1986 | if ((issued & CEPH_CAP_FILE_EXCL) && |
1987 | attr->ia_size > inode->i_size) { | |
99c88e69 | 1988 | i_size_write(inode, attr->ia_size); |
355da1eb SW |
1989 | inode->i_blocks = |
1990 | (attr->ia_size + (1 << 9) - 1) >> 9; | |
1991 | inode->i_ctime = attr->ia_ctime; | |
1992 | ci->i_reported_size = attr->ia_size; | |
1993 | dirtied |= CEPH_CAP_FILE_EXCL; | |
1994 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | |
1995 | attr->ia_size != inode->i_size) { | |
1996 | req->r_args.setattr.size = cpu_to_le64(attr->ia_size); | |
1997 | req->r_args.setattr.old_size = | |
1998 | cpu_to_le64(inode->i_size); | |
1999 | mask |= CEPH_SETATTR_SIZE; | |
2000 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD | | |
2001 | CEPH_CAP_FILE_WR; | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | /* these do nothing */ | |
2006 | if (ia_valid & ATTR_CTIME) { | |
2007 | bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| | |
2008 | ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; | |
2009 | dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode, | |
2010 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, | |
2011 | attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, | |
2012 | only ? "ctime only" : "ignored"); | |
2013 | inode->i_ctime = attr->ia_ctime; | |
2014 | if (only) { | |
2015 | /* | |
2016 | * if kernel wants to dirty ctime but nothing else, | |
2017 | * we need to choose a cap to dirty under, or do | |
2018 | * a almost-no-op setattr | |
2019 | */ | |
2020 | if (issued & CEPH_CAP_AUTH_EXCL) | |
2021 | dirtied |= CEPH_CAP_AUTH_EXCL; | |
2022 | else if (issued & CEPH_CAP_FILE_EXCL) | |
2023 | dirtied |= CEPH_CAP_FILE_EXCL; | |
2024 | else if (issued & CEPH_CAP_XATTR_EXCL) | |
2025 | dirtied |= CEPH_CAP_XATTR_EXCL; | |
2026 | else | |
2027 | mask |= CEPH_SETATTR_CTIME; | |
2028 | } | |
2029 | } | |
2030 | if (ia_valid & ATTR_FILE) | |
2031 | dout("setattr %p ATTR_FILE ... hrm!\n", inode); | |
2032 | ||
2033 | if (dirtied) { | |
f66fd9f0 YZ |
2034 | inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied, |
2035 | &prealloc_cf); | |
8bbd4714 | 2036 | inode->i_ctime = current_fs_time(inode->i_sb); |
355da1eb SW |
2037 | } |
2038 | ||
2039 | release &= issued; | |
be655596 | 2040 | spin_unlock(&ci->i_ceph_lock); |
604d1b02 YZ |
2041 | if (lock_snap_rwsem) |
2042 | up_read(&mdsc->snap_rwsem); | |
355da1eb | 2043 | |
fca65b4a SW |
2044 | if (inode_dirty_flags) |
2045 | __mark_inode_dirty(inode, inode_dirty_flags); | |
2046 | ||
7221fe4c | 2047 | if (ia_valid & ATTR_MODE) { |
4db658ea | 2048 | err = posix_acl_chmod(inode, attr->ia_mode); |
7221fe4c GZ |
2049 | if (err) |
2050 | goto out_put; | |
2051 | } | |
2052 | ||
355da1eb | 2053 | if (mask) { |
70b666c3 SW |
2054 | req->r_inode = inode; |
2055 | ihold(inode); | |
355da1eb SW |
2056 | req->r_inode_drop = release; |
2057 | req->r_args.setattr.mask = cpu_to_le32(mask); | |
2058 | req->r_num_caps = 1; | |
752c8bdc | 2059 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
355da1eb SW |
2060 | } |
2061 | dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, | |
2062 | ceph_cap_string(dirtied), mask); | |
2063 | ||
2064 | ceph_mdsc_put_request(req); | |
b0d7c223 YZ |
2065 | if (mask & CEPH_SETATTR_SIZE) |
2066 | __ceph_do_pending_vmtruncate(inode); | |
f66fd9f0 | 2067 | ceph_free_cap_flush(prealloc_cf); |
355da1eb | 2068 | return err; |
7221fe4c | 2069 | out_put: |
355da1eb | 2070 | ceph_mdsc_put_request(req); |
f66fd9f0 | 2071 | ceph_free_cap_flush(prealloc_cf); |
355da1eb SW |
2072 | return err; |
2073 | } | |
2074 | ||
2075 | /* | |
2076 | * Verify that we have a lease on the given mask. If not, | |
2077 | * do a getattr against an mds. | |
2078 | */ | |
01deead0 YZ |
2079 | int __ceph_do_getattr(struct inode *inode, struct page *locked_page, |
2080 | int mask, bool force) | |
355da1eb | 2081 | { |
3d14c5d2 YS |
2082 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
2083 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
355da1eb SW |
2084 | struct ceph_mds_request *req; |
2085 | int err; | |
2086 | ||
2087 | if (ceph_snap(inode) == CEPH_SNAPDIR) { | |
2088 | dout("do_getattr inode %p SNAPDIR\n", inode); | |
2089 | return 0; | |
2090 | } | |
2091 | ||
01deead0 YZ |
2092 | dout("do_getattr inode %p mask %s mode 0%o\n", |
2093 | inode, ceph_cap_string(mask), inode->i_mode); | |
508b32d8 | 2094 | if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1)) |
355da1eb SW |
2095 | return 0; |
2096 | ||
2097 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS); | |
2098 | if (IS_ERR(req)) | |
2099 | return PTR_ERR(req); | |
70b666c3 SW |
2100 | req->r_inode = inode; |
2101 | ihold(inode); | |
355da1eb SW |
2102 | req->r_num_caps = 1; |
2103 | req->r_args.getattr.mask = cpu_to_le32(mask); | |
01deead0 | 2104 | req->r_locked_page = locked_page; |
355da1eb | 2105 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
01deead0 YZ |
2106 | if (locked_page && err == 0) { |
2107 | u64 inline_version = req->r_reply_info.targeti.inline_version; | |
2108 | if (inline_version == 0) { | |
2109 | /* the reply is supposed to contain inline data */ | |
2110 | err = -EINVAL; | |
2111 | } else if (inline_version == CEPH_INLINE_NONE) { | |
2112 | err = -ENODATA; | |
2113 | } else { | |
2114 | err = req->r_reply_info.targeti.inline_len; | |
2115 | } | |
2116 | } | |
355da1eb SW |
2117 | ceph_mdsc_put_request(req); |
2118 | dout("do_getattr result=%d\n", err); | |
2119 | return err; | |
2120 | } | |
2121 | ||
2122 | ||
2123 | /* | |
2124 | * Check inode permissions. We verify we have a valid value for | |
2125 | * the AUTH cap, then call the generic handler. | |
2126 | */ | |
10556cb2 | 2127 | int ceph_permission(struct inode *inode, int mask) |
355da1eb | 2128 | { |
b74c79e9 NP |
2129 | int err; |
2130 | ||
10556cb2 | 2131 | if (mask & MAY_NOT_BLOCK) |
b74c79e9 NP |
2132 | return -ECHILD; |
2133 | ||
508b32d8 | 2134 | err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false); |
355da1eb SW |
2135 | |
2136 | if (!err) | |
2830ba7f | 2137 | err = generic_permission(inode, mask); |
355da1eb SW |
2138 | return err; |
2139 | } | |
2140 | ||
2141 | /* | |
2142 | * Get all attributes. Hopefully somedata we'll have a statlite() | |
2143 | * and can limit the fields we require to be accurate. | |
2144 | */ | |
2145 | int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
2146 | struct kstat *stat) | |
2147 | { | |
2b0143b5 | 2148 | struct inode *inode = d_inode(dentry); |
232d4b01 | 2149 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb SW |
2150 | int err; |
2151 | ||
508b32d8 | 2152 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL, false); |
355da1eb SW |
2153 | if (!err) { |
2154 | generic_fillattr(inode, stat); | |
ad1fee96 | 2155 | stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino); |
355da1eb SW |
2156 | if (ceph_snap(inode) != CEPH_NOSNAP) |
2157 | stat->dev = ceph_snap(inode); | |
2158 | else | |
2159 | stat->dev = 0; | |
232d4b01 | 2160 | if (S_ISDIR(inode->i_mode)) { |
1c1266bb YS |
2161 | if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), |
2162 | RBYTES)) | |
2163 | stat->size = ci->i_rbytes; | |
2164 | else | |
2165 | stat->size = ci->i_files + ci->i_subdirs; | |
232d4b01 | 2166 | stat->blocks = 0; |
355da1eb | 2167 | stat->blksize = 65536; |
232d4b01 | 2168 | } |
355da1eb SW |
2169 | } |
2170 | return err; | |
2171 | } |