fs: rcu-walk aware d_revalidate method
[linux-block.git] / fs / ceph / inode.c
CommitLineData
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>
9#include <linux/namei.h>
10#include <linux/writeback.h>
11#include <linux/vmalloc.h>
c9af9fb6 12#include <linux/pagevec.h>
355da1eb
SW
13
14#include "super.h"
3d14c5d2
YS
15#include "mds_client.h"
16#include <linux/ceph/decode.h>
355da1eb
SW
17
18/*
19 * Ceph inode operations
20 *
21 * Implement basic inode helpers (get, alloc) and inode ops (getattr,
22 * setattr, etc.), xattr helpers, and helpers for assimilating
23 * metadata returned by the MDS into our cache.
24 *
25 * Also define helpers for doing asynchronous writeback, invalidation,
26 * and truncation for the benefit of those who can't afford to block
27 * (typically because they are in the message handler path).
28 */
29
30static const struct inode_operations ceph_symlink_iops;
31
3c6f6b79
SW
32static void ceph_invalidate_work(struct work_struct *work);
33static void ceph_writeback_work(struct work_struct *work);
34static void ceph_vmtruncate_work(struct work_struct *work);
355da1eb
SW
35
36/*
37 * find or create an inode, given the ceph ino number
38 */
39struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
40{
41 struct inode *inode;
42 ino_t t = ceph_vino_to_ino(vino);
43
44 inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
45 if (inode == NULL)
46 return ERR_PTR(-ENOMEM);
47 if (inode->i_state & I_NEW) {
48 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
49 inode, ceph_vinop(inode), (u64)inode->i_ino);
50 unlock_new_inode(inode);
51 }
52
53 dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
54 vino.snap, inode);
55 return inode;
56}
57
58/*
59 * get/constuct snapdir inode for a given directory
60 */
61struct inode *ceph_get_snapdir(struct inode *parent)
62{
63 struct ceph_vino vino = {
64 .ino = ceph_ino(parent),
65 .snap = CEPH_SNAPDIR,
66 };
67 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
b377ff13 68 struct ceph_inode_info *ci = ceph_inode(inode);
355da1eb
SW
69
70 BUG_ON(!S_ISDIR(parent->i_mode));
71 if (IS_ERR(inode))
7e34bc52 72 return inode;
355da1eb
SW
73 inode->i_mode = parent->i_mode;
74 inode->i_uid = parent->i_uid;
75 inode->i_gid = parent->i_gid;
76 inode->i_op = &ceph_dir_iops;
77 inode->i_fop = &ceph_dir_fops;
b377ff13
SW
78 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
79 ci->i_rbytes = 0;
355da1eb
SW
80 return inode;
81}
82
83const struct inode_operations ceph_file_iops = {
84 .permission = ceph_permission,
85 .setattr = ceph_setattr,
86 .getattr = ceph_getattr,
87 .setxattr = ceph_setxattr,
88 .getxattr = ceph_getxattr,
89 .listxattr = ceph_listxattr,
90 .removexattr = ceph_removexattr,
91};
92
93
94/*
95 * We use a 'frag tree' to keep track of the MDS's directory fragments
96 * for a given inode (usually there is just a single fragment). We
97 * need to know when a child frag is delegated to a new MDS, or when
98 * it is flagged as replicated, so we can direct our requests
99 * accordingly.
100 */
101
102/*
103 * find/create a frag in the tree
104 */
105static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
106 u32 f)
107{
108 struct rb_node **p;
109 struct rb_node *parent = NULL;
110 struct ceph_inode_frag *frag;
111 int c;
112
113 p = &ci->i_fragtree.rb_node;
114 while (*p) {
115 parent = *p;
116 frag = rb_entry(parent, struct ceph_inode_frag, node);
117 c = ceph_frag_compare(f, frag->frag);
118 if (c < 0)
119 p = &(*p)->rb_left;
120 else if (c > 0)
121 p = &(*p)->rb_right;
122 else
123 return frag;
124 }
125
126 frag = kmalloc(sizeof(*frag), GFP_NOFS);
127 if (!frag) {
128 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
129 "frag %x\n", &ci->vfs_inode,
130 ceph_vinop(&ci->vfs_inode), f);
131 return ERR_PTR(-ENOMEM);
132 }
133 frag->frag = f;
134 frag->split_by = 0;
135 frag->mds = -1;
136 frag->ndist = 0;
137
138 rb_link_node(&frag->node, parent, p);
139 rb_insert_color(&frag->node, &ci->i_fragtree);
140
141 dout("get_or_create_frag added %llx.%llx frag %x\n",
142 ceph_vinop(&ci->vfs_inode), f);
143 return frag;
144}
145
146/*
147 * find a specific frag @f
148 */
149struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
150{
151 struct rb_node *n = ci->i_fragtree.rb_node;
152
153 while (n) {
154 struct ceph_inode_frag *frag =
155 rb_entry(n, struct ceph_inode_frag, node);
156 int c = ceph_frag_compare(f, frag->frag);
157 if (c < 0)
158 n = n->rb_left;
159 else if (c > 0)
160 n = n->rb_right;
161 else
162 return frag;
163 }
164 return NULL;
165}
166
167/*
168 * Choose frag containing the given value @v. If @pfrag is
169 * specified, copy the frag delegation info to the caller if
170 * it is present.
171 */
172u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
173 struct ceph_inode_frag *pfrag,
174 int *found)
175{
176 u32 t = ceph_frag_make(0, 0);
177 struct ceph_inode_frag *frag;
178 unsigned nway, i;
179 u32 n;
180
181 if (found)
182 *found = 0;
183
184 mutex_lock(&ci->i_fragtree_mutex);
185 while (1) {
186 WARN_ON(!ceph_frag_contains_value(t, v));
187 frag = __ceph_find_frag(ci, t);
188 if (!frag)
189 break; /* t is a leaf */
190 if (frag->split_by == 0) {
191 if (pfrag)
192 memcpy(pfrag, frag, sizeof(*pfrag));
193 if (found)
194 *found = 1;
195 break;
196 }
197
198 /* choose child */
199 nway = 1 << frag->split_by;
200 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
201 frag->split_by, nway);
202 for (i = 0; i < nway; i++) {
203 n = ceph_frag_make_child(t, frag->split_by, i);
204 if (ceph_frag_contains_value(n, v)) {
205 t = n;
206 break;
207 }
208 }
209 BUG_ON(i == nway);
210 }
211 dout("choose_frag(%x) = %x\n", v, t);
212
213 mutex_unlock(&ci->i_fragtree_mutex);
214 return t;
215}
216
217/*
218 * Process dirfrag (delegation) info from the mds. Include leaf
219 * fragment in tree ONLY if ndist > 0. Otherwise, only
220 * branches/splits are included in i_fragtree)
221 */
222static int ceph_fill_dirfrag(struct inode *inode,
223 struct ceph_mds_reply_dirfrag *dirinfo)
224{
225 struct ceph_inode_info *ci = ceph_inode(inode);
226 struct ceph_inode_frag *frag;
227 u32 id = le32_to_cpu(dirinfo->frag);
228 int mds = le32_to_cpu(dirinfo->auth);
229 int ndist = le32_to_cpu(dirinfo->ndist);
230 int i;
231 int err = 0;
232
233 mutex_lock(&ci->i_fragtree_mutex);
234 if (ndist == 0) {
235 /* no delegation info needed. */
236 frag = __ceph_find_frag(ci, id);
237 if (!frag)
238 goto out;
239 if (frag->split_by == 0) {
240 /* tree leaf, remove */
241 dout("fill_dirfrag removed %llx.%llx frag %x"
242 " (no ref)\n", ceph_vinop(inode), id);
243 rb_erase(&frag->node, &ci->i_fragtree);
244 kfree(frag);
245 } else {
246 /* tree branch, keep and clear */
247 dout("fill_dirfrag cleared %llx.%llx frag %x"
248 " referral\n", ceph_vinop(inode), id);
249 frag->mds = -1;
250 frag->ndist = 0;
251 }
252 goto out;
253 }
254
255
256 /* find/add this frag to store mds delegation info */
257 frag = __get_or_create_frag(ci, id);
258 if (IS_ERR(frag)) {
259 /* this is not the end of the world; we can continue
260 with bad/inaccurate delegation info */
261 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
262 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
263 err = -ENOMEM;
264 goto out;
265 }
266
267 frag->mds = mds;
268 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
269 for (i = 0; i < frag->ndist; i++)
270 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
271 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
272 ceph_vinop(inode), frag->frag, frag->ndist);
273
274out:
275 mutex_unlock(&ci->i_fragtree_mutex);
276 return err;
277}
278
279
280/*
281 * initialize a newly allocated inode.
282 */
283struct inode *ceph_alloc_inode(struct super_block *sb)
284{
285 struct ceph_inode_info *ci;
286 int i;
287
288 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
289 if (!ci)
290 return NULL;
291
292 dout("alloc_inode %p\n", &ci->vfs_inode);
293
294 ci->i_version = 0;
295 ci->i_time_warp_seq = 0;
296 ci->i_ceph_flags = 0;
297 ci->i_release_count = 0;
298 ci->i_symlink = NULL;
299
300 ci->i_fragtree = RB_ROOT;
301 mutex_init(&ci->i_fragtree_mutex);
302
303 ci->i_xattrs.blob = NULL;
304 ci->i_xattrs.prealloc_blob = NULL;
305 ci->i_xattrs.dirty = false;
306 ci->i_xattrs.index = RB_ROOT;
307 ci->i_xattrs.count = 0;
308 ci->i_xattrs.names_size = 0;
309 ci->i_xattrs.vals_size = 0;
310 ci->i_xattrs.version = 0;
311 ci->i_xattrs.index_version = 0;
312
313 ci->i_caps = RB_ROOT;
314 ci->i_auth_cap = NULL;
315 ci->i_dirty_caps = 0;
316 ci->i_flushing_caps = 0;
317 INIT_LIST_HEAD(&ci->i_dirty_item);
318 INIT_LIST_HEAD(&ci->i_flushing_item);
319 ci->i_cap_flush_seq = 0;
320 ci->i_cap_flush_last_tid = 0;
321 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
322 init_waitqueue_head(&ci->i_cap_wq);
323 ci->i_hold_caps_min = 0;
324 ci->i_hold_caps_max = 0;
325 INIT_LIST_HEAD(&ci->i_cap_delay_list);
326 ci->i_cap_exporting_mds = 0;
327 ci->i_cap_exporting_mseq = 0;
328 ci->i_cap_exporting_issued = 0;
329 INIT_LIST_HEAD(&ci->i_cap_snaps);
330 ci->i_head_snapc = NULL;
331 ci->i_snap_caps = 0;
332
333 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
334 ci->i_nr_by_mode[i] = 0;
335
336 ci->i_truncate_seq = 0;
337 ci->i_truncate_size = 0;
338 ci->i_truncate_pending = 0;
339
340 ci->i_max_size = 0;
341 ci->i_reported_size = 0;
342 ci->i_wanted_max_size = 0;
343 ci->i_requested_max_size = 0;
344
345 ci->i_pin_ref = 0;
346 ci->i_rd_ref = 0;
347 ci->i_rdcache_ref = 0;
348 ci->i_wr_ref = 0;
349 ci->i_wrbuffer_ref = 0;
350 ci->i_wrbuffer_ref_head = 0;
351 ci->i_shared_gen = 0;
352 ci->i_rdcache_gen = 0;
353 ci->i_rdcache_revoking = 0;
354
355 INIT_LIST_HEAD(&ci->i_unsafe_writes);
356 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
357 spin_lock_init(&ci->i_unsafe_lock);
358
359 ci->i_snap_realm = NULL;
360 INIT_LIST_HEAD(&ci->i_snap_realm_item);
361 INIT_LIST_HEAD(&ci->i_snap_flush_item);
362
3c6f6b79
SW
363 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
364 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
355da1eb
SW
365
366 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
367
368 return &ci->vfs_inode;
369}
370
fa0d7e3d
NP
371static void ceph_i_callback(struct rcu_head *head)
372{
373 struct inode *inode = container_of(head, struct inode, i_rcu);
374 struct ceph_inode_info *ci = ceph_inode(inode);
375
376 INIT_LIST_HEAD(&inode->i_dentry);
377 kmem_cache_free(ceph_inode_cachep, ci);
378}
379
355da1eb
SW
380void ceph_destroy_inode(struct inode *inode)
381{
382 struct ceph_inode_info *ci = ceph_inode(inode);
383 struct ceph_inode_frag *frag;
384 struct rb_node *n;
385
386 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
387
388 ceph_queue_caps_release(inode);
389
8b218b8a
SW
390 /*
391 * we may still have a snap_realm reference if there are stray
392 * caps in i_cap_exporting_issued or i_snap_caps.
393 */
394 if (ci->i_snap_realm) {
395 struct ceph_mds_client *mdsc =
3d14c5d2 396 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
8b218b8a
SW
397 struct ceph_snap_realm *realm = ci->i_snap_realm;
398
399 dout(" dropping residual ref to snap realm %p\n", realm);
400 spin_lock(&realm->inodes_with_caps_lock);
401 list_del_init(&ci->i_snap_realm_item);
402 spin_unlock(&realm->inodes_with_caps_lock);
403 ceph_put_snap_realm(mdsc, realm);
404 }
405
355da1eb
SW
406 kfree(ci->i_symlink);
407 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
408 frag = rb_entry(n, struct ceph_inode_frag, node);
409 rb_erase(n, &ci->i_fragtree);
410 kfree(frag);
411 }
412
413 __ceph_destroy_xattrs(ci);
b6c1d5b8
SW
414 if (ci->i_xattrs.blob)
415 ceph_buffer_put(ci->i_xattrs.blob);
416 if (ci->i_xattrs.prealloc_blob)
417 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
355da1eb 418
fa0d7e3d 419 call_rcu(&inode->i_rcu, ceph_i_callback);
355da1eb
SW
420}
421
422
423/*
424 * Helpers to fill in size, ctime, mtime, and atime. We have to be
425 * careful because either the client or MDS may have more up to date
426 * info, depending on which capabilities are held, and whether
427 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
428 * and size are monotonically increasing, except when utimes() or
429 * truncate() increments the corresponding _seq values.)
430 */
431int ceph_fill_file_size(struct inode *inode, int issued,
432 u32 truncate_seq, u64 truncate_size, u64 size)
433{
434 struct ceph_inode_info *ci = ceph_inode(inode);
435 int queue_trunc = 0;
436
437 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
438 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
439 dout("size %lld -> %llu\n", inode->i_size, size);
440 inode->i_size = size;
441 inode->i_blocks = (size + (1<<9) - 1) >> 9;
442 ci->i_reported_size = size;
443 if (truncate_seq != ci->i_truncate_seq) {
444 dout("truncate_seq %u -> %u\n",
445 ci->i_truncate_seq, truncate_seq);
446 ci->i_truncate_seq = truncate_seq;
3d497d85
YS
447 /*
448 * If we hold relevant caps, or in the case where we're
449 * not the only client referencing this file and we
450 * don't hold those caps, then we need to check whether
451 * the file is either opened or mmaped
452 */
453 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
2962507c
SW
454 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
455 CEPH_CAP_FILE_EXCL|
456 CEPH_CAP_FILE_LAZYIO)) ||
3d497d85
YS
457 mapping_mapped(inode->i_mapping) ||
458 __ceph_caps_file_wanted(ci)) {
355da1eb
SW
459 ci->i_truncate_pending++;
460 queue_trunc = 1;
461 }
462 }
463 }
464 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
465 ci->i_truncate_size != truncate_size) {
466 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
467 truncate_size);
468 ci->i_truncate_size = truncate_size;
469 }
470 return queue_trunc;
471}
472
473void ceph_fill_file_time(struct inode *inode, int issued,
474 u64 time_warp_seq, struct timespec *ctime,
475 struct timespec *mtime, struct timespec *atime)
476{
477 struct ceph_inode_info *ci = ceph_inode(inode);
478 int warn = 0;
479
480 if (issued & (CEPH_CAP_FILE_EXCL|
481 CEPH_CAP_FILE_WR|
d8672d64
SW
482 CEPH_CAP_FILE_BUFFER|
483 CEPH_CAP_AUTH_EXCL|
484 CEPH_CAP_XATTR_EXCL)) {
355da1eb
SW
485 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
486 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
487 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
488 ctime->tv_sec, ctime->tv_nsec);
489 inode->i_ctime = *ctime;
490 }
491 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
492 /* the MDS did a utimes() */
493 dout("mtime %ld.%09ld -> %ld.%09ld "
494 "tw %d -> %d\n",
495 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
496 mtime->tv_sec, mtime->tv_nsec,
497 ci->i_time_warp_seq, (int)time_warp_seq);
498
499 inode->i_mtime = *mtime;
500 inode->i_atime = *atime;
501 ci->i_time_warp_seq = time_warp_seq;
502 } else if (time_warp_seq == ci->i_time_warp_seq) {
503 /* nobody did utimes(); take the max */
504 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
505 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
506 inode->i_mtime.tv_sec,
507 inode->i_mtime.tv_nsec,
508 mtime->tv_sec, mtime->tv_nsec);
509 inode->i_mtime = *mtime;
510 }
511 if (timespec_compare(atime, &inode->i_atime) > 0) {
512 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
513 inode->i_atime.tv_sec,
514 inode->i_atime.tv_nsec,
515 atime->tv_sec, atime->tv_nsec);
516 inode->i_atime = *atime;
517 }
518 } else if (issued & CEPH_CAP_FILE_EXCL) {
519 /* we did a utimes(); ignore mds values */
520 } else {
521 warn = 1;
522 }
523 } else {
d8672d64 524 /* we have no write|excl caps; whatever the MDS says is true */
355da1eb
SW
525 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
526 inode->i_ctime = *ctime;
527 inode->i_mtime = *mtime;
528 inode->i_atime = *atime;
529 ci->i_time_warp_seq = time_warp_seq;
530 } else {
531 warn = 1;
532 }
533 }
534 if (warn) /* time_warp_seq shouldn't go backwards */
535 dout("%p mds time_warp_seq %llu < %u\n",
536 inode, time_warp_seq, ci->i_time_warp_seq);
537}
538
539/*
540 * Populate an inode based on info from mds. May be called on new or
541 * existing inodes.
542 */
543static int fill_inode(struct inode *inode,
544 struct ceph_mds_reply_info_in *iinfo,
545 struct ceph_mds_reply_dirfrag *dirinfo,
546 struct ceph_mds_session *session,
547 unsigned long ttl_from, int cap_fmode,
548 struct ceph_cap_reservation *caps_reservation)
549{
550 struct ceph_mds_reply_inode *info = iinfo->in;
551 struct ceph_inode_info *ci = ceph_inode(inode);
552 int i;
553 int issued, implemented;
554 struct timespec mtime, atime, ctime;
555 u32 nsplits;
556 struct ceph_buffer *xattr_blob = NULL;
557 int err = 0;
558 int queue_trunc = 0;
559
560 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
561 inode, ceph_vinop(inode), le64_to_cpu(info->version),
562 ci->i_version);
563
564 /*
565 * prealloc xattr data, if it looks like we'll need it. only
566 * if len > 4 (meaning there are actually xattrs; the first 4
567 * bytes are the xattr count).
568 */
569 if (iinfo->xattr_len > 4) {
b6c1d5b8 570 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
355da1eb
SW
571 if (!xattr_blob)
572 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
573 iinfo->xattr_len);
574 }
575
576 spin_lock(&inode->i_lock);
577
578 /*
579 * provided version will be odd if inode value is projected,
8bd59e01
SW
580 * even if stable. skip the update if we have newer stable
581 * info (ours>=theirs, e.g. due to racing mds replies), unless
582 * we are getting projected (unstable) info (in which case the
583 * version is odd, and we want ours>theirs).
584 * us them
585 * 2 2 skip
586 * 3 2 skip
587 * 3 3 update
355da1eb
SW
588 */
589 if (le64_to_cpu(info->version) > 0 &&
8bd59e01 590 (ci->i_version & ~1) >= le64_to_cpu(info->version))
355da1eb
SW
591 goto no_change;
592
593 issued = __ceph_caps_issued(ci, &implemented);
594 issued |= implemented | __ceph_caps_dirty(ci);
595
596 /* update inode */
597 ci->i_version = le64_to_cpu(info->version);
598 inode->i_version++;
599 inode->i_rdev = le32_to_cpu(info->rdev);
600
601 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
602 inode->i_mode = le32_to_cpu(info->mode);
603 inode->i_uid = le32_to_cpu(info->uid);
604 inode->i_gid = le32_to_cpu(info->gid);
605 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
606 inode->i_uid, inode->i_gid);
607 }
608
609 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
610 inode->i_nlink = le32_to_cpu(info->nlink);
611
612 /* be careful with mtime, atime, size */
613 ceph_decode_timespec(&atime, &info->atime);
614 ceph_decode_timespec(&mtime, &info->mtime);
615 ceph_decode_timespec(&ctime, &info->ctime);
616 queue_trunc = ceph_fill_file_size(inode, issued,
617 le32_to_cpu(info->truncate_seq),
618 le64_to_cpu(info->truncate_size),
355da1eb
SW
619 le64_to_cpu(info->size));
620 ceph_fill_file_time(inode, issued,
621 le32_to_cpu(info->time_warp_seq),
622 &ctime, &mtime, &atime);
623
912a9b03
SW
624 /* only update max_size on auth cap */
625 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
626 ci->i_max_size != le64_to_cpu(info->max_size)) {
627 dout("max_size %lld -> %llu\n", ci->i_max_size,
628 le64_to_cpu(info->max_size));
629 ci->i_max_size = le64_to_cpu(info->max_size);
630 }
631
355da1eb
SW
632 ci->i_layout = info->layout;
633 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
634
635 /* xattrs */
636 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
637 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
638 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
639 if (ci->i_xattrs.blob)
640 ceph_buffer_put(ci->i_xattrs.blob);
641 ci->i_xattrs.blob = xattr_blob;
642 if (xattr_blob)
643 memcpy(ci->i_xattrs.blob->vec.iov_base,
644 iinfo->xattr_data, iinfo->xattr_len);
645 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
a6424e48 646 xattr_blob = NULL;
355da1eb
SW
647 }
648
649 inode->i_mapping->a_ops = &ceph_aops;
650 inode->i_mapping->backing_dev_info =
640ef79d 651 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
355da1eb
SW
652
653 switch (inode->i_mode & S_IFMT) {
654 case S_IFIFO:
655 case S_IFBLK:
656 case S_IFCHR:
657 case S_IFSOCK:
658 init_special_inode(inode, inode->i_mode, inode->i_rdev);
659 inode->i_op = &ceph_file_iops;
660 break;
661 case S_IFREG:
662 inode->i_op = &ceph_file_iops;
663 inode->i_fop = &ceph_file_fops;
664 break;
665 case S_IFLNK:
666 inode->i_op = &ceph_symlink_iops;
667 if (!ci->i_symlink) {
668 int symlen = iinfo->symlink_len;
669 char *sym;
670
671 BUG_ON(symlen != inode->i_size);
672 spin_unlock(&inode->i_lock);
673
674 err = -ENOMEM;
675 sym = kmalloc(symlen+1, GFP_NOFS);
676 if (!sym)
677 goto out;
678 memcpy(sym, iinfo->symlink, symlen);
679 sym[symlen] = 0;
680
681 spin_lock(&inode->i_lock);
682 if (!ci->i_symlink)
683 ci->i_symlink = sym;
684 else
685 kfree(sym); /* lost a race */
686 }
687 break;
688 case S_IFDIR:
689 inode->i_op = &ceph_dir_iops;
690 inode->i_fop = &ceph_dir_fops;
691
692 ci->i_files = le64_to_cpu(info->files);
693 ci->i_subdirs = le64_to_cpu(info->subdirs);
694 ci->i_rbytes = le64_to_cpu(info->rbytes);
695 ci->i_rfiles = le64_to_cpu(info->rfiles);
696 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
697 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
698
699 /* set dir completion flag? */
700 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
701 ceph_snap(inode) == CEPH_NOSNAP &&
1b7facc4 702 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
12451491 703 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
1b7facc4 704 (ci->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
355da1eb
SW
705 dout(" marking %p complete (empty)\n", inode);
706 ci->i_ceph_flags |= CEPH_I_COMPLETE;
707 ci->i_max_offset = 2;
708 }
709
710 /* it may be better to set st_size in getattr instead? */
3d14c5d2 711 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), RBYTES))
355da1eb
SW
712 inode->i_size = ci->i_rbytes;
713 break;
714 default:
715 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
716 ceph_vinop(inode), inode->i_mode);
717 }
718
719no_change:
720 spin_unlock(&inode->i_lock);
721
722 /* queue truncate if we saw i_size decrease */
723 if (queue_trunc)
3c6f6b79 724 ceph_queue_vmtruncate(inode);
355da1eb
SW
725
726 /* populate frag tree */
727 /* FIXME: move me up, if/when version reflects fragtree changes */
728 nsplits = le32_to_cpu(info->fragtree.nsplits);
729 mutex_lock(&ci->i_fragtree_mutex);
730 for (i = 0; i < nsplits; i++) {
731 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
732 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
733
734 if (IS_ERR(frag))
735 continue;
736 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
737 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
738 }
739 mutex_unlock(&ci->i_fragtree_mutex);
740
741 /* were we issued a capability? */
742 if (info->cap.caps) {
743 if (ceph_snap(inode) == CEPH_NOSNAP) {
744 ceph_add_cap(inode, session,
745 le64_to_cpu(info->cap.cap_id),
746 cap_fmode,
747 le32_to_cpu(info->cap.caps),
748 le32_to_cpu(info->cap.wanted),
749 le32_to_cpu(info->cap.seq),
750 le32_to_cpu(info->cap.mseq),
751 le64_to_cpu(info->cap.realm),
752 info->cap.flags,
753 caps_reservation);
754 } else {
755 spin_lock(&inode->i_lock);
756 dout(" %p got snap_caps %s\n", inode,
757 ceph_cap_string(le32_to_cpu(info->cap.caps)));
758 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
759 if (cap_fmode >= 0)
760 __ceph_get_fmode(ci, cap_fmode);
761 spin_unlock(&inode->i_lock);
762 }
04d000eb
SW
763 } else if (cap_fmode >= 0) {
764 pr_warning("mds issued no caps on %llx.%llx\n",
765 ceph_vinop(inode));
766 __ceph_get_fmode(ci, cap_fmode);
355da1eb
SW
767 }
768
769 /* update delegation info? */
770 if (dirinfo)
771 ceph_fill_dirfrag(inode, dirinfo);
772
773 err = 0;
774
775out:
b6c1d5b8
SW
776 if (xattr_blob)
777 ceph_buffer_put(xattr_blob);
355da1eb
SW
778 return err;
779}
780
781/*
782 * caller should hold session s_mutex.
783 */
784static void update_dentry_lease(struct dentry *dentry,
785 struct ceph_mds_reply_lease *lease,
786 struct ceph_mds_session *session,
787 unsigned long from_time)
788{
789 struct ceph_dentry_info *di = ceph_dentry(dentry);
790 long unsigned duration = le32_to_cpu(lease->duration_ms);
791 long unsigned ttl = from_time + (duration * HZ) / 1000;
792 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
793 struct inode *dir;
794
795 /* only track leases on regular dentries */
796 if (dentry->d_op != &ceph_dentry_ops)
797 return;
798
799 spin_lock(&dentry->d_lock);
800 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
801 dentry, le16_to_cpu(lease->mask), duration, ttl);
802
803 /* make lease_rdcache_gen match directory */
804 dir = dentry->d_parent->d_inode;
805 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
806
807 if (lease->mask == 0)
808 goto out_unlock;
809
810 if (di->lease_gen == session->s_cap_gen &&
811 time_before(ttl, dentry->d_time))
812 goto out_unlock; /* we already have a newer lease. */
813
814 if (di->lease_session && di->lease_session != session)
815 goto out_unlock;
816
817 ceph_dentry_lru_touch(dentry);
818
819 if (!di->lease_session)
820 di->lease_session = ceph_get_mds_session(session);
821 di->lease_gen = session->s_cap_gen;
822 di->lease_seq = le32_to_cpu(lease->seq);
823 di->lease_renew_after = half_ttl;
824 di->lease_renew_from = 0;
825 dentry->d_time = ttl;
826out_unlock:
827 spin_unlock(&dentry->d_lock);
828 return;
829}
830
1cd3935b
SW
831/*
832 * Set dentry's directory position based on the current dir's max, and
833 * order it in d_subdirs, so that dcache_readdir behaves.
834 */
835static void ceph_set_dentry_offset(struct dentry *dn)
836{
837 struct dentry *dir = dn->d_parent;
838 struct inode *inode = dn->d_parent->d_inode;
839 struct ceph_dentry_info *di;
840
841 BUG_ON(!inode);
842
843 di = ceph_dentry(dn);
844
845 spin_lock(&inode->i_lock);
846 if ((ceph_inode(inode)->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
847 spin_unlock(&inode->i_lock);
848 return;
849 }
850 di->offset = ceph_inode(inode)->i_max_offset++;
851 spin_unlock(&inode->i_lock);
852
2fd6b7f5
NP
853 spin_lock(&dir->d_lock);
854 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
13a4214c 855 list_move(&dn->d_u.d_child, &dir->d_subdirs);
1cd3935b
SW
856 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
857 dn->d_u.d_child.prev, dn->d_u.d_child.next);
858 spin_unlock(&dn->d_lock);
2fd6b7f5 859 spin_unlock(&dir->d_lock);
1cd3935b
SW
860}
861
355da1eb
SW
862/*
863 * splice a dentry to an inode.
864 * caller must hold directory i_mutex for this to be safe.
865 *
866 * we will only rehash the resulting dentry if @prehash is
867 * true; @prehash will be set to false (for the benefit of
868 * the caller) if we fail.
869 */
870static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
467c5251 871 bool *prehash, bool set_offset)
355da1eb
SW
872{
873 struct dentry *realdn;
874
1cd3935b
SW
875 BUG_ON(dn->d_inode);
876
355da1eb
SW
877 /* dn must be unhashed */
878 if (!d_unhashed(dn))
879 d_drop(dn);
880 realdn = d_materialise_unique(dn, in);
881 if (IS_ERR(realdn)) {
d69ed05a
SW
882 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
883 PTR_ERR(realdn), dn, in, ceph_vinop(in));
355da1eb
SW
884 if (prehash)
885 *prehash = false; /* don't rehash on error */
886 dn = realdn; /* note realdn contains the error */
887 goto out;
888 } else if (realdn) {
889 dout("dn %p (%d) spliced with %p (%d) "
890 "inode %p ino %llx.%llx\n",
b7ab39f6
NP
891 dn, dn->d_count,
892 realdn, realdn->d_count,
355da1eb
SW
893 realdn->d_inode, ceph_vinop(realdn->d_inode));
894 dput(dn);
895 dn = realdn;
896 } else {
897 BUG_ON(!ceph_dentry(dn));
355da1eb
SW
898 dout("dn %p attached to %p ino %llx.%llx\n",
899 dn, dn->d_inode, ceph_vinop(dn->d_inode));
900 }
901 if ((!prehash || *prehash) && d_unhashed(dn))
902 d_rehash(dn);
467c5251
SW
903 if (set_offset)
904 ceph_set_dentry_offset(dn);
355da1eb
SW
905out:
906 return dn;
907}
908
909/*
910 * Incorporate results into the local cache. This is either just
911 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
912 * after a lookup).
913 *
914 * A reply may contain
915 * a directory inode along with a dentry.
916 * and/or a target inode
917 *
918 * Called with snap_rwsem (read).
919 */
920int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
921 struct ceph_mds_session *session)
922{
923 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
924 struct inode *in = NULL;
925 struct ceph_mds_reply_inode *ininfo;
926 struct ceph_vino vino;
3d14c5d2 927 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
355da1eb
SW
928 int i = 0;
929 int err = 0;
930
931 dout("fill_trace %p is_dentry %d is_target %d\n", req,
932 rinfo->head->is_dentry, rinfo->head->is_target);
933
934#if 0
935 /*
936 * Debugging hook:
937 *
938 * If we resend completed ops to a recovering mds, we get no
939 * trace. Since that is very rare, pretend this is the case
940 * to ensure the 'no trace' handlers in the callers behave.
941 *
942 * Fill in inodes unconditionally to avoid breaking cap
943 * invariants.
944 */
945 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
946 pr_info("fill_trace faking empty trace on %lld %s\n",
947 req->r_tid, ceph_mds_op_name(rinfo->head->op));
948 if (rinfo->head->is_dentry) {
949 rinfo->head->is_dentry = 0;
950 err = fill_inode(req->r_locked_dir,
951 &rinfo->diri, rinfo->dirfrag,
952 session, req->r_request_started, -1);
953 }
954 if (rinfo->head->is_target) {
955 rinfo->head->is_target = 0;
956 ininfo = rinfo->targeti.in;
957 vino.ino = le64_to_cpu(ininfo->ino);
958 vino.snap = le64_to_cpu(ininfo->snapid);
959 in = ceph_get_inode(sb, vino);
960 err = fill_inode(in, &rinfo->targeti, NULL,
961 session, req->r_request_started,
962 req->r_fmode);
963 iput(in);
964 }
965 }
966#endif
967
968 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
969 dout("fill_trace reply is empty!\n");
167c9e35
SW
970 if (rinfo->head->result == 0 && req->r_locked_dir)
971 ceph_invalidate_dir_request(req);
355da1eb
SW
972 return 0;
973 }
974
975 if (rinfo->head->is_dentry) {
5b1daecd
SW
976 struct inode *dir = req->r_locked_dir;
977
978 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
979 session, req->r_request_started, -1,
980 &req->r_caps_reservation);
981 if (err < 0)
982 return err;
983 }
984
9358c6d4
SW
985 /*
986 * ignore null lease/binding on snapdir ENOENT, or else we
987 * will have trouble splicing in the virtual snapdir later
988 */
989 if (rinfo->head->is_dentry && !req->r_aborted &&
990 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
3d14c5d2 991 fsc->mount_options->snapdir_name,
9358c6d4 992 req->r_dentry->d_name.len))) {
355da1eb
SW
993 /*
994 * lookup link rename : null -> possibly existing inode
995 * mknod symlink mkdir : null -> new inode
996 * unlink : linked -> null
997 */
998 struct inode *dir = req->r_locked_dir;
999 struct dentry *dn = req->r_dentry;
1000 bool have_dir_cap, have_lease;
1001
1002 BUG_ON(!dn);
1003 BUG_ON(!dir);
1004 BUG_ON(dn->d_parent->d_inode != dir);
1005 BUG_ON(ceph_ino(dir) !=
1006 le64_to_cpu(rinfo->diri.in->ino));
1007 BUG_ON(ceph_snap(dir) !=
1008 le64_to_cpu(rinfo->diri.in->snapid));
1009
355da1eb
SW
1010 /* do we have a lease on the whole dir? */
1011 have_dir_cap =
1012 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1013 CEPH_CAP_FILE_SHARED);
1014
1015 /* do we have a dn lease? */
1016 have_lease = have_dir_cap ||
1017 (le16_to_cpu(rinfo->dlease->mask) &
1018 CEPH_LOCK_DN);
1019
1020 if (!have_lease)
1021 dout("fill_trace no dentry lease or dir cap\n");
1022
1023 /* rename? */
1024 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1025 dout(" src %p '%.*s' dst %p '%.*s'\n",
1026 req->r_old_dentry,
1027 req->r_old_dentry->d_name.len,
1028 req->r_old_dentry->d_name.name,
1029 dn, dn->d_name.len, dn->d_name.name);
1030 dout("fill_trace doing d_move %p -> %p\n",
1031 req->r_old_dentry, dn);
c10f5e12
SW
1032
1033 /* d_move screws up d_subdirs order */
1034 ceph_i_clear(dir, CEPH_I_COMPLETE);
1035
355da1eb
SW
1036 d_move(req->r_old_dentry, dn);
1037 dout(" src %p '%.*s' dst %p '%.*s'\n",
1038 req->r_old_dentry,
1039 req->r_old_dentry->d_name.len,
1040 req->r_old_dentry->d_name.name,
1041 dn, dn->d_name.len, dn->d_name.name);
81a6cf2d 1042
c4a29f26
SW
1043 /* ensure target dentry is invalidated, despite
1044 rehashing bug in vfs_rename_dir */
81a6cf2d
SW
1045 ceph_invalidate_dentry_lease(dn);
1046
355da1eb 1047 /* take overwritten dentry's readdir offset */
1cd3935b
SW
1048 dout("dn %p gets %p offset %lld (old offset %lld)\n",
1049 req->r_old_dentry, dn, ceph_dentry(dn)->offset,
1050 ceph_dentry(req->r_old_dentry)->offset);
355da1eb
SW
1051 ceph_dentry(req->r_old_dentry)->offset =
1052 ceph_dentry(dn)->offset;
81a6cf2d 1053
355da1eb
SW
1054 dn = req->r_old_dentry; /* use old_dentry */
1055 in = dn->d_inode;
1056 }
1057
1058 /* null dentry? */
1059 if (!rinfo->head->is_target) {
1060 dout("fill_trace null dentry\n");
1061 if (dn->d_inode) {
1062 dout("d_delete %p\n", dn);
1063 d_delete(dn);
1064 } else {
1065 dout("d_instantiate %p NULL\n", dn);
1066 d_instantiate(dn, NULL);
1067 if (have_lease && d_unhashed(dn))
1068 d_rehash(dn);
1069 update_dentry_lease(dn, rinfo->dlease,
1070 session,
1071 req->r_request_started);
1072 }
1073 goto done;
1074 }
1075
1076 /* attach proper inode */
1077 ininfo = rinfo->targeti.in;
1078 vino.ino = le64_to_cpu(ininfo->ino);
1079 vino.snap = le64_to_cpu(ininfo->snapid);
d8b16b3d
SW
1080 in = dn->d_inode;
1081 if (!in) {
355da1eb
SW
1082 in = ceph_get_inode(sb, vino);
1083 if (IS_ERR(in)) {
1084 pr_err("fill_trace bad get_inode "
1085 "%llx.%llx\n", vino.ino, vino.snap);
1086 err = PTR_ERR(in);
1087 d_delete(dn);
1088 goto done;
1089 }
467c5251 1090 dn = splice_dentry(dn, in, &have_lease, true);
355da1eb
SW
1091 if (IS_ERR(dn)) {
1092 err = PTR_ERR(dn);
1093 goto done;
1094 }
1095 req->r_dentry = dn; /* may have spliced */
1096 igrab(in);
1097 } else if (ceph_ino(in) == vino.ino &&
1098 ceph_snap(in) == vino.snap) {
1099 igrab(in);
1100 } else {
1101 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1102 dn, in, ceph_ino(in), ceph_snap(in),
1103 vino.ino, vino.snap);
1104 have_lease = false;
1105 in = NULL;
1106 }
1107
1108 if (have_lease)
1109 update_dentry_lease(dn, rinfo->dlease, session,
1110 req->r_request_started);
1111 dout(" final dn %p\n", dn);
1112 i++;
1113 } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1114 req->r_op == CEPH_MDS_OP_MKSNAP) {
1115 struct dentry *dn = req->r_dentry;
1116
1117 /* fill out a snapdir LOOKUPSNAP dentry */
1118 BUG_ON(!dn);
1119 BUG_ON(!req->r_locked_dir);
1120 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1121 ininfo = rinfo->targeti.in;
1122 vino.ino = le64_to_cpu(ininfo->ino);
1123 vino.snap = le64_to_cpu(ininfo->snapid);
1124 in = ceph_get_inode(sb, vino);
1125 if (IS_ERR(in)) {
1126 pr_err("fill_inode get_inode badness %llx.%llx\n",
1127 vino.ino, vino.snap);
1128 err = PTR_ERR(in);
1129 d_delete(dn);
1130 goto done;
1131 }
1132 dout(" linking snapped dir %p to dn %p\n", in, dn);
467c5251 1133 dn = splice_dentry(dn, in, NULL, true);
355da1eb
SW
1134 if (IS_ERR(dn)) {
1135 err = PTR_ERR(dn);
1136 goto done;
1137 }
1138 req->r_dentry = dn; /* may have spliced */
1139 igrab(in);
1140 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1141 }
1142
1143 if (rinfo->head->is_target) {
1144 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1145 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1146
1147 if (in == NULL || ceph_ino(in) != vino.ino ||
1148 ceph_snap(in) != vino.snap) {
1149 in = ceph_get_inode(sb, vino);
1150 if (IS_ERR(in)) {
1151 err = PTR_ERR(in);
1152 goto done;
1153 }
1154 }
1155 req->r_target_inode = in;
1156
1157 err = fill_inode(in,
1158 &rinfo->targeti, NULL,
1159 session, req->r_request_started,
1160 (le32_to_cpu(rinfo->head->result) == 0) ?
1161 req->r_fmode : -1,
1162 &req->r_caps_reservation);
1163 if (err < 0) {
1164 pr_err("fill_inode badness %p %llx.%llx\n",
1165 in, ceph_vinop(in));
1166 goto done;
1167 }
1168 }
1169
1170done:
1171 dout("fill_trace done err=%d\n", err);
1172 return err;
1173}
1174
1175/*
1176 * Prepopulate our cache with readdir results, leases, etc.
1177 */
1178int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1179 struct ceph_mds_session *session)
1180{
1181 struct dentry *parent = req->r_dentry;
1182 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1183 struct qstr dname;
1184 struct dentry *dn;
1185 struct inode *in;
1186 int err = 0, i;
1187 struct inode *snapdir = NULL;
1188 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1189 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1190 struct ceph_dentry_info *di;
1191
1192 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1193 snapdir = ceph_get_snapdir(parent->d_inode);
1194 parent = d_find_alias(snapdir);
1195 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1196 rinfo->dir_nr, parent);
1197 } else {
1198 dout("readdir_prepopulate %d items under dn %p\n",
1199 rinfo->dir_nr, parent);
1200 if (rinfo->dir_dir)
1201 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1202 }
1203
1204 for (i = 0; i < rinfo->dir_nr; i++) {
1205 struct ceph_vino vino;
1206
1207 dname.name = rinfo->dir_dname[i];
1208 dname.len = rinfo->dir_dname_len[i];
1209 dname.hash = full_name_hash(dname.name, dname.len);
1210
1211 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1212 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1213
1214retry_lookup:
1215 dn = d_lookup(parent, &dname);
1216 dout("d_lookup on parent=%p name=%.*s got %p\n",
1217 parent, dname.len, dname.name, dn);
1218
1219 if (!dn) {
1220 dn = d_alloc(parent, &dname);
1221 dout("d_alloc %p '%.*s' = %p\n", parent,
1222 dname.len, dname.name, dn);
1223 if (dn == NULL) {
1224 dout("d_alloc badness\n");
1225 err = -ENOMEM;
1226 goto out;
1227 }
1228 err = ceph_init_dentry(dn);
8c696737
SW
1229 if (err < 0) {
1230 dput(dn);
355da1eb 1231 goto out;
8c696737 1232 }
355da1eb
SW
1233 } else if (dn->d_inode &&
1234 (ceph_ino(dn->d_inode) != vino.ino ||
1235 ceph_snap(dn->d_inode) != vino.snap)) {
1236 dout(" dn %p points to wrong inode %p\n",
1237 dn, dn->d_inode);
1238 d_delete(dn);
1239 dput(dn);
1240 goto retry_lookup;
1241 } else {
1242 /* reorder parent's d_subdirs */
2fd6b7f5
NP
1243 spin_lock(&parent->d_lock);
1244 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
355da1eb
SW
1245 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1246 spin_unlock(&dn->d_lock);
2fd6b7f5 1247 spin_unlock(&parent->d_lock);
355da1eb
SW
1248 }
1249
1250 di = dn->d_fsdata;
1251 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1252
1253 /* inode */
1254 if (dn->d_inode) {
1255 in = dn->d_inode;
1256 } else {
1257 in = ceph_get_inode(parent->d_sb, vino);
ac1f12ef 1258 if (IS_ERR(in)) {
355da1eb
SW
1259 dout("new_inode badness\n");
1260 d_delete(dn);
1261 dput(dn);
ac1f12ef 1262 err = PTR_ERR(in);
355da1eb
SW
1263 goto out;
1264 }
467c5251 1265 dn = splice_dentry(dn, in, NULL, false);
d69ed05a
SW
1266 if (IS_ERR(dn))
1267 dn = NULL;
355da1eb
SW
1268 }
1269
1270 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1271 req->r_request_started, -1,
1272 &req->r_caps_reservation) < 0) {
1273 pr_err("fill_inode badness on %p\n", in);
d69ed05a 1274 goto next_item;
355da1eb 1275 }
d69ed05a
SW
1276 if (dn)
1277 update_dentry_lease(dn, rinfo->dir_dlease[i],
1278 req->r_session,
1279 req->r_request_started);
1280next_item:
1281 if (dn)
1282 dput(dn);
355da1eb
SW
1283 }
1284 req->r_did_prepopulate = true;
1285
1286out:
1287 if (snapdir) {
1288 iput(snapdir);
1289 dput(parent);
1290 }
1291 dout("readdir_prepopulate done\n");
1292 return err;
1293}
1294
1295int ceph_inode_set_size(struct inode *inode, loff_t size)
1296{
1297 struct ceph_inode_info *ci = ceph_inode(inode);
1298 int ret = 0;
1299
1300 spin_lock(&inode->i_lock);
1301 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1302 inode->i_size = size;
1303 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1304
1305 /* tell the MDS if we are approaching max_size */
1306 if ((size << 1) >= ci->i_max_size &&
1307 (ci->i_reported_size << 1) < ci->i_max_size)
1308 ret = 1;
1309
1310 spin_unlock(&inode->i_lock);
1311 return ret;
1312}
1313
1314/*
1315 * Write back inode data in a worker thread. (This can't be done
1316 * in the message handler context.)
1317 */
3c6f6b79
SW
1318void ceph_queue_writeback(struct inode *inode)
1319{
1320 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1321 &ceph_inode(inode)->i_wb_work)) {
2c27c9a5 1322 dout("ceph_queue_writeback %p\n", inode);
3c6f6b79
SW
1323 igrab(inode);
1324 } else {
2c27c9a5 1325 dout("ceph_queue_writeback %p failed\n", inode);
3c6f6b79
SW
1326 }
1327}
1328
1329static void ceph_writeback_work(struct work_struct *work)
355da1eb
SW
1330{
1331 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1332 i_wb_work);
1333 struct inode *inode = &ci->vfs_inode;
1334
1335 dout("writeback %p\n", inode);
1336 filemap_fdatawrite(&inode->i_data);
1337 iput(inode);
1338}
1339
3c6f6b79
SW
1340/*
1341 * queue an async invalidation
1342 */
1343void ceph_queue_invalidate(struct inode *inode)
1344{
1345 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1346 &ceph_inode(inode)->i_pg_inv_work)) {
1347 dout("ceph_queue_invalidate %p\n", inode);
1348 igrab(inode);
1349 } else {
1350 dout("ceph_queue_invalidate %p failed\n", inode);
1351 }
1352}
1353
c9af9fb6
YS
1354/*
1355 * invalidate any pages that are not dirty or under writeback. this
1356 * includes pages that are clean and mapped.
1357 */
1358static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
1359{
1360 struct pagevec pvec;
1361 pgoff_t next = 0;
1362 int i;
1363
1364 pagevec_init(&pvec, 0);
1365 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
1366 for (i = 0; i < pagevec_count(&pvec); i++) {
1367 struct page *page = pvec.pages[i];
1368 pgoff_t index;
1369 int skip_page =
1370 (PageDirty(page) || PageWriteback(page));
1371
1372 if (!skip_page)
1373 skip_page = !trylock_page(page);
1374
1375 /*
1376 * We really shouldn't be looking at the ->index of an
1377 * unlocked page. But we're not allowed to lock these
1378 * pages. So we rely upon nobody altering the ->index
1379 * of this (pinned-by-us) page.
1380 */
1381 index = page->index;
1382 if (index > next)
1383 next = index;
1384 next++;
1385
1386 if (skip_page)
1387 continue;
1388
1389 generic_error_remove_page(mapping, page);
1390 unlock_page(page);
1391 }
1392 pagevec_release(&pvec);
1393 cond_resched();
1394 }
1395}
1396
355da1eb
SW
1397/*
1398 * Invalidate inode pages in a worker thread. (This can't be done
1399 * in the message handler context.)
1400 */
3c6f6b79 1401static void ceph_invalidate_work(struct work_struct *work)
355da1eb
SW
1402{
1403 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1404 i_pg_inv_work);
1405 struct inode *inode = &ci->vfs_inode;
1406 u32 orig_gen;
1407 int check = 0;
1408
1409 spin_lock(&inode->i_lock);
1410 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1411 ci->i_rdcache_gen, ci->i_rdcache_revoking);
cd045cb4 1412 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
355da1eb 1413 /* nevermind! */
355da1eb
SW
1414 spin_unlock(&inode->i_lock);
1415 goto out;
1416 }
1417 orig_gen = ci->i_rdcache_gen;
1418 spin_unlock(&inode->i_lock);
1419
c9af9fb6 1420 ceph_invalidate_nondirty_pages(inode->i_mapping);
355da1eb
SW
1421
1422 spin_lock(&inode->i_lock);
cd045cb4
SW
1423 if (orig_gen == ci->i_rdcache_gen &&
1424 orig_gen == ci->i_rdcache_revoking) {
355da1eb
SW
1425 dout("invalidate_pages %p gen %d successful\n", inode,
1426 ci->i_rdcache_gen);
cd045cb4 1427 ci->i_rdcache_revoking--;
355da1eb
SW
1428 check = 1;
1429 } else {
cd045cb4
SW
1430 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1431 inode, orig_gen, ci->i_rdcache_gen,
1432 ci->i_rdcache_revoking);
355da1eb
SW
1433 }
1434 spin_unlock(&inode->i_lock);
1435
1436 if (check)
1437 ceph_check_caps(ci, 0, NULL);
1438out:
1439 iput(inode);
1440}
1441
1442
1443/*
1444 * called by trunc_wq; take i_mutex ourselves
1445 *
1446 * We also truncate in a separate thread as well.
1447 */
3c6f6b79 1448static void ceph_vmtruncate_work(struct work_struct *work)
355da1eb
SW
1449{
1450 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1451 i_vmtruncate_work);
1452 struct inode *inode = &ci->vfs_inode;
1453
1454 dout("vmtruncate_work %p\n", inode);
1455 mutex_lock(&inode->i_mutex);
1456 __ceph_do_pending_vmtruncate(inode);
1457 mutex_unlock(&inode->i_mutex);
1458 iput(inode);
1459}
1460
3c6f6b79
SW
1461/*
1462 * Queue an async vmtruncate. If we fail to queue work, we will handle
1463 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1464 */
1465void ceph_queue_vmtruncate(struct inode *inode)
1466{
1467 struct ceph_inode_info *ci = ceph_inode(inode);
1468
640ef79d 1469 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
3c6f6b79
SW
1470 &ci->i_vmtruncate_work)) {
1471 dout("ceph_queue_vmtruncate %p\n", inode);
1472 igrab(inode);
1473 } else {
1474 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1475 inode, ci->i_truncate_pending);
1476 }
1477}
1478
355da1eb
SW
1479/*
1480 * called with i_mutex held.
1481 *
1482 * Make sure any pending truncation is applied before doing anything
1483 * that may depend on it.
1484 */
1485void __ceph_do_pending_vmtruncate(struct inode *inode)
1486{
1487 struct ceph_inode_info *ci = ceph_inode(inode);
1488 u64 to;
1489 int wrbuffer_refs, wake = 0;
1490
1491retry:
1492 spin_lock(&inode->i_lock);
1493 if (ci->i_truncate_pending == 0) {
1494 dout("__do_pending_vmtruncate %p none pending\n", inode);
1495 spin_unlock(&inode->i_lock);
1496 return;
1497 }
1498
1499 /*
1500 * make sure any dirty snapped pages are flushed before we
1501 * possibly truncate them.. so write AND block!
1502 */
1503 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1504 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1505 inode);
1506 spin_unlock(&inode->i_lock);
1507 filemap_write_and_wait_range(&inode->i_data, 0,
1508 inode->i_sb->s_maxbytes);
1509 goto retry;
1510 }
1511
1512 to = ci->i_truncate_size;
1513 wrbuffer_refs = ci->i_wrbuffer_ref;
1514 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1515 ci->i_truncate_pending, to);
1516 spin_unlock(&inode->i_lock);
1517
1518 truncate_inode_pages(inode->i_mapping, to);
1519
1520 spin_lock(&inode->i_lock);
1521 ci->i_truncate_pending--;
1522 if (ci->i_truncate_pending == 0)
1523 wake = 1;
1524 spin_unlock(&inode->i_lock);
1525
1526 if (wrbuffer_refs == 0)
1527 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1528 if (wake)
03066f23 1529 wake_up_all(&ci->i_cap_wq);
355da1eb
SW
1530}
1531
1532
1533/*
1534 * symlinks
1535 */
1536static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1537{
1538 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1539 nd_set_link(nd, ci->i_symlink);
1540 return NULL;
1541}
1542
1543static const struct inode_operations ceph_symlink_iops = {
1544 .readlink = generic_readlink,
1545 .follow_link = ceph_sym_follow_link,
1546};
1547
1548/*
1549 * setattr
1550 */
1551int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1552{
1553 struct inode *inode = dentry->d_inode;
1554 struct ceph_inode_info *ci = ceph_inode(inode);
1555 struct inode *parent_inode = dentry->d_parent->d_inode;
1556 const unsigned int ia_valid = attr->ia_valid;
1557 struct ceph_mds_request *req;
3d14c5d2 1558 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
355da1eb
SW
1559 int issued;
1560 int release = 0, dirtied = 0;
1561 int mask = 0;
1562 int err = 0;
355da1eb
SW
1563
1564 if (ceph_snap(inode) != CEPH_NOSNAP)
1565 return -EROFS;
1566
1567 __ceph_do_pending_vmtruncate(inode);
1568
1569 err = inode_change_ok(inode, attr);
1570 if (err != 0)
1571 return err;
1572
1573 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1574 USE_AUTH_MDS);
1575 if (IS_ERR(req))
1576 return PTR_ERR(req);
1577
1578 spin_lock(&inode->i_lock);
1579 issued = __ceph_caps_issued(ci, NULL);
1580 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1581
1582 if (ia_valid & ATTR_UID) {
1583 dout("setattr %p uid %d -> %d\n", inode,
1584 inode->i_uid, attr->ia_uid);
1585 if (issued & CEPH_CAP_AUTH_EXCL) {
1586 inode->i_uid = attr->ia_uid;
1587 dirtied |= CEPH_CAP_AUTH_EXCL;
1588 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1589 attr->ia_uid != inode->i_uid) {
1590 req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1591 mask |= CEPH_SETATTR_UID;
1592 release |= CEPH_CAP_AUTH_SHARED;
1593 }
1594 }
1595 if (ia_valid & ATTR_GID) {
1596 dout("setattr %p gid %d -> %d\n", inode,
1597 inode->i_gid, attr->ia_gid);
1598 if (issued & CEPH_CAP_AUTH_EXCL) {
1599 inode->i_gid = attr->ia_gid;
1600 dirtied |= CEPH_CAP_AUTH_EXCL;
1601 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1602 attr->ia_gid != inode->i_gid) {
1603 req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1604 mask |= CEPH_SETATTR_GID;
1605 release |= CEPH_CAP_AUTH_SHARED;
1606 }
1607 }
1608 if (ia_valid & ATTR_MODE) {
1609 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1610 attr->ia_mode);
1611 if (issued & CEPH_CAP_AUTH_EXCL) {
1612 inode->i_mode = attr->ia_mode;
1613 dirtied |= CEPH_CAP_AUTH_EXCL;
1614 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1615 attr->ia_mode != inode->i_mode) {
1616 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1617 mask |= CEPH_SETATTR_MODE;
1618 release |= CEPH_CAP_AUTH_SHARED;
1619 }
1620 }
1621
1622 if (ia_valid & ATTR_ATIME) {
1623 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1624 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1625 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1626 if (issued & CEPH_CAP_FILE_EXCL) {
1627 ci->i_time_warp_seq++;
1628 inode->i_atime = attr->ia_atime;
1629 dirtied |= CEPH_CAP_FILE_EXCL;
1630 } else if ((issued & CEPH_CAP_FILE_WR) &&
1631 timespec_compare(&inode->i_atime,
1632 &attr->ia_atime) < 0) {
1633 inode->i_atime = attr->ia_atime;
1634 dirtied |= CEPH_CAP_FILE_WR;
1635 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1636 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1637 ceph_encode_timespec(&req->r_args.setattr.atime,
1638 &attr->ia_atime);
1639 mask |= CEPH_SETATTR_ATIME;
1640 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1641 CEPH_CAP_FILE_WR;
1642 }
1643 }
1644 if (ia_valid & ATTR_MTIME) {
1645 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1646 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1647 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1648 if (issued & CEPH_CAP_FILE_EXCL) {
1649 ci->i_time_warp_seq++;
1650 inode->i_mtime = attr->ia_mtime;
1651 dirtied |= CEPH_CAP_FILE_EXCL;
1652 } else if ((issued & CEPH_CAP_FILE_WR) &&
1653 timespec_compare(&inode->i_mtime,
1654 &attr->ia_mtime) < 0) {
1655 inode->i_mtime = attr->ia_mtime;
1656 dirtied |= CEPH_CAP_FILE_WR;
1657 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1658 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1659 ceph_encode_timespec(&req->r_args.setattr.mtime,
1660 &attr->ia_mtime);
1661 mask |= CEPH_SETATTR_MTIME;
1662 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1663 CEPH_CAP_FILE_WR;
1664 }
1665 }
1666 if (ia_valid & ATTR_SIZE) {
1667 dout("setattr %p size %lld -> %lld\n", inode,
1668 inode->i_size, attr->ia_size);
1669 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1670 err = -EINVAL;
1671 goto out;
1672 }
1673 if ((issued & CEPH_CAP_FILE_EXCL) &&
1674 attr->ia_size > inode->i_size) {
1675 inode->i_size = attr->ia_size;
355da1eb
SW
1676 inode->i_blocks =
1677 (attr->ia_size + (1 << 9) - 1) >> 9;
1678 inode->i_ctime = attr->ia_ctime;
1679 ci->i_reported_size = attr->ia_size;
1680 dirtied |= CEPH_CAP_FILE_EXCL;
1681 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1682 attr->ia_size != inode->i_size) {
1683 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1684 req->r_args.setattr.old_size =
1685 cpu_to_le64(inode->i_size);
1686 mask |= CEPH_SETATTR_SIZE;
1687 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1688 CEPH_CAP_FILE_WR;
1689 }
1690 }
1691
1692 /* these do nothing */
1693 if (ia_valid & ATTR_CTIME) {
1694 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1695 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1696 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1697 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1698 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1699 only ? "ctime only" : "ignored");
1700 inode->i_ctime = attr->ia_ctime;
1701 if (only) {
1702 /*
1703 * if kernel wants to dirty ctime but nothing else,
1704 * we need to choose a cap to dirty under, or do
1705 * a almost-no-op setattr
1706 */
1707 if (issued & CEPH_CAP_AUTH_EXCL)
1708 dirtied |= CEPH_CAP_AUTH_EXCL;
1709 else if (issued & CEPH_CAP_FILE_EXCL)
1710 dirtied |= CEPH_CAP_FILE_EXCL;
1711 else if (issued & CEPH_CAP_XATTR_EXCL)
1712 dirtied |= CEPH_CAP_XATTR_EXCL;
1713 else
1714 mask |= CEPH_SETATTR_CTIME;
1715 }
1716 }
1717 if (ia_valid & ATTR_FILE)
1718 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1719
1720 if (dirtied) {
1721 __ceph_mark_dirty_caps(ci, dirtied);
1722 inode->i_ctime = CURRENT_TIME;
1723 }
1724
1725 release &= issued;
1726 spin_unlock(&inode->i_lock);
1727
355da1eb
SW
1728 if (mask) {
1729 req->r_inode = igrab(inode);
1730 req->r_inode_drop = release;
1731 req->r_args.setattr.mask = cpu_to_le32(mask);
1732 req->r_num_caps = 1;
1733 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1734 }
1735 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1736 ceph_cap_string(dirtied), mask);
1737
1738 ceph_mdsc_put_request(req);
1739 __ceph_do_pending_vmtruncate(inode);
1740 return err;
1741out:
1742 spin_unlock(&inode->i_lock);
1743 ceph_mdsc_put_request(req);
1744 return err;
1745}
1746
1747/*
1748 * Verify that we have a lease on the given mask. If not,
1749 * do a getattr against an mds.
1750 */
1751int ceph_do_getattr(struct inode *inode, int mask)
1752{
3d14c5d2
YS
1753 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1754 struct ceph_mds_client *mdsc = fsc->mdsc;
355da1eb
SW
1755 struct ceph_mds_request *req;
1756 int err;
1757
1758 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1759 dout("do_getattr inode %p SNAPDIR\n", inode);
1760 return 0;
1761 }
1762
b7495fc2 1763 dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
355da1eb
SW
1764 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1765 return 0;
1766
1767 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1768 if (IS_ERR(req))
1769 return PTR_ERR(req);
1770 req->r_inode = igrab(inode);
1771 req->r_num_caps = 1;
1772 req->r_args.getattr.mask = cpu_to_le32(mask);
1773 err = ceph_mdsc_do_request(mdsc, NULL, req);
1774 ceph_mdsc_put_request(req);
1775 dout("do_getattr result=%d\n", err);
1776 return err;
1777}
1778
1779
1780/*
1781 * Check inode permissions. We verify we have a valid value for
1782 * the AUTH cap, then call the generic handler.
1783 */
1784int ceph_permission(struct inode *inode, int mask)
1785{
1786 int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1787
1788 if (!err)
1789 err = generic_permission(inode, mask, NULL);
1790 return err;
1791}
1792
1793/*
1794 * Get all attributes. Hopefully somedata we'll have a statlite()
1795 * and can limit the fields we require to be accurate.
1796 */
1797int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1798 struct kstat *stat)
1799{
1800 struct inode *inode = dentry->d_inode;
232d4b01 1801 struct ceph_inode_info *ci = ceph_inode(inode);
355da1eb
SW
1802 int err;
1803
1804 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1805 if (!err) {
1806 generic_fillattr(inode, stat);
1807 stat->ino = inode->i_ino;
1808 if (ceph_snap(inode) != CEPH_NOSNAP)
1809 stat->dev = ceph_snap(inode);
1810 else
1811 stat->dev = 0;
232d4b01
SW
1812 if (S_ISDIR(inode->i_mode)) {
1813 stat->size = ci->i_rbytes;
1814 stat->blocks = 0;
355da1eb 1815 stat->blksize = 65536;
232d4b01 1816 }
355da1eb
SW
1817 }
1818 return err;
1819}