blob: 226f5a50d36294163e44de833c6949967c04348d [file] [log] [blame]
Sage Weil355da1e2009-10-06 11:31:08 -07001#include "ceph_debug.h"
2
3#include <linux/module.h>
4#include <linux/fs.h>
5#include <linux/smp_lock.h>
6#include <linux/slab.h>
7#include <linux/string.h>
8#include <linux/uaccess.h>
9#include <linux/kernel.h>
10#include <linux/namei.h>
11#include <linux/writeback.h>
12#include <linux/vmalloc.h>
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +000013#include <linux/pagevec.h>
Sage Weil355da1e2009-10-06 11:31:08 -070014
15#include "super.h"
16#include "decode.h"
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
Sage Weil3c6f6b72010-02-09 15:24:44 -080032static 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);
Sage Weil355da1e2009-10-06 11:31:08 -070035
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);
Sage Weilb377ff12009-11-11 15:22:37 -080068 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -070069
70 BUG_ON(!S_ISDIR(parent->i_mode));
71 if (IS_ERR(inode))
Julia Lawall7e34bc52010-05-22 12:01:14 +020072 return inode;
Sage Weil355da1e2009-10-06 11:31:08 -070073 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;
Sage Weilb377ff12009-11-11 15:22:37 -080078 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
79 ci->i_rbytes = 0;
Sage Weil355da1e2009-10-06 11:31:08 -070080 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
Sage Weil3c6f6b72010-02-09 15:24:44 -0800363 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
364 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
Sage Weil355da1e2009-10-06 11:31:08 -0700365
366 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
367
368 return &ci->vfs_inode;
369}
370
371void ceph_destroy_inode(struct inode *inode)
372{
373 struct ceph_inode_info *ci = ceph_inode(inode);
374 struct ceph_inode_frag *frag;
375 struct rb_node *n;
376
377 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
378
379 ceph_queue_caps_release(inode);
380
Sage Weil8b218b82010-03-09 12:59:08 -0800381 /*
382 * we may still have a snap_realm reference if there are stray
383 * caps in i_cap_exporting_issued or i_snap_caps.
384 */
385 if (ci->i_snap_realm) {
386 struct ceph_mds_client *mdsc =
Cheng Renquan640ef792010-03-26 17:40:33 +0800387 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil8b218b82010-03-09 12:59:08 -0800388 struct ceph_snap_realm *realm = ci->i_snap_realm;
389
390 dout(" dropping residual ref to snap realm %p\n", realm);
391 spin_lock(&realm->inodes_with_caps_lock);
392 list_del_init(&ci->i_snap_realm_item);
393 spin_unlock(&realm->inodes_with_caps_lock);
394 ceph_put_snap_realm(mdsc, realm);
395 }
396
Sage Weil355da1e2009-10-06 11:31:08 -0700397 kfree(ci->i_symlink);
398 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
399 frag = rb_entry(n, struct ceph_inode_frag, node);
400 rb_erase(n, &ci->i_fragtree);
401 kfree(frag);
402 }
403
404 __ceph_destroy_xattrs(ci);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800405 if (ci->i_xattrs.blob)
406 ceph_buffer_put(ci->i_xattrs.blob);
407 if (ci->i_xattrs.prealloc_blob)
408 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700409
410 kmem_cache_free(ceph_inode_cachep, ci);
411}
412
413
414/*
415 * Helpers to fill in size, ctime, mtime, and atime. We have to be
416 * careful because either the client or MDS may have more up to date
417 * info, depending on which capabilities are held, and whether
418 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
419 * and size are monotonically increasing, except when utimes() or
420 * truncate() increments the corresponding _seq values.)
421 */
422int ceph_fill_file_size(struct inode *inode, int issued,
423 u32 truncate_seq, u64 truncate_size, u64 size)
424{
425 struct ceph_inode_info *ci = ceph_inode(inode);
426 int queue_trunc = 0;
427
428 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
429 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
430 dout("size %lld -> %llu\n", inode->i_size, size);
431 inode->i_size = size;
432 inode->i_blocks = (size + (1<<9) - 1) >> 9;
433 ci->i_reported_size = size;
434 if (truncate_seq != ci->i_truncate_seq) {
435 dout("truncate_seq %u -> %u\n",
436 ci->i_truncate_seq, truncate_seq);
437 ci->i_truncate_seq = truncate_seq;
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800438 /*
439 * If we hold relevant caps, or in the case where we're
440 * not the only client referencing this file and we
441 * don't hold those caps, then we need to check whether
442 * the file is either opened or mmaped
443 */
444 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
Sage Weil355da1e2009-10-06 11:31:08 -0700445 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800446 CEPH_CAP_FILE_EXCL)) ||
447 mapping_mapped(inode->i_mapping) ||
448 __ceph_caps_file_wanted(ci)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700449 ci->i_truncate_pending++;
450 queue_trunc = 1;
451 }
452 }
453 }
454 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
455 ci->i_truncate_size != truncate_size) {
456 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
457 truncate_size);
458 ci->i_truncate_size = truncate_size;
459 }
460 return queue_trunc;
461}
462
463void ceph_fill_file_time(struct inode *inode, int issued,
464 u64 time_warp_seq, struct timespec *ctime,
465 struct timespec *mtime, struct timespec *atime)
466{
467 struct ceph_inode_info *ci = ceph_inode(inode);
468 int warn = 0;
469
470 if (issued & (CEPH_CAP_FILE_EXCL|
471 CEPH_CAP_FILE_WR|
472 CEPH_CAP_FILE_BUFFER)) {
473 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
474 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
475 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
476 ctime->tv_sec, ctime->tv_nsec);
477 inode->i_ctime = *ctime;
478 }
479 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
480 /* the MDS did a utimes() */
481 dout("mtime %ld.%09ld -> %ld.%09ld "
482 "tw %d -> %d\n",
483 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
484 mtime->tv_sec, mtime->tv_nsec,
485 ci->i_time_warp_seq, (int)time_warp_seq);
486
487 inode->i_mtime = *mtime;
488 inode->i_atime = *atime;
489 ci->i_time_warp_seq = time_warp_seq;
490 } else if (time_warp_seq == ci->i_time_warp_seq) {
491 /* nobody did utimes(); take the max */
492 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
493 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
494 inode->i_mtime.tv_sec,
495 inode->i_mtime.tv_nsec,
496 mtime->tv_sec, mtime->tv_nsec);
497 inode->i_mtime = *mtime;
498 }
499 if (timespec_compare(atime, &inode->i_atime) > 0) {
500 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
501 inode->i_atime.tv_sec,
502 inode->i_atime.tv_nsec,
503 atime->tv_sec, atime->tv_nsec);
504 inode->i_atime = *atime;
505 }
506 } else if (issued & CEPH_CAP_FILE_EXCL) {
507 /* we did a utimes(); ignore mds values */
508 } else {
509 warn = 1;
510 }
511 } else {
512 /* we have no write caps; whatever the MDS says is true */
513 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
514 inode->i_ctime = *ctime;
515 inode->i_mtime = *mtime;
516 inode->i_atime = *atime;
517 ci->i_time_warp_seq = time_warp_seq;
518 } else {
519 warn = 1;
520 }
521 }
522 if (warn) /* time_warp_seq shouldn't go backwards */
523 dout("%p mds time_warp_seq %llu < %u\n",
524 inode, time_warp_seq, ci->i_time_warp_seq);
525}
526
527/*
528 * Populate an inode based on info from mds. May be called on new or
529 * existing inodes.
530 */
531static int fill_inode(struct inode *inode,
532 struct ceph_mds_reply_info_in *iinfo,
533 struct ceph_mds_reply_dirfrag *dirinfo,
534 struct ceph_mds_session *session,
535 unsigned long ttl_from, int cap_fmode,
536 struct ceph_cap_reservation *caps_reservation)
537{
538 struct ceph_mds_reply_inode *info = iinfo->in;
539 struct ceph_inode_info *ci = ceph_inode(inode);
540 int i;
541 int issued, implemented;
542 struct timespec mtime, atime, ctime;
543 u32 nsplits;
544 struct ceph_buffer *xattr_blob = NULL;
545 int err = 0;
546 int queue_trunc = 0;
547
548 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
549 inode, ceph_vinop(inode), le64_to_cpu(info->version),
550 ci->i_version);
551
552 /*
553 * prealloc xattr data, if it looks like we'll need it. only
554 * if len > 4 (meaning there are actually xattrs; the first 4
555 * bytes are the xattr count).
556 */
557 if (iinfo->xattr_len > 4) {
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800558 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700559 if (!xattr_blob)
560 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
561 iinfo->xattr_len);
562 }
563
564 spin_lock(&inode->i_lock);
565
566 /*
567 * provided version will be odd if inode value is projected,
568 * even if stable. skip the update if we have a newer info
569 * (e.g., due to inode info racing form multiple MDSs), or if
570 * we are getting projected (unstable) inode info.
571 */
572 if (le64_to_cpu(info->version) > 0 &&
573 (ci->i_version & ~1) > le64_to_cpu(info->version))
574 goto no_change;
575
576 issued = __ceph_caps_issued(ci, &implemented);
577 issued |= implemented | __ceph_caps_dirty(ci);
578
579 /* update inode */
580 ci->i_version = le64_to_cpu(info->version);
581 inode->i_version++;
582 inode->i_rdev = le32_to_cpu(info->rdev);
583
584 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
585 inode->i_mode = le32_to_cpu(info->mode);
586 inode->i_uid = le32_to_cpu(info->uid);
587 inode->i_gid = le32_to_cpu(info->gid);
588 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
589 inode->i_uid, inode->i_gid);
590 }
591
592 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
593 inode->i_nlink = le32_to_cpu(info->nlink);
594
595 /* be careful with mtime, atime, size */
596 ceph_decode_timespec(&atime, &info->atime);
597 ceph_decode_timespec(&mtime, &info->mtime);
598 ceph_decode_timespec(&ctime, &info->ctime);
599 queue_trunc = ceph_fill_file_size(inode, issued,
600 le32_to_cpu(info->truncate_seq),
601 le64_to_cpu(info->truncate_size),
Sage Weil355da1e2009-10-06 11:31:08 -0700602 le64_to_cpu(info->size));
603 ceph_fill_file_time(inode, issued,
604 le32_to_cpu(info->time_warp_seq),
605 &ctime, &mtime, &atime);
606
607 ci->i_max_size = le64_to_cpu(info->max_size);
608 ci->i_layout = info->layout;
609 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
610
611 /* xattrs */
612 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
613 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
614 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
615 if (ci->i_xattrs.blob)
616 ceph_buffer_put(ci->i_xattrs.blob);
617 ci->i_xattrs.blob = xattr_blob;
618 if (xattr_blob)
619 memcpy(ci->i_xattrs.blob->vec.iov_base,
620 iinfo->xattr_data, iinfo->xattr_len);
621 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
Sage Weila6424e42010-04-29 09:28:11 -0700622 xattr_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700623 }
624
625 inode->i_mapping->a_ops = &ceph_aops;
626 inode->i_mapping->backing_dev_info =
Cheng Renquan640ef792010-03-26 17:40:33 +0800627 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
Sage Weil355da1e2009-10-06 11:31:08 -0700628
629 switch (inode->i_mode & S_IFMT) {
630 case S_IFIFO:
631 case S_IFBLK:
632 case S_IFCHR:
633 case S_IFSOCK:
634 init_special_inode(inode, inode->i_mode, inode->i_rdev);
635 inode->i_op = &ceph_file_iops;
636 break;
637 case S_IFREG:
638 inode->i_op = &ceph_file_iops;
639 inode->i_fop = &ceph_file_fops;
640 break;
641 case S_IFLNK:
642 inode->i_op = &ceph_symlink_iops;
643 if (!ci->i_symlink) {
644 int symlen = iinfo->symlink_len;
645 char *sym;
646
647 BUG_ON(symlen != inode->i_size);
648 spin_unlock(&inode->i_lock);
649
650 err = -ENOMEM;
651 sym = kmalloc(symlen+1, GFP_NOFS);
652 if (!sym)
653 goto out;
654 memcpy(sym, iinfo->symlink, symlen);
655 sym[symlen] = 0;
656
657 spin_lock(&inode->i_lock);
658 if (!ci->i_symlink)
659 ci->i_symlink = sym;
660 else
661 kfree(sym); /* lost a race */
662 }
663 break;
664 case S_IFDIR:
665 inode->i_op = &ceph_dir_iops;
666 inode->i_fop = &ceph_dir_fops;
667
668 ci->i_files = le64_to_cpu(info->files);
669 ci->i_subdirs = le64_to_cpu(info->subdirs);
670 ci->i_rbytes = le64_to_cpu(info->rbytes);
671 ci->i_rfiles = le64_to_cpu(info->rfiles);
672 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
673 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
674
675 /* set dir completion flag? */
676 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
677 ceph_snap(inode) == CEPH_NOSNAP &&
Sage Weil1b7facc2010-04-16 12:58:02 -0700678 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
679 (ci->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
Sage Weil355da1e2009-10-06 11:31:08 -0700680 dout(" marking %p complete (empty)\n", inode);
681 ci->i_ceph_flags |= CEPH_I_COMPLETE;
682 ci->i_max_offset = 2;
683 }
684
685 /* it may be better to set st_size in getattr instead? */
Cheng Renquan640ef792010-03-26 17:40:33 +0800686 if (ceph_test_opt(ceph_sb_to_client(inode->i_sb), RBYTES))
Sage Weil355da1e2009-10-06 11:31:08 -0700687 inode->i_size = ci->i_rbytes;
688 break;
689 default:
690 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
691 ceph_vinop(inode), inode->i_mode);
692 }
693
694no_change:
695 spin_unlock(&inode->i_lock);
696
697 /* queue truncate if we saw i_size decrease */
698 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -0800699 ceph_queue_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700700
701 /* populate frag tree */
702 /* FIXME: move me up, if/when version reflects fragtree changes */
703 nsplits = le32_to_cpu(info->fragtree.nsplits);
704 mutex_lock(&ci->i_fragtree_mutex);
705 for (i = 0; i < nsplits; i++) {
706 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
707 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
708
709 if (IS_ERR(frag))
710 continue;
711 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
712 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
713 }
714 mutex_unlock(&ci->i_fragtree_mutex);
715
716 /* were we issued a capability? */
717 if (info->cap.caps) {
718 if (ceph_snap(inode) == CEPH_NOSNAP) {
719 ceph_add_cap(inode, session,
720 le64_to_cpu(info->cap.cap_id),
721 cap_fmode,
722 le32_to_cpu(info->cap.caps),
723 le32_to_cpu(info->cap.wanted),
724 le32_to_cpu(info->cap.seq),
725 le32_to_cpu(info->cap.mseq),
726 le64_to_cpu(info->cap.realm),
727 info->cap.flags,
728 caps_reservation);
729 } else {
730 spin_lock(&inode->i_lock);
731 dout(" %p got snap_caps %s\n", inode,
732 ceph_cap_string(le32_to_cpu(info->cap.caps)));
733 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
734 if (cap_fmode >= 0)
735 __ceph_get_fmode(ci, cap_fmode);
736 spin_unlock(&inode->i_lock);
737 }
Sage Weil04d000e2010-05-07 11:26:34 -0700738 } else if (cap_fmode >= 0) {
739 pr_warning("mds issued no caps on %llx.%llx\n",
740 ceph_vinop(inode));
741 __ceph_get_fmode(ci, cap_fmode);
Sage Weil355da1e2009-10-06 11:31:08 -0700742 }
743
744 /* update delegation info? */
745 if (dirinfo)
746 ceph_fill_dirfrag(inode, dirinfo);
747
748 err = 0;
749
750out:
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800751 if (xattr_blob)
752 ceph_buffer_put(xattr_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700753 return err;
754}
755
756/*
757 * caller should hold session s_mutex.
758 */
759static void update_dentry_lease(struct dentry *dentry,
760 struct ceph_mds_reply_lease *lease,
761 struct ceph_mds_session *session,
762 unsigned long from_time)
763{
764 struct ceph_dentry_info *di = ceph_dentry(dentry);
765 long unsigned duration = le32_to_cpu(lease->duration_ms);
766 long unsigned ttl = from_time + (duration * HZ) / 1000;
767 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
768 struct inode *dir;
769
770 /* only track leases on regular dentries */
771 if (dentry->d_op != &ceph_dentry_ops)
772 return;
773
774 spin_lock(&dentry->d_lock);
775 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
776 dentry, le16_to_cpu(lease->mask), duration, ttl);
777
778 /* make lease_rdcache_gen match directory */
779 dir = dentry->d_parent->d_inode;
780 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
781
782 if (lease->mask == 0)
783 goto out_unlock;
784
785 if (di->lease_gen == session->s_cap_gen &&
786 time_before(ttl, dentry->d_time))
787 goto out_unlock; /* we already have a newer lease. */
788
789 if (di->lease_session && di->lease_session != session)
790 goto out_unlock;
791
792 ceph_dentry_lru_touch(dentry);
793
794 if (!di->lease_session)
795 di->lease_session = ceph_get_mds_session(session);
796 di->lease_gen = session->s_cap_gen;
797 di->lease_seq = le32_to_cpu(lease->seq);
798 di->lease_renew_after = half_ttl;
799 di->lease_renew_from = 0;
800 dentry->d_time = ttl;
801out_unlock:
802 spin_unlock(&dentry->d_lock);
803 return;
804}
805
806/*
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800807 * Set dentry's directory position based on the current dir's max, and
808 * order it in d_subdirs, so that dcache_readdir behaves.
809 */
810static void ceph_set_dentry_offset(struct dentry *dn)
811{
812 struct dentry *dir = dn->d_parent;
813 struct inode *inode = dn->d_parent->d_inode;
814 struct ceph_dentry_info *di;
815
816 BUG_ON(!inode);
817
818 di = ceph_dentry(dn);
819
820 spin_lock(&inode->i_lock);
Sage Weile8a74982010-04-15 14:08:49 -0700821 if ((ceph_inode(inode)->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
822 spin_unlock(&inode->i_lock);
823 return;
824 }
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800825 di->offset = ceph_inode(inode)->i_max_offset++;
826 spin_unlock(&inode->i_lock);
827
828 spin_lock(&dcache_lock);
829 spin_lock(&dn->d_lock);
830 list_move_tail(&dir->d_subdirs, &dn->d_u.d_child);
831 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
832 dn->d_u.d_child.prev, dn->d_u.d_child.next);
833 spin_unlock(&dn->d_lock);
834 spin_unlock(&dcache_lock);
835}
836
837/*
Sage Weil1cd39352010-05-03 22:08:02 -0700838 * splice a dentry to an inode.
839 * caller must hold directory i_mutex for this to be safe.
840 *
841 * we will only rehash the resulting dentry if @prehash is
842 * true; @prehash will be set to false (for the benefit of
843 * the caller) if we fail.
844 */
845static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
846 bool *prehash)
847{
848 struct dentry *realdn;
849
850 BUG_ON(dn->d_inode);
851
852 /* dn must be unhashed */
853 if (!d_unhashed(dn))
854 d_drop(dn);
855 realdn = d_materialise_unique(dn, in);
856 if (IS_ERR(realdn)) {
857 pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
858 dn, in, ceph_vinop(in));
859 if (prehash)
860 *prehash = false; /* don't rehash on error */
861 dn = realdn; /* note realdn contains the error */
862 goto out;
863 } else if (realdn) {
864 dout("dn %p (%d) spliced with %p (%d) "
865 "inode %p ino %llx.%llx\n",
866 dn, atomic_read(&dn->d_count),
867 realdn, atomic_read(&realdn->d_count),
868 realdn->d_inode, ceph_vinop(realdn->d_inode));
869 dput(dn);
870 dn = realdn;
871 } else {
872 BUG_ON(!ceph_dentry(dn));
873 dout("dn %p attached to %p ino %llx.%llx\n",
874 dn, dn->d_inode, ceph_vinop(dn->d_inode));
875 }
876 if ((!prehash || *prehash) && d_unhashed(dn))
877 d_rehash(dn);
878 ceph_set_dentry_offset(dn);
879out:
880 return dn;
881}
882
883/*
Sage Weil355da1e2009-10-06 11:31:08 -0700884 * Incorporate results into the local cache. This is either just
885 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
886 * after a lookup).
887 *
888 * A reply may contain
889 * a directory inode along with a dentry.
890 * and/or a target inode
891 *
892 * Called with snap_rwsem (read).
893 */
894int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
895 struct ceph_mds_session *session)
896{
897 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
898 struct inode *in = NULL;
899 struct ceph_mds_reply_inode *ininfo;
900 struct ceph_vino vino;
Sage Weil9358c6d2010-03-30 13:54:41 -0700901 struct ceph_client *client = ceph_sb_to_client(sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700902 int i = 0;
903 int err = 0;
904
905 dout("fill_trace %p is_dentry %d is_target %d\n", req,
906 rinfo->head->is_dentry, rinfo->head->is_target);
907
908#if 0
909 /*
910 * Debugging hook:
911 *
912 * If we resend completed ops to a recovering mds, we get no
913 * trace. Since that is very rare, pretend this is the case
914 * to ensure the 'no trace' handlers in the callers behave.
915 *
916 * Fill in inodes unconditionally to avoid breaking cap
917 * invariants.
918 */
919 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
920 pr_info("fill_trace faking empty trace on %lld %s\n",
921 req->r_tid, ceph_mds_op_name(rinfo->head->op));
922 if (rinfo->head->is_dentry) {
923 rinfo->head->is_dentry = 0;
924 err = fill_inode(req->r_locked_dir,
925 &rinfo->diri, rinfo->dirfrag,
926 session, req->r_request_started, -1);
927 }
928 if (rinfo->head->is_target) {
929 rinfo->head->is_target = 0;
930 ininfo = rinfo->targeti.in;
931 vino.ino = le64_to_cpu(ininfo->ino);
932 vino.snap = le64_to_cpu(ininfo->snapid);
933 in = ceph_get_inode(sb, vino);
934 err = fill_inode(in, &rinfo->targeti, NULL,
935 session, req->r_request_started,
936 req->r_fmode);
937 iput(in);
938 }
939 }
940#endif
941
942 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
943 dout("fill_trace reply is empty!\n");
Sage Weil167c9e32010-05-14 10:02:57 -0700944 if (rinfo->head->result == 0 && req->r_locked_dir)
945 ceph_invalidate_dir_request(req);
Sage Weil355da1e2009-10-06 11:31:08 -0700946 return 0;
947 }
948
949 if (rinfo->head->is_dentry) {
Sage Weil5b1daec2010-01-25 11:33:08 -0800950 struct inode *dir = req->r_locked_dir;
951
952 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
953 session, req->r_request_started, -1,
954 &req->r_caps_reservation);
955 if (err < 0)
956 return err;
957 }
958
Sage Weil9358c6d2010-03-30 13:54:41 -0700959 /*
960 * ignore null lease/binding on snapdir ENOENT, or else we
961 * will have trouble splicing in the virtual snapdir later
962 */
963 if (rinfo->head->is_dentry && !req->r_aborted &&
964 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
965 client->mount_args->snapdir_name,
966 req->r_dentry->d_name.len))) {
Sage Weil355da1e2009-10-06 11:31:08 -0700967 /*
968 * lookup link rename : null -> possibly existing inode
969 * mknod symlink mkdir : null -> new inode
970 * unlink : linked -> null
971 */
972 struct inode *dir = req->r_locked_dir;
973 struct dentry *dn = req->r_dentry;
974 bool have_dir_cap, have_lease;
975
976 BUG_ON(!dn);
977 BUG_ON(!dir);
978 BUG_ON(dn->d_parent->d_inode != dir);
979 BUG_ON(ceph_ino(dir) !=
980 le64_to_cpu(rinfo->diri.in->ino));
981 BUG_ON(ceph_snap(dir) !=
982 le64_to_cpu(rinfo->diri.in->snapid));
983
Sage Weil355da1e2009-10-06 11:31:08 -0700984 /* do we have a lease on the whole dir? */
985 have_dir_cap =
986 (le32_to_cpu(rinfo->diri.in->cap.caps) &
987 CEPH_CAP_FILE_SHARED);
988
989 /* do we have a dn lease? */
990 have_lease = have_dir_cap ||
991 (le16_to_cpu(rinfo->dlease->mask) &
992 CEPH_LOCK_DN);
993
994 if (!have_lease)
995 dout("fill_trace no dentry lease or dir cap\n");
996
997 /* rename? */
998 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
999 dout(" src %p '%.*s' dst %p '%.*s'\n",
1000 req->r_old_dentry,
1001 req->r_old_dentry->d_name.len,
1002 req->r_old_dentry->d_name.name,
1003 dn, dn->d_name.len, dn->d_name.name);
1004 dout("fill_trace doing d_move %p -> %p\n",
1005 req->r_old_dentry, dn);
Sage Weilc10f5e12010-04-16 12:56:11 -07001006
1007 /* d_move screws up d_subdirs order */
1008 ceph_i_clear(dir, CEPH_I_COMPLETE);
1009
Sage Weil355da1e2009-10-06 11:31:08 -07001010 d_move(req->r_old_dentry, dn);
1011 dout(" src %p '%.*s' dst %p '%.*s'\n",
1012 req->r_old_dentry,
1013 req->r_old_dentry->d_name.len,
1014 req->r_old_dentry->d_name.name,
1015 dn, dn->d_name.len, dn->d_name.name);
Sage Weil81a6cf22010-05-14 09:35:38 -07001016
Sage Weilc4a29f22009-12-21 11:42:18 -08001017 /* ensure target dentry is invalidated, despite
1018 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001019 ceph_invalidate_dentry_lease(dn);
1020
Sage Weil355da1e2009-10-06 11:31:08 -07001021 /* take overwritten dentry's readdir offset */
Sage Weil1cd39352010-05-03 22:08:02 -07001022 dout("dn %p gets %p offset %lld (old offset %lld)\n",
1023 req->r_old_dentry, dn, ceph_dentry(dn)->offset,
1024 ceph_dentry(req->r_old_dentry)->offset);
Sage Weil355da1e2009-10-06 11:31:08 -07001025 ceph_dentry(req->r_old_dentry)->offset =
1026 ceph_dentry(dn)->offset;
Sage Weil81a6cf22010-05-14 09:35:38 -07001027
Sage Weil355da1e2009-10-06 11:31:08 -07001028 dn = req->r_old_dentry; /* use old_dentry */
1029 in = dn->d_inode;
1030 }
1031
1032 /* null dentry? */
1033 if (!rinfo->head->is_target) {
1034 dout("fill_trace null dentry\n");
1035 if (dn->d_inode) {
1036 dout("d_delete %p\n", dn);
1037 d_delete(dn);
1038 } else {
1039 dout("d_instantiate %p NULL\n", dn);
1040 d_instantiate(dn, NULL);
1041 if (have_lease && d_unhashed(dn))
1042 d_rehash(dn);
1043 update_dentry_lease(dn, rinfo->dlease,
1044 session,
1045 req->r_request_started);
1046 }
1047 goto done;
1048 }
1049
1050 /* attach proper inode */
1051 ininfo = rinfo->targeti.in;
1052 vino.ino = le64_to_cpu(ininfo->ino);
1053 vino.snap = le64_to_cpu(ininfo->snapid);
1054 if (!dn->d_inode) {
1055 in = ceph_get_inode(sb, vino);
1056 if (IS_ERR(in)) {
1057 pr_err("fill_trace bad get_inode "
1058 "%llx.%llx\n", vino.ino, vino.snap);
1059 err = PTR_ERR(in);
1060 d_delete(dn);
1061 goto done;
1062 }
1063 dn = splice_dentry(dn, in, &have_lease);
1064 if (IS_ERR(dn)) {
1065 err = PTR_ERR(dn);
1066 goto done;
1067 }
1068 req->r_dentry = dn; /* may have spliced */
1069 igrab(in);
1070 } else if (ceph_ino(in) == vino.ino &&
1071 ceph_snap(in) == vino.snap) {
1072 igrab(in);
1073 } else {
1074 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1075 dn, in, ceph_ino(in), ceph_snap(in),
1076 vino.ino, vino.snap);
1077 have_lease = false;
1078 in = NULL;
1079 }
1080
1081 if (have_lease)
1082 update_dentry_lease(dn, rinfo->dlease, session,
1083 req->r_request_started);
1084 dout(" final dn %p\n", dn);
1085 i++;
1086 } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1087 req->r_op == CEPH_MDS_OP_MKSNAP) {
1088 struct dentry *dn = req->r_dentry;
1089
1090 /* fill out a snapdir LOOKUPSNAP dentry */
1091 BUG_ON(!dn);
1092 BUG_ON(!req->r_locked_dir);
1093 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1094 ininfo = rinfo->targeti.in;
1095 vino.ino = le64_to_cpu(ininfo->ino);
1096 vino.snap = le64_to_cpu(ininfo->snapid);
1097 in = ceph_get_inode(sb, vino);
1098 if (IS_ERR(in)) {
1099 pr_err("fill_inode get_inode badness %llx.%llx\n",
1100 vino.ino, vino.snap);
1101 err = PTR_ERR(in);
1102 d_delete(dn);
1103 goto done;
1104 }
1105 dout(" linking snapped dir %p to dn %p\n", in, dn);
1106 dn = splice_dentry(dn, in, NULL);
1107 if (IS_ERR(dn)) {
1108 err = PTR_ERR(dn);
1109 goto done;
1110 }
1111 req->r_dentry = dn; /* may have spliced */
1112 igrab(in);
1113 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1114 }
1115
1116 if (rinfo->head->is_target) {
1117 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1118 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1119
1120 if (in == NULL || ceph_ino(in) != vino.ino ||
1121 ceph_snap(in) != vino.snap) {
1122 in = ceph_get_inode(sb, vino);
1123 if (IS_ERR(in)) {
1124 err = PTR_ERR(in);
1125 goto done;
1126 }
1127 }
1128 req->r_target_inode = in;
1129
1130 err = fill_inode(in,
1131 &rinfo->targeti, NULL,
1132 session, req->r_request_started,
1133 (le32_to_cpu(rinfo->head->result) == 0) ?
1134 req->r_fmode : -1,
1135 &req->r_caps_reservation);
1136 if (err < 0) {
1137 pr_err("fill_inode badness %p %llx.%llx\n",
1138 in, ceph_vinop(in));
1139 goto done;
1140 }
1141 }
1142
1143done:
1144 dout("fill_trace done err=%d\n", err);
1145 return err;
1146}
1147
1148/*
1149 * Prepopulate our cache with readdir results, leases, etc.
1150 */
1151int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1152 struct ceph_mds_session *session)
1153{
1154 struct dentry *parent = req->r_dentry;
1155 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1156 struct qstr dname;
1157 struct dentry *dn;
1158 struct inode *in;
1159 int err = 0, i;
1160 struct inode *snapdir = NULL;
1161 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1162 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1163 struct ceph_dentry_info *di;
1164
1165 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1166 snapdir = ceph_get_snapdir(parent->d_inode);
1167 parent = d_find_alias(snapdir);
1168 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1169 rinfo->dir_nr, parent);
1170 } else {
1171 dout("readdir_prepopulate %d items under dn %p\n",
1172 rinfo->dir_nr, parent);
1173 if (rinfo->dir_dir)
1174 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1175 }
1176
1177 for (i = 0; i < rinfo->dir_nr; i++) {
1178 struct ceph_vino vino;
1179
1180 dname.name = rinfo->dir_dname[i];
1181 dname.len = rinfo->dir_dname_len[i];
1182 dname.hash = full_name_hash(dname.name, dname.len);
1183
1184 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1185 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1186
1187retry_lookup:
1188 dn = d_lookup(parent, &dname);
1189 dout("d_lookup on parent=%p name=%.*s got %p\n",
1190 parent, dname.len, dname.name, dn);
1191
1192 if (!dn) {
1193 dn = d_alloc(parent, &dname);
1194 dout("d_alloc %p '%.*s' = %p\n", parent,
1195 dname.len, dname.name, dn);
1196 if (dn == NULL) {
1197 dout("d_alloc badness\n");
1198 err = -ENOMEM;
1199 goto out;
1200 }
1201 err = ceph_init_dentry(dn);
1202 if (err < 0)
1203 goto out;
1204 } else if (dn->d_inode &&
1205 (ceph_ino(dn->d_inode) != vino.ino ||
1206 ceph_snap(dn->d_inode) != vino.snap)) {
1207 dout(" dn %p points to wrong inode %p\n",
1208 dn, dn->d_inode);
1209 d_delete(dn);
1210 dput(dn);
1211 goto retry_lookup;
1212 } else {
1213 /* reorder parent's d_subdirs */
1214 spin_lock(&dcache_lock);
1215 spin_lock(&dn->d_lock);
1216 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1217 spin_unlock(&dn->d_lock);
1218 spin_unlock(&dcache_lock);
1219 }
1220
1221 di = dn->d_fsdata;
1222 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1223
1224 /* inode */
1225 if (dn->d_inode) {
1226 in = dn->d_inode;
1227 } else {
1228 in = ceph_get_inode(parent->d_sb, vino);
1229 if (in == NULL) {
1230 dout("new_inode badness\n");
1231 d_delete(dn);
1232 dput(dn);
1233 err = -ENOMEM;
1234 goto out;
1235 }
1236 dn = splice_dentry(dn, in, NULL);
1237 }
1238
1239 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1240 req->r_request_started, -1,
1241 &req->r_caps_reservation) < 0) {
1242 pr_err("fill_inode badness on %p\n", in);
1243 dput(dn);
1244 continue;
1245 }
1246 update_dentry_lease(dn, rinfo->dir_dlease[i],
1247 req->r_session, req->r_request_started);
1248 dput(dn);
1249 }
1250 req->r_did_prepopulate = true;
1251
1252out:
1253 if (snapdir) {
1254 iput(snapdir);
1255 dput(parent);
1256 }
1257 dout("readdir_prepopulate done\n");
1258 return err;
1259}
1260
1261int ceph_inode_set_size(struct inode *inode, loff_t size)
1262{
1263 struct ceph_inode_info *ci = ceph_inode(inode);
1264 int ret = 0;
1265
1266 spin_lock(&inode->i_lock);
1267 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1268 inode->i_size = size;
1269 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1270
1271 /* tell the MDS if we are approaching max_size */
1272 if ((size << 1) >= ci->i_max_size &&
1273 (ci->i_reported_size << 1) < ci->i_max_size)
1274 ret = 1;
1275
1276 spin_unlock(&inode->i_lock);
1277 return ret;
1278}
1279
1280/*
1281 * Write back inode data in a worker thread. (This can't be done
1282 * in the message handler context.)
1283 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001284void ceph_queue_writeback(struct inode *inode)
1285{
1286 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1287 &ceph_inode(inode)->i_wb_work)) {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001288 dout("ceph_queue_writeback %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001289 igrab(inode);
1290 } else {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001291 dout("ceph_queue_writeback %p failed\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001292 }
1293}
1294
1295static void ceph_writeback_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001296{
1297 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1298 i_wb_work);
1299 struct inode *inode = &ci->vfs_inode;
1300
1301 dout("writeback %p\n", inode);
1302 filemap_fdatawrite(&inode->i_data);
1303 iput(inode);
1304}
1305
1306/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001307 * queue an async invalidation
1308 */
1309void ceph_queue_invalidate(struct inode *inode)
1310{
1311 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1312 &ceph_inode(inode)->i_pg_inv_work)) {
1313 dout("ceph_queue_invalidate %p\n", inode);
1314 igrab(inode);
1315 } else {
1316 dout("ceph_queue_invalidate %p failed\n", inode);
1317 }
1318}
1319
1320/*
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +00001321 * invalidate any pages that are not dirty or under writeback. this
1322 * includes pages that are clean and mapped.
1323 */
1324static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
1325{
1326 struct pagevec pvec;
1327 pgoff_t next = 0;
1328 int i;
1329
1330 pagevec_init(&pvec, 0);
1331 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
1332 for (i = 0; i < pagevec_count(&pvec); i++) {
1333 struct page *page = pvec.pages[i];
1334 pgoff_t index;
1335 int skip_page =
1336 (PageDirty(page) || PageWriteback(page));
1337
1338 if (!skip_page)
1339 skip_page = !trylock_page(page);
1340
1341 /*
1342 * We really shouldn't be looking at the ->index of an
1343 * unlocked page. But we're not allowed to lock these
1344 * pages. So we rely upon nobody altering the ->index
1345 * of this (pinned-by-us) page.
1346 */
1347 index = page->index;
1348 if (index > next)
1349 next = index;
1350 next++;
1351
1352 if (skip_page)
1353 continue;
1354
1355 generic_error_remove_page(mapping, page);
1356 unlock_page(page);
1357 }
1358 pagevec_release(&pvec);
1359 cond_resched();
1360 }
1361}
1362
1363/*
Sage Weil355da1e2009-10-06 11:31:08 -07001364 * Invalidate inode pages in a worker thread. (This can't be done
1365 * in the message handler context.)
1366 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001367static void ceph_invalidate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001368{
1369 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1370 i_pg_inv_work);
1371 struct inode *inode = &ci->vfs_inode;
1372 u32 orig_gen;
1373 int check = 0;
1374
1375 spin_lock(&inode->i_lock);
1376 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1377 ci->i_rdcache_gen, ci->i_rdcache_revoking);
1378 if (ci->i_rdcache_gen == 0 ||
1379 ci->i_rdcache_revoking != ci->i_rdcache_gen) {
1380 BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
1381 /* nevermind! */
1382 ci->i_rdcache_revoking = 0;
1383 spin_unlock(&inode->i_lock);
1384 goto out;
1385 }
1386 orig_gen = ci->i_rdcache_gen;
1387 spin_unlock(&inode->i_lock);
1388
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +00001389 ceph_invalidate_nondirty_pages(inode->i_mapping);
Sage Weil355da1e2009-10-06 11:31:08 -07001390
1391 spin_lock(&inode->i_lock);
1392 if (orig_gen == ci->i_rdcache_gen) {
1393 dout("invalidate_pages %p gen %d successful\n", inode,
1394 ci->i_rdcache_gen);
1395 ci->i_rdcache_gen = 0;
1396 ci->i_rdcache_revoking = 0;
1397 check = 1;
1398 } else {
1399 dout("invalidate_pages %p gen %d raced, gen now %d\n",
1400 inode, orig_gen, ci->i_rdcache_gen);
1401 }
1402 spin_unlock(&inode->i_lock);
1403
1404 if (check)
1405 ceph_check_caps(ci, 0, NULL);
1406out:
1407 iput(inode);
1408}
1409
1410
1411/*
1412 * called by trunc_wq; take i_mutex ourselves
1413 *
1414 * We also truncate in a separate thread as well.
1415 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001416static void ceph_vmtruncate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001417{
1418 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1419 i_vmtruncate_work);
1420 struct inode *inode = &ci->vfs_inode;
1421
1422 dout("vmtruncate_work %p\n", inode);
1423 mutex_lock(&inode->i_mutex);
1424 __ceph_do_pending_vmtruncate(inode);
1425 mutex_unlock(&inode->i_mutex);
1426 iput(inode);
1427}
1428
1429/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001430 * Queue an async vmtruncate. If we fail to queue work, we will handle
1431 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1432 */
1433void ceph_queue_vmtruncate(struct inode *inode)
1434{
1435 struct ceph_inode_info *ci = ceph_inode(inode);
1436
Cheng Renquan640ef792010-03-26 17:40:33 +08001437 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
Sage Weil3c6f6b72010-02-09 15:24:44 -08001438 &ci->i_vmtruncate_work)) {
1439 dout("ceph_queue_vmtruncate %p\n", inode);
1440 igrab(inode);
1441 } else {
1442 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1443 inode, ci->i_truncate_pending);
1444 }
1445}
1446
1447/*
Sage Weil355da1e2009-10-06 11:31:08 -07001448 * called with i_mutex held.
1449 *
1450 * Make sure any pending truncation is applied before doing anything
1451 * that may depend on it.
1452 */
1453void __ceph_do_pending_vmtruncate(struct inode *inode)
1454{
1455 struct ceph_inode_info *ci = ceph_inode(inode);
1456 u64 to;
1457 int wrbuffer_refs, wake = 0;
1458
1459retry:
1460 spin_lock(&inode->i_lock);
1461 if (ci->i_truncate_pending == 0) {
1462 dout("__do_pending_vmtruncate %p none pending\n", inode);
1463 spin_unlock(&inode->i_lock);
1464 return;
1465 }
1466
1467 /*
1468 * make sure any dirty snapped pages are flushed before we
1469 * possibly truncate them.. so write AND block!
1470 */
1471 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1472 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1473 inode);
1474 spin_unlock(&inode->i_lock);
1475 filemap_write_and_wait_range(&inode->i_data, 0,
1476 inode->i_sb->s_maxbytes);
1477 goto retry;
1478 }
1479
1480 to = ci->i_truncate_size;
1481 wrbuffer_refs = ci->i_wrbuffer_ref;
1482 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1483 ci->i_truncate_pending, to);
1484 spin_unlock(&inode->i_lock);
1485
1486 truncate_inode_pages(inode->i_mapping, to);
1487
1488 spin_lock(&inode->i_lock);
1489 ci->i_truncate_pending--;
1490 if (ci->i_truncate_pending == 0)
1491 wake = 1;
1492 spin_unlock(&inode->i_lock);
1493
1494 if (wrbuffer_refs == 0)
1495 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1496 if (wake)
1497 wake_up(&ci->i_cap_wq);
1498}
1499
1500
1501/*
1502 * symlinks
1503 */
1504static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1505{
1506 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1507 nd_set_link(nd, ci->i_symlink);
1508 return NULL;
1509}
1510
1511static const struct inode_operations ceph_symlink_iops = {
1512 .readlink = generic_readlink,
1513 .follow_link = ceph_sym_follow_link,
1514};
1515
1516/*
1517 * setattr
1518 */
1519int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1520{
1521 struct inode *inode = dentry->d_inode;
1522 struct ceph_inode_info *ci = ceph_inode(inode);
1523 struct inode *parent_inode = dentry->d_parent->d_inode;
1524 const unsigned int ia_valid = attr->ia_valid;
1525 struct ceph_mds_request *req;
Cheng Renquan640ef792010-03-26 17:40:33 +08001526 struct ceph_mds_client *mdsc = &ceph_sb_to_client(dentry->d_sb)->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001527 int issued;
1528 int release = 0, dirtied = 0;
1529 int mask = 0;
1530 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001531
1532 if (ceph_snap(inode) != CEPH_NOSNAP)
1533 return -EROFS;
1534
1535 __ceph_do_pending_vmtruncate(inode);
1536
1537 err = inode_change_ok(inode, attr);
1538 if (err != 0)
1539 return err;
1540
1541 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1542 USE_AUTH_MDS);
1543 if (IS_ERR(req))
1544 return PTR_ERR(req);
1545
1546 spin_lock(&inode->i_lock);
1547 issued = __ceph_caps_issued(ci, NULL);
1548 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1549
1550 if (ia_valid & ATTR_UID) {
1551 dout("setattr %p uid %d -> %d\n", inode,
1552 inode->i_uid, attr->ia_uid);
1553 if (issued & CEPH_CAP_AUTH_EXCL) {
1554 inode->i_uid = attr->ia_uid;
1555 dirtied |= CEPH_CAP_AUTH_EXCL;
1556 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1557 attr->ia_uid != inode->i_uid) {
1558 req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1559 mask |= CEPH_SETATTR_UID;
1560 release |= CEPH_CAP_AUTH_SHARED;
1561 }
1562 }
1563 if (ia_valid & ATTR_GID) {
1564 dout("setattr %p gid %d -> %d\n", inode,
1565 inode->i_gid, attr->ia_gid);
1566 if (issued & CEPH_CAP_AUTH_EXCL) {
1567 inode->i_gid = attr->ia_gid;
1568 dirtied |= CEPH_CAP_AUTH_EXCL;
1569 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1570 attr->ia_gid != inode->i_gid) {
1571 req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1572 mask |= CEPH_SETATTR_GID;
1573 release |= CEPH_CAP_AUTH_SHARED;
1574 }
1575 }
1576 if (ia_valid & ATTR_MODE) {
1577 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1578 attr->ia_mode);
1579 if (issued & CEPH_CAP_AUTH_EXCL) {
1580 inode->i_mode = attr->ia_mode;
1581 dirtied |= CEPH_CAP_AUTH_EXCL;
1582 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1583 attr->ia_mode != inode->i_mode) {
1584 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1585 mask |= CEPH_SETATTR_MODE;
1586 release |= CEPH_CAP_AUTH_SHARED;
1587 }
1588 }
1589
1590 if (ia_valid & ATTR_ATIME) {
1591 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1592 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1593 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1594 if (issued & CEPH_CAP_FILE_EXCL) {
1595 ci->i_time_warp_seq++;
1596 inode->i_atime = attr->ia_atime;
1597 dirtied |= CEPH_CAP_FILE_EXCL;
1598 } else if ((issued & CEPH_CAP_FILE_WR) &&
1599 timespec_compare(&inode->i_atime,
1600 &attr->ia_atime) < 0) {
1601 inode->i_atime = attr->ia_atime;
1602 dirtied |= CEPH_CAP_FILE_WR;
1603 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1604 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1605 ceph_encode_timespec(&req->r_args.setattr.atime,
1606 &attr->ia_atime);
1607 mask |= CEPH_SETATTR_ATIME;
1608 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1609 CEPH_CAP_FILE_WR;
1610 }
1611 }
1612 if (ia_valid & ATTR_MTIME) {
1613 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1614 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1615 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1616 if (issued & CEPH_CAP_FILE_EXCL) {
1617 ci->i_time_warp_seq++;
1618 inode->i_mtime = attr->ia_mtime;
1619 dirtied |= CEPH_CAP_FILE_EXCL;
1620 } else if ((issued & CEPH_CAP_FILE_WR) &&
1621 timespec_compare(&inode->i_mtime,
1622 &attr->ia_mtime) < 0) {
1623 inode->i_mtime = attr->ia_mtime;
1624 dirtied |= CEPH_CAP_FILE_WR;
1625 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1626 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1627 ceph_encode_timespec(&req->r_args.setattr.mtime,
1628 &attr->ia_mtime);
1629 mask |= CEPH_SETATTR_MTIME;
1630 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1631 CEPH_CAP_FILE_WR;
1632 }
1633 }
1634 if (ia_valid & ATTR_SIZE) {
1635 dout("setattr %p size %lld -> %lld\n", inode,
1636 inode->i_size, attr->ia_size);
1637 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1638 err = -EINVAL;
1639 goto out;
1640 }
1641 if ((issued & CEPH_CAP_FILE_EXCL) &&
1642 attr->ia_size > inode->i_size) {
1643 inode->i_size = attr->ia_size;
Sage Weil355da1e2009-10-06 11:31:08 -07001644 inode->i_blocks =
1645 (attr->ia_size + (1 << 9) - 1) >> 9;
1646 inode->i_ctime = attr->ia_ctime;
1647 ci->i_reported_size = attr->ia_size;
1648 dirtied |= CEPH_CAP_FILE_EXCL;
1649 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1650 attr->ia_size != inode->i_size) {
1651 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1652 req->r_args.setattr.old_size =
1653 cpu_to_le64(inode->i_size);
1654 mask |= CEPH_SETATTR_SIZE;
1655 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1656 CEPH_CAP_FILE_WR;
1657 }
1658 }
1659
1660 /* these do nothing */
1661 if (ia_valid & ATTR_CTIME) {
1662 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1663 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1664 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1665 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1666 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1667 only ? "ctime only" : "ignored");
1668 inode->i_ctime = attr->ia_ctime;
1669 if (only) {
1670 /*
1671 * if kernel wants to dirty ctime but nothing else,
1672 * we need to choose a cap to dirty under, or do
1673 * a almost-no-op setattr
1674 */
1675 if (issued & CEPH_CAP_AUTH_EXCL)
1676 dirtied |= CEPH_CAP_AUTH_EXCL;
1677 else if (issued & CEPH_CAP_FILE_EXCL)
1678 dirtied |= CEPH_CAP_FILE_EXCL;
1679 else if (issued & CEPH_CAP_XATTR_EXCL)
1680 dirtied |= CEPH_CAP_XATTR_EXCL;
1681 else
1682 mask |= CEPH_SETATTR_CTIME;
1683 }
1684 }
1685 if (ia_valid & ATTR_FILE)
1686 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1687
1688 if (dirtied) {
1689 __ceph_mark_dirty_caps(ci, dirtied);
1690 inode->i_ctime = CURRENT_TIME;
1691 }
1692
1693 release &= issued;
1694 spin_unlock(&inode->i_lock);
1695
Sage Weil355da1e2009-10-06 11:31:08 -07001696 if (mask) {
1697 req->r_inode = igrab(inode);
1698 req->r_inode_drop = release;
1699 req->r_args.setattr.mask = cpu_to_le32(mask);
1700 req->r_num_caps = 1;
1701 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1702 }
1703 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1704 ceph_cap_string(dirtied), mask);
1705
1706 ceph_mdsc_put_request(req);
1707 __ceph_do_pending_vmtruncate(inode);
1708 return err;
1709out:
1710 spin_unlock(&inode->i_lock);
1711 ceph_mdsc_put_request(req);
1712 return err;
1713}
1714
1715/*
1716 * Verify that we have a lease on the given mask. If not,
1717 * do a getattr against an mds.
1718 */
1719int ceph_do_getattr(struct inode *inode, int mask)
1720{
1721 struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
1722 struct ceph_mds_client *mdsc = &client->mdsc;
1723 struct ceph_mds_request *req;
1724 int err;
1725
1726 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1727 dout("do_getattr inode %p SNAPDIR\n", inode);
1728 return 0;
1729 }
1730
1731 dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
1732 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1733 return 0;
1734
1735 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1736 if (IS_ERR(req))
1737 return PTR_ERR(req);
1738 req->r_inode = igrab(inode);
1739 req->r_num_caps = 1;
1740 req->r_args.getattr.mask = cpu_to_le32(mask);
1741 err = ceph_mdsc_do_request(mdsc, NULL, req);
1742 ceph_mdsc_put_request(req);
1743 dout("do_getattr result=%d\n", err);
1744 return err;
1745}
1746
1747
1748/*
1749 * Check inode permissions. We verify we have a valid value for
1750 * the AUTH cap, then call the generic handler.
1751 */
1752int ceph_permission(struct inode *inode, int mask)
1753{
1754 int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1755
1756 if (!err)
1757 err = generic_permission(inode, mask, NULL);
1758 return err;
1759}
1760
1761/*
1762 * Get all attributes. Hopefully somedata we'll have a statlite()
1763 * and can limit the fields we require to be accurate.
1764 */
1765int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1766 struct kstat *stat)
1767{
1768 struct inode *inode = dentry->d_inode;
Sage Weil232d4b02009-10-21 11:21:49 -07001769 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001770 int err;
1771
1772 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1773 if (!err) {
1774 generic_fillattr(inode, stat);
1775 stat->ino = inode->i_ino;
1776 if (ceph_snap(inode) != CEPH_NOSNAP)
1777 stat->dev = ceph_snap(inode);
1778 else
1779 stat->dev = 0;
Sage Weil232d4b02009-10-21 11:21:49 -07001780 if (S_ISDIR(inode->i_mode)) {
1781 stat->size = ci->i_rbytes;
1782 stat->blocks = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001783 stat->blksize = 65536;
Sage Weil232d4b02009-10-21 11:21:49 -07001784 }
Sage Weil355da1e2009-10-06 11:31:08 -07001785 }
1786 return err;
1787}