blob: bb68c799074d86775844b57ae7f4858f3b58bc85 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil355da1e2009-10-06 11:31:08 -07002
3#include <linux/module.h>
4#include <linux/fs.h>
Sage Weil355da1e2009-10-06 11:31:08 -07005#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>
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +000012#include <linux/pagevec.h>
Sage Weil355da1e2009-10-06 11:31:08 -070013
14#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include "mds_client.h"
16#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -070017
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 =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700387 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 Weil29625072010-05-27 10:40:43 -0700445 CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
446 CEPH_CAP_FILE_EXCL|
447 CEPH_CAP_FILE_LAZYIO)) ||
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800448 mapping_mapped(inode->i_mapping) ||
449 __ceph_caps_file_wanted(ci)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700450 ci->i_truncate_pending++;
451 queue_trunc = 1;
452 }
453 }
454 }
455 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
456 ci->i_truncate_size != truncate_size) {
457 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
458 truncate_size);
459 ci->i_truncate_size = truncate_size;
460 }
461 return queue_trunc;
462}
463
464void ceph_fill_file_time(struct inode *inode, int issued,
465 u64 time_warp_seq, struct timespec *ctime,
466 struct timespec *mtime, struct timespec *atime)
467{
468 struct ceph_inode_info *ci = ceph_inode(inode);
469 int warn = 0;
470
471 if (issued & (CEPH_CAP_FILE_EXCL|
472 CEPH_CAP_FILE_WR|
Sage Weild8672d62010-11-08 09:24:34 -0800473 CEPH_CAP_FILE_BUFFER|
474 CEPH_CAP_AUTH_EXCL|
475 CEPH_CAP_XATTR_EXCL)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700476 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
477 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
478 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
479 ctime->tv_sec, ctime->tv_nsec);
480 inode->i_ctime = *ctime;
481 }
482 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
483 /* the MDS did a utimes() */
484 dout("mtime %ld.%09ld -> %ld.%09ld "
485 "tw %d -> %d\n",
486 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
487 mtime->tv_sec, mtime->tv_nsec,
488 ci->i_time_warp_seq, (int)time_warp_seq);
489
490 inode->i_mtime = *mtime;
491 inode->i_atime = *atime;
492 ci->i_time_warp_seq = time_warp_seq;
493 } else if (time_warp_seq == ci->i_time_warp_seq) {
494 /* nobody did utimes(); take the max */
495 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
496 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
497 inode->i_mtime.tv_sec,
498 inode->i_mtime.tv_nsec,
499 mtime->tv_sec, mtime->tv_nsec);
500 inode->i_mtime = *mtime;
501 }
502 if (timespec_compare(atime, &inode->i_atime) > 0) {
503 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
504 inode->i_atime.tv_sec,
505 inode->i_atime.tv_nsec,
506 atime->tv_sec, atime->tv_nsec);
507 inode->i_atime = *atime;
508 }
509 } else if (issued & CEPH_CAP_FILE_EXCL) {
510 /* we did a utimes(); ignore mds values */
511 } else {
512 warn = 1;
513 }
514 } else {
Sage Weild8672d62010-11-08 09:24:34 -0800515 /* we have no write|excl caps; whatever the MDS says is true */
Sage Weil355da1e2009-10-06 11:31:08 -0700516 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
517 inode->i_ctime = *ctime;
518 inode->i_mtime = *mtime;
519 inode->i_atime = *atime;
520 ci->i_time_warp_seq = time_warp_seq;
521 } else {
522 warn = 1;
523 }
524 }
525 if (warn) /* time_warp_seq shouldn't go backwards */
526 dout("%p mds time_warp_seq %llu < %u\n",
527 inode, time_warp_seq, ci->i_time_warp_seq);
528}
529
530/*
531 * Populate an inode based on info from mds. May be called on new or
532 * existing inodes.
533 */
534static int fill_inode(struct inode *inode,
535 struct ceph_mds_reply_info_in *iinfo,
536 struct ceph_mds_reply_dirfrag *dirinfo,
537 struct ceph_mds_session *session,
538 unsigned long ttl_from, int cap_fmode,
539 struct ceph_cap_reservation *caps_reservation)
540{
541 struct ceph_mds_reply_inode *info = iinfo->in;
542 struct ceph_inode_info *ci = ceph_inode(inode);
543 int i;
544 int issued, implemented;
545 struct timespec mtime, atime, ctime;
546 u32 nsplits;
547 struct ceph_buffer *xattr_blob = NULL;
548 int err = 0;
549 int queue_trunc = 0;
550
551 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
552 inode, ceph_vinop(inode), le64_to_cpu(info->version),
553 ci->i_version);
554
555 /*
556 * prealloc xattr data, if it looks like we'll need it. only
557 * if len > 4 (meaning there are actually xattrs; the first 4
558 * bytes are the xattr count).
559 */
560 if (iinfo->xattr_len > 4) {
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800561 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700562 if (!xattr_blob)
563 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
564 iinfo->xattr_len);
565 }
566
567 spin_lock(&inode->i_lock);
568
569 /*
570 * provided version will be odd if inode value is projected,
Sage Weil8bd59e02010-11-08 09:23:12 -0800571 * even if stable. skip the update if we have newer stable
572 * info (ours>=theirs, e.g. due to racing mds replies), unless
573 * we are getting projected (unstable) info (in which case the
574 * version is odd, and we want ours>theirs).
575 * us them
576 * 2 2 skip
577 * 3 2 skip
578 * 3 3 update
Sage Weil355da1e2009-10-06 11:31:08 -0700579 */
580 if (le64_to_cpu(info->version) > 0 &&
Sage Weil8bd59e02010-11-08 09:23:12 -0800581 (ci->i_version & ~1) >= le64_to_cpu(info->version))
Sage Weil355da1e2009-10-06 11:31:08 -0700582 goto no_change;
583
584 issued = __ceph_caps_issued(ci, &implemented);
585 issued |= implemented | __ceph_caps_dirty(ci);
586
587 /* update inode */
588 ci->i_version = le64_to_cpu(info->version);
589 inode->i_version++;
590 inode->i_rdev = le32_to_cpu(info->rdev);
591
592 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
593 inode->i_mode = le32_to_cpu(info->mode);
594 inode->i_uid = le32_to_cpu(info->uid);
595 inode->i_gid = le32_to_cpu(info->gid);
596 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
597 inode->i_uid, inode->i_gid);
598 }
599
600 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
601 inode->i_nlink = le32_to_cpu(info->nlink);
602
603 /* be careful with mtime, atime, size */
604 ceph_decode_timespec(&atime, &info->atime);
605 ceph_decode_timespec(&mtime, &info->mtime);
606 ceph_decode_timespec(&ctime, &info->ctime);
607 queue_trunc = ceph_fill_file_size(inode, issued,
608 le32_to_cpu(info->truncate_seq),
609 le64_to_cpu(info->truncate_size),
Sage Weil355da1e2009-10-06 11:31:08 -0700610 le64_to_cpu(info->size));
611 ceph_fill_file_time(inode, issued,
612 le32_to_cpu(info->time_warp_seq),
613 &ctime, &mtime, &atime);
614
Sage Weil912a9b02010-11-07 09:37:25 -0800615 /* only update max_size on auth cap */
616 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
617 ci->i_max_size != le64_to_cpu(info->max_size)) {
618 dout("max_size %lld -> %llu\n", ci->i_max_size,
619 le64_to_cpu(info->max_size));
620 ci->i_max_size = le64_to_cpu(info->max_size);
621 }
622
Sage Weil355da1e2009-10-06 11:31:08 -0700623 ci->i_layout = info->layout;
624 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
625
626 /* xattrs */
627 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
628 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
629 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
630 if (ci->i_xattrs.blob)
631 ceph_buffer_put(ci->i_xattrs.blob);
632 ci->i_xattrs.blob = xattr_blob;
633 if (xattr_blob)
634 memcpy(ci->i_xattrs.blob->vec.iov_base,
635 iinfo->xattr_data, iinfo->xattr_len);
636 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
Sage Weila6424e42010-04-29 09:28:11 -0700637 xattr_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700638 }
639
640 inode->i_mapping->a_ops = &ceph_aops;
641 inode->i_mapping->backing_dev_info =
Cheng Renquan640ef792010-03-26 17:40:33 +0800642 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
Sage Weil355da1e2009-10-06 11:31:08 -0700643
644 switch (inode->i_mode & S_IFMT) {
645 case S_IFIFO:
646 case S_IFBLK:
647 case S_IFCHR:
648 case S_IFSOCK:
649 init_special_inode(inode, inode->i_mode, inode->i_rdev);
650 inode->i_op = &ceph_file_iops;
651 break;
652 case S_IFREG:
653 inode->i_op = &ceph_file_iops;
654 inode->i_fop = &ceph_file_fops;
655 break;
656 case S_IFLNK:
657 inode->i_op = &ceph_symlink_iops;
658 if (!ci->i_symlink) {
659 int symlen = iinfo->symlink_len;
660 char *sym;
661
662 BUG_ON(symlen != inode->i_size);
663 spin_unlock(&inode->i_lock);
664
665 err = -ENOMEM;
666 sym = kmalloc(symlen+1, GFP_NOFS);
667 if (!sym)
668 goto out;
669 memcpy(sym, iinfo->symlink, symlen);
670 sym[symlen] = 0;
671
672 spin_lock(&inode->i_lock);
673 if (!ci->i_symlink)
674 ci->i_symlink = sym;
675 else
676 kfree(sym); /* lost a race */
677 }
678 break;
679 case S_IFDIR:
680 inode->i_op = &ceph_dir_iops;
681 inode->i_fop = &ceph_dir_fops;
682
683 ci->i_files = le64_to_cpu(info->files);
684 ci->i_subdirs = le64_to_cpu(info->subdirs);
685 ci->i_rbytes = le64_to_cpu(info->rbytes);
686 ci->i_rfiles = le64_to_cpu(info->rfiles);
687 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
688 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
689
690 /* set dir completion flag? */
691 if (ci->i_files == 0 && ci->i_subdirs == 0 &&
692 ceph_snap(inode) == CEPH_NOSNAP &&
Sage Weil1b7facc2010-04-16 12:58:02 -0700693 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
Sage Weil12451492010-08-22 21:33:32 -0700694 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
Sage Weil1b7facc2010-04-16 12:58:02 -0700695 (ci->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
Sage Weil355da1e2009-10-06 11:31:08 -0700696 dout(" marking %p complete (empty)\n", inode);
697 ci->i_ceph_flags |= CEPH_I_COMPLETE;
698 ci->i_max_offset = 2;
699 }
700
701 /* it may be better to set st_size in getattr instead? */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700702 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), RBYTES))
Sage Weil355da1e2009-10-06 11:31:08 -0700703 inode->i_size = ci->i_rbytes;
704 break;
705 default:
706 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
707 ceph_vinop(inode), inode->i_mode);
708 }
709
710no_change:
711 spin_unlock(&inode->i_lock);
712
713 /* queue truncate if we saw i_size decrease */
714 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -0800715 ceph_queue_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700716
717 /* populate frag tree */
718 /* FIXME: move me up, if/when version reflects fragtree changes */
719 nsplits = le32_to_cpu(info->fragtree.nsplits);
720 mutex_lock(&ci->i_fragtree_mutex);
721 for (i = 0; i < nsplits; i++) {
722 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
723 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
724
725 if (IS_ERR(frag))
726 continue;
727 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
728 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
729 }
730 mutex_unlock(&ci->i_fragtree_mutex);
731
732 /* were we issued a capability? */
733 if (info->cap.caps) {
734 if (ceph_snap(inode) == CEPH_NOSNAP) {
735 ceph_add_cap(inode, session,
736 le64_to_cpu(info->cap.cap_id),
737 cap_fmode,
738 le32_to_cpu(info->cap.caps),
739 le32_to_cpu(info->cap.wanted),
740 le32_to_cpu(info->cap.seq),
741 le32_to_cpu(info->cap.mseq),
742 le64_to_cpu(info->cap.realm),
743 info->cap.flags,
744 caps_reservation);
745 } else {
746 spin_lock(&inode->i_lock);
747 dout(" %p got snap_caps %s\n", inode,
748 ceph_cap_string(le32_to_cpu(info->cap.caps)));
749 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
750 if (cap_fmode >= 0)
751 __ceph_get_fmode(ci, cap_fmode);
752 spin_unlock(&inode->i_lock);
753 }
Sage Weil04d000e2010-05-07 11:26:34 -0700754 } else if (cap_fmode >= 0) {
755 pr_warning("mds issued no caps on %llx.%llx\n",
756 ceph_vinop(inode));
757 __ceph_get_fmode(ci, cap_fmode);
Sage Weil355da1e2009-10-06 11:31:08 -0700758 }
759
760 /* update delegation info? */
761 if (dirinfo)
762 ceph_fill_dirfrag(inode, dirinfo);
763
764 err = 0;
765
766out:
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800767 if (xattr_blob)
768 ceph_buffer_put(xattr_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700769 return err;
770}
771
772/*
773 * caller should hold session s_mutex.
774 */
775static void update_dentry_lease(struct dentry *dentry,
776 struct ceph_mds_reply_lease *lease,
777 struct ceph_mds_session *session,
778 unsigned long from_time)
779{
780 struct ceph_dentry_info *di = ceph_dentry(dentry);
781 long unsigned duration = le32_to_cpu(lease->duration_ms);
782 long unsigned ttl = from_time + (duration * HZ) / 1000;
783 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
784 struct inode *dir;
785
786 /* only track leases on regular dentries */
787 if (dentry->d_op != &ceph_dentry_ops)
788 return;
789
790 spin_lock(&dentry->d_lock);
791 dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
792 dentry, le16_to_cpu(lease->mask), duration, ttl);
793
794 /* make lease_rdcache_gen match directory */
795 dir = dentry->d_parent->d_inode;
796 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
797
798 if (lease->mask == 0)
799 goto out_unlock;
800
801 if (di->lease_gen == session->s_cap_gen &&
802 time_before(ttl, dentry->d_time))
803 goto out_unlock; /* we already have a newer lease. */
804
805 if (di->lease_session && di->lease_session != session)
806 goto out_unlock;
807
808 ceph_dentry_lru_touch(dentry);
809
810 if (!di->lease_session)
811 di->lease_session = ceph_get_mds_session(session);
812 di->lease_gen = session->s_cap_gen;
813 di->lease_seq = le32_to_cpu(lease->seq);
814 di->lease_renew_after = half_ttl;
815 di->lease_renew_from = 0;
816 dentry->d_time = ttl;
817out_unlock:
818 spin_unlock(&dentry->d_lock);
819 return;
820}
821
822/*
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800823 * Set dentry's directory position based on the current dir's max, and
824 * order it in d_subdirs, so that dcache_readdir behaves.
825 */
826static void ceph_set_dentry_offset(struct dentry *dn)
827{
828 struct dentry *dir = dn->d_parent;
829 struct inode *inode = dn->d_parent->d_inode;
830 struct ceph_dentry_info *di;
831
832 BUG_ON(!inode);
833
834 di = ceph_dentry(dn);
835
836 spin_lock(&inode->i_lock);
Sage Weile8a74982010-04-15 14:08:49 -0700837 if ((ceph_inode(inode)->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
838 spin_unlock(&inode->i_lock);
839 return;
840 }
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800841 di->offset = ceph_inode(inode)->i_max_offset++;
842 spin_unlock(&inode->i_lock);
843
844 spin_lock(&dcache_lock);
845 spin_lock(&dn->d_lock);
Henry C Chang13a42142010-06-01 11:31:08 -0700846 list_move(&dn->d_u.d_child, &dir->d_subdirs);
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800847 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
848 dn->d_u.d_child.prev, dn->d_u.d_child.next);
849 spin_unlock(&dn->d_lock);
850 spin_unlock(&dcache_lock);
851}
852
853/*
Sage Weil1cd39352010-05-03 22:08:02 -0700854 * splice a dentry to an inode.
855 * caller must hold directory i_mutex for this to be safe.
856 *
857 * we will only rehash the resulting dentry if @prehash is
858 * true; @prehash will be set to false (for the benefit of
859 * the caller) if we fail.
860 */
861static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
Sage Weil467c5252010-09-13 11:39:20 -0700862 bool *prehash, bool set_offset)
Sage Weil1cd39352010-05-03 22:08:02 -0700863{
864 struct dentry *realdn;
865
866 BUG_ON(dn->d_inode);
867
868 /* dn must be unhashed */
869 if (!d_unhashed(dn))
870 d_drop(dn);
871 realdn = d_materialise_unique(dn, in);
872 if (IS_ERR(realdn)) {
Sage Weild69ed052010-06-21 10:38:14 -0700873 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
874 PTR_ERR(realdn), dn, in, ceph_vinop(in));
Sage Weil1cd39352010-05-03 22:08:02 -0700875 if (prehash)
876 *prehash = false; /* don't rehash on error */
877 dn = realdn; /* note realdn contains the error */
878 goto out;
879 } else if (realdn) {
880 dout("dn %p (%d) spliced with %p (%d) "
881 "inode %p ino %llx.%llx\n",
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100882 dn, dn->d_count,
883 realdn, realdn->d_count,
Sage Weil1cd39352010-05-03 22:08:02 -0700884 realdn->d_inode, ceph_vinop(realdn->d_inode));
885 dput(dn);
886 dn = realdn;
887 } else {
888 BUG_ON(!ceph_dentry(dn));
889 dout("dn %p attached to %p ino %llx.%llx\n",
890 dn, dn->d_inode, ceph_vinop(dn->d_inode));
891 }
892 if ((!prehash || *prehash) && d_unhashed(dn))
893 d_rehash(dn);
Sage Weil467c5252010-09-13 11:39:20 -0700894 if (set_offset)
895 ceph_set_dentry_offset(dn);
Sage Weil1cd39352010-05-03 22:08:02 -0700896out:
897 return dn;
898}
899
900/*
Sage Weil355da1e2009-10-06 11:31:08 -0700901 * Incorporate results into the local cache. This is either just
902 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
903 * after a lookup).
904 *
905 * A reply may contain
906 * a directory inode along with a dentry.
907 * and/or a target inode
908 *
909 * Called with snap_rwsem (read).
910 */
911int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
912 struct ceph_mds_session *session)
913{
914 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
915 struct inode *in = NULL;
916 struct ceph_mds_reply_inode *ininfo;
917 struct ceph_vino vino;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700918 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700919 int i = 0;
920 int err = 0;
921
922 dout("fill_trace %p is_dentry %d is_target %d\n", req,
923 rinfo->head->is_dentry, rinfo->head->is_target);
924
925#if 0
926 /*
927 * Debugging hook:
928 *
929 * If we resend completed ops to a recovering mds, we get no
930 * trace. Since that is very rare, pretend this is the case
931 * to ensure the 'no trace' handlers in the callers behave.
932 *
933 * Fill in inodes unconditionally to avoid breaking cap
934 * invariants.
935 */
936 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
937 pr_info("fill_trace faking empty trace on %lld %s\n",
938 req->r_tid, ceph_mds_op_name(rinfo->head->op));
939 if (rinfo->head->is_dentry) {
940 rinfo->head->is_dentry = 0;
941 err = fill_inode(req->r_locked_dir,
942 &rinfo->diri, rinfo->dirfrag,
943 session, req->r_request_started, -1);
944 }
945 if (rinfo->head->is_target) {
946 rinfo->head->is_target = 0;
947 ininfo = rinfo->targeti.in;
948 vino.ino = le64_to_cpu(ininfo->ino);
949 vino.snap = le64_to_cpu(ininfo->snapid);
950 in = ceph_get_inode(sb, vino);
951 err = fill_inode(in, &rinfo->targeti, NULL,
952 session, req->r_request_started,
953 req->r_fmode);
954 iput(in);
955 }
956 }
957#endif
958
959 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
960 dout("fill_trace reply is empty!\n");
Sage Weil167c9e32010-05-14 10:02:57 -0700961 if (rinfo->head->result == 0 && req->r_locked_dir)
962 ceph_invalidate_dir_request(req);
Sage Weil355da1e2009-10-06 11:31:08 -0700963 return 0;
964 }
965
966 if (rinfo->head->is_dentry) {
Sage Weil5b1daec2010-01-25 11:33:08 -0800967 struct inode *dir = req->r_locked_dir;
968
969 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
970 session, req->r_request_started, -1,
971 &req->r_caps_reservation);
972 if (err < 0)
973 return err;
974 }
975
Sage Weil9358c6d2010-03-30 13:54:41 -0700976 /*
977 * ignore null lease/binding on snapdir ENOENT, or else we
978 * will have trouble splicing in the virtual snapdir later
979 */
980 if (rinfo->head->is_dentry && !req->r_aborted &&
981 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700982 fsc->mount_options->snapdir_name,
Sage Weil9358c6d2010-03-30 13:54:41 -0700983 req->r_dentry->d_name.len))) {
Sage Weil355da1e2009-10-06 11:31:08 -0700984 /*
985 * lookup link rename : null -> possibly existing inode
986 * mknod symlink mkdir : null -> new inode
987 * unlink : linked -> null
988 */
989 struct inode *dir = req->r_locked_dir;
990 struct dentry *dn = req->r_dentry;
991 bool have_dir_cap, have_lease;
992
993 BUG_ON(!dn);
994 BUG_ON(!dir);
995 BUG_ON(dn->d_parent->d_inode != dir);
996 BUG_ON(ceph_ino(dir) !=
997 le64_to_cpu(rinfo->diri.in->ino));
998 BUG_ON(ceph_snap(dir) !=
999 le64_to_cpu(rinfo->diri.in->snapid));
1000
Sage Weil355da1e2009-10-06 11:31:08 -07001001 /* do we have a lease on the whole dir? */
1002 have_dir_cap =
1003 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1004 CEPH_CAP_FILE_SHARED);
1005
1006 /* do we have a dn lease? */
1007 have_lease = have_dir_cap ||
1008 (le16_to_cpu(rinfo->dlease->mask) &
1009 CEPH_LOCK_DN);
1010
1011 if (!have_lease)
1012 dout("fill_trace no dentry lease or dir cap\n");
1013
1014 /* rename? */
1015 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1016 dout(" src %p '%.*s' dst %p '%.*s'\n",
1017 req->r_old_dentry,
1018 req->r_old_dentry->d_name.len,
1019 req->r_old_dentry->d_name.name,
1020 dn, dn->d_name.len, dn->d_name.name);
1021 dout("fill_trace doing d_move %p -> %p\n",
1022 req->r_old_dentry, dn);
Sage Weilc10f5e12010-04-16 12:56:11 -07001023
1024 /* d_move screws up d_subdirs order */
1025 ceph_i_clear(dir, CEPH_I_COMPLETE);
1026
Sage Weil355da1e2009-10-06 11:31:08 -07001027 d_move(req->r_old_dentry, dn);
1028 dout(" src %p '%.*s' dst %p '%.*s'\n",
1029 req->r_old_dentry,
1030 req->r_old_dentry->d_name.len,
1031 req->r_old_dentry->d_name.name,
1032 dn, dn->d_name.len, dn->d_name.name);
Sage Weil81a6cf22010-05-14 09:35:38 -07001033
Sage Weilc4a29f22009-12-21 11:42:18 -08001034 /* ensure target dentry is invalidated, despite
1035 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001036 ceph_invalidate_dentry_lease(dn);
1037
Sage Weil355da1e2009-10-06 11:31:08 -07001038 /* take overwritten dentry's readdir offset */
Sage Weil1cd39352010-05-03 22:08:02 -07001039 dout("dn %p gets %p offset %lld (old offset %lld)\n",
1040 req->r_old_dentry, dn, ceph_dentry(dn)->offset,
1041 ceph_dentry(req->r_old_dentry)->offset);
Sage Weil355da1e2009-10-06 11:31:08 -07001042 ceph_dentry(req->r_old_dentry)->offset =
1043 ceph_dentry(dn)->offset;
Sage Weil81a6cf22010-05-14 09:35:38 -07001044
Sage Weil355da1e2009-10-06 11:31:08 -07001045 dn = req->r_old_dentry; /* use old_dentry */
1046 in = dn->d_inode;
1047 }
1048
1049 /* null dentry? */
1050 if (!rinfo->head->is_target) {
1051 dout("fill_trace null dentry\n");
1052 if (dn->d_inode) {
1053 dout("d_delete %p\n", dn);
1054 d_delete(dn);
1055 } else {
1056 dout("d_instantiate %p NULL\n", dn);
1057 d_instantiate(dn, NULL);
1058 if (have_lease && d_unhashed(dn))
1059 d_rehash(dn);
1060 update_dentry_lease(dn, rinfo->dlease,
1061 session,
1062 req->r_request_started);
1063 }
1064 goto done;
1065 }
1066
1067 /* attach proper inode */
1068 ininfo = rinfo->targeti.in;
1069 vino.ino = le64_to_cpu(ininfo->ino);
1070 vino.snap = le64_to_cpu(ininfo->snapid);
Sage Weild8b16b32010-11-06 12:41:16 -07001071 in = dn->d_inode;
1072 if (!in) {
Sage Weil355da1e2009-10-06 11:31:08 -07001073 in = ceph_get_inode(sb, vino);
1074 if (IS_ERR(in)) {
1075 pr_err("fill_trace bad get_inode "
1076 "%llx.%llx\n", vino.ino, vino.snap);
1077 err = PTR_ERR(in);
1078 d_delete(dn);
1079 goto done;
1080 }
Sage Weil467c5252010-09-13 11:39:20 -07001081 dn = splice_dentry(dn, in, &have_lease, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001082 if (IS_ERR(dn)) {
1083 err = PTR_ERR(dn);
1084 goto done;
1085 }
1086 req->r_dentry = dn; /* may have spliced */
1087 igrab(in);
1088 } else if (ceph_ino(in) == vino.ino &&
1089 ceph_snap(in) == vino.snap) {
1090 igrab(in);
1091 } else {
1092 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1093 dn, in, ceph_ino(in), ceph_snap(in),
1094 vino.ino, vino.snap);
1095 have_lease = false;
1096 in = NULL;
1097 }
1098
1099 if (have_lease)
1100 update_dentry_lease(dn, rinfo->dlease, session,
1101 req->r_request_started);
1102 dout(" final dn %p\n", dn);
1103 i++;
1104 } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1105 req->r_op == CEPH_MDS_OP_MKSNAP) {
1106 struct dentry *dn = req->r_dentry;
1107
1108 /* fill out a snapdir LOOKUPSNAP dentry */
1109 BUG_ON(!dn);
1110 BUG_ON(!req->r_locked_dir);
1111 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1112 ininfo = rinfo->targeti.in;
1113 vino.ino = le64_to_cpu(ininfo->ino);
1114 vino.snap = le64_to_cpu(ininfo->snapid);
1115 in = ceph_get_inode(sb, vino);
1116 if (IS_ERR(in)) {
1117 pr_err("fill_inode get_inode badness %llx.%llx\n",
1118 vino.ino, vino.snap);
1119 err = PTR_ERR(in);
1120 d_delete(dn);
1121 goto done;
1122 }
1123 dout(" linking snapped dir %p to dn %p\n", in, dn);
Sage Weil467c5252010-09-13 11:39:20 -07001124 dn = splice_dentry(dn, in, NULL, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001125 if (IS_ERR(dn)) {
1126 err = PTR_ERR(dn);
1127 goto done;
1128 }
1129 req->r_dentry = dn; /* may have spliced */
1130 igrab(in);
1131 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1132 }
1133
1134 if (rinfo->head->is_target) {
1135 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1136 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1137
1138 if (in == NULL || ceph_ino(in) != vino.ino ||
1139 ceph_snap(in) != vino.snap) {
1140 in = ceph_get_inode(sb, vino);
1141 if (IS_ERR(in)) {
1142 err = PTR_ERR(in);
1143 goto done;
1144 }
1145 }
1146 req->r_target_inode = in;
1147
1148 err = fill_inode(in,
1149 &rinfo->targeti, NULL,
1150 session, req->r_request_started,
1151 (le32_to_cpu(rinfo->head->result) == 0) ?
1152 req->r_fmode : -1,
1153 &req->r_caps_reservation);
1154 if (err < 0) {
1155 pr_err("fill_inode badness %p %llx.%llx\n",
1156 in, ceph_vinop(in));
1157 goto done;
1158 }
1159 }
1160
1161done:
1162 dout("fill_trace done err=%d\n", err);
1163 return err;
1164}
1165
1166/*
1167 * Prepopulate our cache with readdir results, leases, etc.
1168 */
1169int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1170 struct ceph_mds_session *session)
1171{
1172 struct dentry *parent = req->r_dentry;
1173 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1174 struct qstr dname;
1175 struct dentry *dn;
1176 struct inode *in;
1177 int err = 0, i;
1178 struct inode *snapdir = NULL;
1179 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1180 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1181 struct ceph_dentry_info *di;
1182
1183 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1184 snapdir = ceph_get_snapdir(parent->d_inode);
1185 parent = d_find_alias(snapdir);
1186 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1187 rinfo->dir_nr, parent);
1188 } else {
1189 dout("readdir_prepopulate %d items under dn %p\n",
1190 rinfo->dir_nr, parent);
1191 if (rinfo->dir_dir)
1192 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1193 }
1194
1195 for (i = 0; i < rinfo->dir_nr; i++) {
1196 struct ceph_vino vino;
1197
1198 dname.name = rinfo->dir_dname[i];
1199 dname.len = rinfo->dir_dname_len[i];
1200 dname.hash = full_name_hash(dname.name, dname.len);
1201
1202 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1203 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1204
1205retry_lookup:
1206 dn = d_lookup(parent, &dname);
1207 dout("d_lookup on parent=%p name=%.*s got %p\n",
1208 parent, dname.len, dname.name, dn);
1209
1210 if (!dn) {
1211 dn = d_alloc(parent, &dname);
1212 dout("d_alloc %p '%.*s' = %p\n", parent,
1213 dname.len, dname.name, dn);
1214 if (dn == NULL) {
1215 dout("d_alloc badness\n");
1216 err = -ENOMEM;
1217 goto out;
1218 }
1219 err = ceph_init_dentry(dn);
Sage Weil8c696732010-07-22 14:11:56 -07001220 if (err < 0) {
1221 dput(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001222 goto out;
Sage Weil8c696732010-07-22 14:11:56 -07001223 }
Sage Weil355da1e2009-10-06 11:31:08 -07001224 } else if (dn->d_inode &&
1225 (ceph_ino(dn->d_inode) != vino.ino ||
1226 ceph_snap(dn->d_inode) != vino.snap)) {
1227 dout(" dn %p points to wrong inode %p\n",
1228 dn, dn->d_inode);
1229 d_delete(dn);
1230 dput(dn);
1231 goto retry_lookup;
1232 } else {
1233 /* reorder parent's d_subdirs */
1234 spin_lock(&dcache_lock);
1235 spin_lock(&dn->d_lock);
1236 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1237 spin_unlock(&dn->d_lock);
1238 spin_unlock(&dcache_lock);
1239 }
1240
1241 di = dn->d_fsdata;
1242 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1243
1244 /* inode */
1245 if (dn->d_inode) {
1246 in = dn->d_inode;
1247 } else {
1248 in = ceph_get_inode(parent->d_sb, vino);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001249 if (IS_ERR(in)) {
Sage Weil355da1e2009-10-06 11:31:08 -07001250 dout("new_inode badness\n");
1251 d_delete(dn);
1252 dput(dn);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001253 err = PTR_ERR(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001254 goto out;
1255 }
Sage Weil467c5252010-09-13 11:39:20 -07001256 dn = splice_dentry(dn, in, NULL, false);
Sage Weild69ed052010-06-21 10:38:14 -07001257 if (IS_ERR(dn))
1258 dn = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001259 }
1260
1261 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1262 req->r_request_started, -1,
1263 &req->r_caps_reservation) < 0) {
1264 pr_err("fill_inode badness on %p\n", in);
Sage Weild69ed052010-06-21 10:38:14 -07001265 goto next_item;
Sage Weil355da1e2009-10-06 11:31:08 -07001266 }
Sage Weild69ed052010-06-21 10:38:14 -07001267 if (dn)
1268 update_dentry_lease(dn, rinfo->dir_dlease[i],
1269 req->r_session,
1270 req->r_request_started);
1271next_item:
1272 if (dn)
1273 dput(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001274 }
1275 req->r_did_prepopulate = true;
1276
1277out:
1278 if (snapdir) {
1279 iput(snapdir);
1280 dput(parent);
1281 }
1282 dout("readdir_prepopulate done\n");
1283 return err;
1284}
1285
1286int ceph_inode_set_size(struct inode *inode, loff_t size)
1287{
1288 struct ceph_inode_info *ci = ceph_inode(inode);
1289 int ret = 0;
1290
1291 spin_lock(&inode->i_lock);
1292 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1293 inode->i_size = size;
1294 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1295
1296 /* tell the MDS if we are approaching max_size */
1297 if ((size << 1) >= ci->i_max_size &&
1298 (ci->i_reported_size << 1) < ci->i_max_size)
1299 ret = 1;
1300
1301 spin_unlock(&inode->i_lock);
1302 return ret;
1303}
1304
1305/*
1306 * Write back inode data in a worker thread. (This can't be done
1307 * in the message handler context.)
1308 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001309void ceph_queue_writeback(struct inode *inode)
1310{
1311 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1312 &ceph_inode(inode)->i_wb_work)) {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001313 dout("ceph_queue_writeback %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001314 igrab(inode);
1315 } else {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001316 dout("ceph_queue_writeback %p failed\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001317 }
1318}
1319
1320static void ceph_writeback_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001321{
1322 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1323 i_wb_work);
1324 struct inode *inode = &ci->vfs_inode;
1325
1326 dout("writeback %p\n", inode);
1327 filemap_fdatawrite(&inode->i_data);
1328 iput(inode);
1329}
1330
1331/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001332 * queue an async invalidation
1333 */
1334void ceph_queue_invalidate(struct inode *inode)
1335{
1336 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1337 &ceph_inode(inode)->i_pg_inv_work)) {
1338 dout("ceph_queue_invalidate %p\n", inode);
1339 igrab(inode);
1340 } else {
1341 dout("ceph_queue_invalidate %p failed\n", inode);
1342 }
1343}
1344
1345/*
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +00001346 * invalidate any pages that are not dirty or under writeback. this
1347 * includes pages that are clean and mapped.
1348 */
1349static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
1350{
1351 struct pagevec pvec;
1352 pgoff_t next = 0;
1353 int i;
1354
1355 pagevec_init(&pvec, 0);
1356 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
1357 for (i = 0; i < pagevec_count(&pvec); i++) {
1358 struct page *page = pvec.pages[i];
1359 pgoff_t index;
1360 int skip_page =
1361 (PageDirty(page) || PageWriteback(page));
1362
1363 if (!skip_page)
1364 skip_page = !trylock_page(page);
1365
1366 /*
1367 * We really shouldn't be looking at the ->index of an
1368 * unlocked page. But we're not allowed to lock these
1369 * pages. So we rely upon nobody altering the ->index
1370 * of this (pinned-by-us) page.
1371 */
1372 index = page->index;
1373 if (index > next)
1374 next = index;
1375 next++;
1376
1377 if (skip_page)
1378 continue;
1379
1380 generic_error_remove_page(mapping, page);
1381 unlock_page(page);
1382 }
1383 pagevec_release(&pvec);
1384 cond_resched();
1385 }
1386}
1387
1388/*
Sage Weil355da1e2009-10-06 11:31:08 -07001389 * Invalidate inode pages in a worker thread. (This can't be done
1390 * in the message handler context.)
1391 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001392static void ceph_invalidate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001393{
1394 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1395 i_pg_inv_work);
1396 struct inode *inode = &ci->vfs_inode;
1397 u32 orig_gen;
1398 int check = 0;
1399
1400 spin_lock(&inode->i_lock);
1401 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1402 ci->i_rdcache_gen, ci->i_rdcache_revoking);
Sage Weilcd045cb2010-11-04 11:05:05 -07001403 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Sage Weil355da1e2009-10-06 11:31:08 -07001404 /* nevermind! */
Sage Weil355da1e2009-10-06 11:31:08 -07001405 spin_unlock(&inode->i_lock);
1406 goto out;
1407 }
1408 orig_gen = ci->i_rdcache_gen;
1409 spin_unlock(&inode->i_lock);
1410
Yehuda Sadehc9af9fb2010-02-19 00:10:11 +00001411 ceph_invalidate_nondirty_pages(inode->i_mapping);
Sage Weil355da1e2009-10-06 11:31:08 -07001412
1413 spin_lock(&inode->i_lock);
Sage Weilcd045cb2010-11-04 11:05:05 -07001414 if (orig_gen == ci->i_rdcache_gen &&
1415 orig_gen == ci->i_rdcache_revoking) {
Sage Weil355da1e2009-10-06 11:31:08 -07001416 dout("invalidate_pages %p gen %d successful\n", inode,
1417 ci->i_rdcache_gen);
Sage Weilcd045cb2010-11-04 11:05:05 -07001418 ci->i_rdcache_revoking--;
Sage Weil355da1e2009-10-06 11:31:08 -07001419 check = 1;
1420 } else {
Sage Weilcd045cb2010-11-04 11:05:05 -07001421 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1422 inode, orig_gen, ci->i_rdcache_gen,
1423 ci->i_rdcache_revoking);
Sage Weil355da1e2009-10-06 11:31:08 -07001424 }
1425 spin_unlock(&inode->i_lock);
1426
1427 if (check)
1428 ceph_check_caps(ci, 0, NULL);
1429out:
1430 iput(inode);
1431}
1432
1433
1434/*
1435 * called by trunc_wq; take i_mutex ourselves
1436 *
1437 * We also truncate in a separate thread as well.
1438 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001439static void ceph_vmtruncate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001440{
1441 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1442 i_vmtruncate_work);
1443 struct inode *inode = &ci->vfs_inode;
1444
1445 dout("vmtruncate_work %p\n", inode);
1446 mutex_lock(&inode->i_mutex);
1447 __ceph_do_pending_vmtruncate(inode);
1448 mutex_unlock(&inode->i_mutex);
1449 iput(inode);
1450}
1451
1452/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001453 * Queue an async vmtruncate. If we fail to queue work, we will handle
1454 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1455 */
1456void ceph_queue_vmtruncate(struct inode *inode)
1457{
1458 struct ceph_inode_info *ci = ceph_inode(inode);
1459
Cheng Renquan640ef792010-03-26 17:40:33 +08001460 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
Sage Weil3c6f6b72010-02-09 15:24:44 -08001461 &ci->i_vmtruncate_work)) {
1462 dout("ceph_queue_vmtruncate %p\n", inode);
1463 igrab(inode);
1464 } else {
1465 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1466 inode, ci->i_truncate_pending);
1467 }
1468}
1469
1470/*
Sage Weil355da1e2009-10-06 11:31:08 -07001471 * called with i_mutex held.
1472 *
1473 * Make sure any pending truncation is applied before doing anything
1474 * that may depend on it.
1475 */
1476void __ceph_do_pending_vmtruncate(struct inode *inode)
1477{
1478 struct ceph_inode_info *ci = ceph_inode(inode);
1479 u64 to;
1480 int wrbuffer_refs, wake = 0;
1481
1482retry:
1483 spin_lock(&inode->i_lock);
1484 if (ci->i_truncate_pending == 0) {
1485 dout("__do_pending_vmtruncate %p none pending\n", inode);
1486 spin_unlock(&inode->i_lock);
1487 return;
1488 }
1489
1490 /*
1491 * make sure any dirty snapped pages are flushed before we
1492 * possibly truncate them.. so write AND block!
1493 */
1494 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1495 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1496 inode);
1497 spin_unlock(&inode->i_lock);
1498 filemap_write_and_wait_range(&inode->i_data, 0,
1499 inode->i_sb->s_maxbytes);
1500 goto retry;
1501 }
1502
1503 to = ci->i_truncate_size;
1504 wrbuffer_refs = ci->i_wrbuffer_ref;
1505 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1506 ci->i_truncate_pending, to);
1507 spin_unlock(&inode->i_lock);
1508
1509 truncate_inode_pages(inode->i_mapping, to);
1510
1511 spin_lock(&inode->i_lock);
1512 ci->i_truncate_pending--;
1513 if (ci->i_truncate_pending == 0)
1514 wake = 1;
1515 spin_unlock(&inode->i_lock);
1516
1517 if (wrbuffer_refs == 0)
1518 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
1519 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001520 wake_up_all(&ci->i_cap_wq);
Sage Weil355da1e2009-10-06 11:31:08 -07001521}
1522
1523
1524/*
1525 * symlinks
1526 */
1527static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1528{
1529 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1530 nd_set_link(nd, ci->i_symlink);
1531 return NULL;
1532}
1533
1534static const struct inode_operations ceph_symlink_iops = {
1535 .readlink = generic_readlink,
1536 .follow_link = ceph_sym_follow_link,
1537};
1538
1539/*
1540 * setattr
1541 */
1542int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1543{
1544 struct inode *inode = dentry->d_inode;
1545 struct ceph_inode_info *ci = ceph_inode(inode);
1546 struct inode *parent_inode = dentry->d_parent->d_inode;
1547 const unsigned int ia_valid = attr->ia_valid;
1548 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001549 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001550 int issued;
1551 int release = 0, dirtied = 0;
1552 int mask = 0;
1553 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001554
1555 if (ceph_snap(inode) != CEPH_NOSNAP)
1556 return -EROFS;
1557
1558 __ceph_do_pending_vmtruncate(inode);
1559
1560 err = inode_change_ok(inode, attr);
1561 if (err != 0)
1562 return err;
1563
1564 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1565 USE_AUTH_MDS);
1566 if (IS_ERR(req))
1567 return PTR_ERR(req);
1568
1569 spin_lock(&inode->i_lock);
1570 issued = __ceph_caps_issued(ci, NULL);
1571 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1572
1573 if (ia_valid & ATTR_UID) {
1574 dout("setattr %p uid %d -> %d\n", inode,
1575 inode->i_uid, attr->ia_uid);
1576 if (issued & CEPH_CAP_AUTH_EXCL) {
1577 inode->i_uid = attr->ia_uid;
1578 dirtied |= CEPH_CAP_AUTH_EXCL;
1579 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1580 attr->ia_uid != inode->i_uid) {
1581 req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
1582 mask |= CEPH_SETATTR_UID;
1583 release |= CEPH_CAP_AUTH_SHARED;
1584 }
1585 }
1586 if (ia_valid & ATTR_GID) {
1587 dout("setattr %p gid %d -> %d\n", inode,
1588 inode->i_gid, attr->ia_gid);
1589 if (issued & CEPH_CAP_AUTH_EXCL) {
1590 inode->i_gid = attr->ia_gid;
1591 dirtied |= CEPH_CAP_AUTH_EXCL;
1592 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1593 attr->ia_gid != inode->i_gid) {
1594 req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
1595 mask |= CEPH_SETATTR_GID;
1596 release |= CEPH_CAP_AUTH_SHARED;
1597 }
1598 }
1599 if (ia_valid & ATTR_MODE) {
1600 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1601 attr->ia_mode);
1602 if (issued & CEPH_CAP_AUTH_EXCL) {
1603 inode->i_mode = attr->ia_mode;
1604 dirtied |= CEPH_CAP_AUTH_EXCL;
1605 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1606 attr->ia_mode != inode->i_mode) {
1607 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1608 mask |= CEPH_SETATTR_MODE;
1609 release |= CEPH_CAP_AUTH_SHARED;
1610 }
1611 }
1612
1613 if (ia_valid & ATTR_ATIME) {
1614 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1615 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1616 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1617 if (issued & CEPH_CAP_FILE_EXCL) {
1618 ci->i_time_warp_seq++;
1619 inode->i_atime = attr->ia_atime;
1620 dirtied |= CEPH_CAP_FILE_EXCL;
1621 } else if ((issued & CEPH_CAP_FILE_WR) &&
1622 timespec_compare(&inode->i_atime,
1623 &attr->ia_atime) < 0) {
1624 inode->i_atime = attr->ia_atime;
1625 dirtied |= CEPH_CAP_FILE_WR;
1626 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1627 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1628 ceph_encode_timespec(&req->r_args.setattr.atime,
1629 &attr->ia_atime);
1630 mask |= CEPH_SETATTR_ATIME;
1631 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1632 CEPH_CAP_FILE_WR;
1633 }
1634 }
1635 if (ia_valid & ATTR_MTIME) {
1636 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1637 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1638 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1639 if (issued & CEPH_CAP_FILE_EXCL) {
1640 ci->i_time_warp_seq++;
1641 inode->i_mtime = attr->ia_mtime;
1642 dirtied |= CEPH_CAP_FILE_EXCL;
1643 } else if ((issued & CEPH_CAP_FILE_WR) &&
1644 timespec_compare(&inode->i_mtime,
1645 &attr->ia_mtime) < 0) {
1646 inode->i_mtime = attr->ia_mtime;
1647 dirtied |= CEPH_CAP_FILE_WR;
1648 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1649 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1650 ceph_encode_timespec(&req->r_args.setattr.mtime,
1651 &attr->ia_mtime);
1652 mask |= CEPH_SETATTR_MTIME;
1653 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1654 CEPH_CAP_FILE_WR;
1655 }
1656 }
1657 if (ia_valid & ATTR_SIZE) {
1658 dout("setattr %p size %lld -> %lld\n", inode,
1659 inode->i_size, attr->ia_size);
1660 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1661 err = -EINVAL;
1662 goto out;
1663 }
1664 if ((issued & CEPH_CAP_FILE_EXCL) &&
1665 attr->ia_size > inode->i_size) {
1666 inode->i_size = attr->ia_size;
Sage Weil355da1e2009-10-06 11:31:08 -07001667 inode->i_blocks =
1668 (attr->ia_size + (1 << 9) - 1) >> 9;
1669 inode->i_ctime = attr->ia_ctime;
1670 ci->i_reported_size = attr->ia_size;
1671 dirtied |= CEPH_CAP_FILE_EXCL;
1672 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1673 attr->ia_size != inode->i_size) {
1674 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1675 req->r_args.setattr.old_size =
1676 cpu_to_le64(inode->i_size);
1677 mask |= CEPH_SETATTR_SIZE;
1678 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1679 CEPH_CAP_FILE_WR;
1680 }
1681 }
1682
1683 /* these do nothing */
1684 if (ia_valid & ATTR_CTIME) {
1685 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1686 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1687 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1688 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1689 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1690 only ? "ctime only" : "ignored");
1691 inode->i_ctime = attr->ia_ctime;
1692 if (only) {
1693 /*
1694 * if kernel wants to dirty ctime but nothing else,
1695 * we need to choose a cap to dirty under, or do
1696 * a almost-no-op setattr
1697 */
1698 if (issued & CEPH_CAP_AUTH_EXCL)
1699 dirtied |= CEPH_CAP_AUTH_EXCL;
1700 else if (issued & CEPH_CAP_FILE_EXCL)
1701 dirtied |= CEPH_CAP_FILE_EXCL;
1702 else if (issued & CEPH_CAP_XATTR_EXCL)
1703 dirtied |= CEPH_CAP_XATTR_EXCL;
1704 else
1705 mask |= CEPH_SETATTR_CTIME;
1706 }
1707 }
1708 if (ia_valid & ATTR_FILE)
1709 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1710
1711 if (dirtied) {
1712 __ceph_mark_dirty_caps(ci, dirtied);
1713 inode->i_ctime = CURRENT_TIME;
1714 }
1715
1716 release &= issued;
1717 spin_unlock(&inode->i_lock);
1718
Sage Weil355da1e2009-10-06 11:31:08 -07001719 if (mask) {
1720 req->r_inode = igrab(inode);
1721 req->r_inode_drop = release;
1722 req->r_args.setattr.mask = cpu_to_le32(mask);
1723 req->r_num_caps = 1;
1724 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
1725 }
1726 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1727 ceph_cap_string(dirtied), mask);
1728
1729 ceph_mdsc_put_request(req);
1730 __ceph_do_pending_vmtruncate(inode);
1731 return err;
1732out:
1733 spin_unlock(&inode->i_lock);
1734 ceph_mdsc_put_request(req);
1735 return err;
1736}
1737
1738/*
1739 * Verify that we have a lease on the given mask. If not,
1740 * do a getattr against an mds.
1741 */
1742int ceph_do_getattr(struct inode *inode, int mask)
1743{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001744 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1745 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001746 struct ceph_mds_request *req;
1747 int err;
1748
1749 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1750 dout("do_getattr inode %p SNAPDIR\n", inode);
1751 return 0;
1752 }
1753
Sage Weilb7495fc2010-11-09 12:43:12 -08001754 dout("do_getattr inode %p mask %s mode 0%o\n", inode, ceph_cap_string(mask), inode->i_mode);
Sage Weil355da1e2009-10-06 11:31:08 -07001755 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1756 return 0;
1757
1758 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1759 if (IS_ERR(req))
1760 return PTR_ERR(req);
1761 req->r_inode = igrab(inode);
1762 req->r_num_caps = 1;
1763 req->r_args.getattr.mask = cpu_to_le32(mask);
1764 err = ceph_mdsc_do_request(mdsc, NULL, req);
1765 ceph_mdsc_put_request(req);
1766 dout("do_getattr result=%d\n", err);
1767 return err;
1768}
1769
1770
1771/*
1772 * Check inode permissions. We verify we have a valid value for
1773 * the AUTH cap, then call the generic handler.
1774 */
1775int ceph_permission(struct inode *inode, int mask)
1776{
1777 int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
1778
1779 if (!err)
1780 err = generic_permission(inode, mask, NULL);
1781 return err;
1782}
1783
1784/*
1785 * Get all attributes. Hopefully somedata we'll have a statlite()
1786 * and can limit the fields we require to be accurate.
1787 */
1788int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1789 struct kstat *stat)
1790{
1791 struct inode *inode = dentry->d_inode;
Sage Weil232d4b02009-10-21 11:21:49 -07001792 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001793 int err;
1794
1795 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1796 if (!err) {
1797 generic_fillattr(inode, stat);
1798 stat->ino = inode->i_ino;
1799 if (ceph_snap(inode) != CEPH_NOSNAP)
1800 stat->dev = ceph_snap(inode);
1801 else
1802 stat->dev = 0;
Sage Weil232d4b02009-10-21 11:21:49 -07001803 if (S_ISDIR(inode->i_mode)) {
1804 stat->size = ci->i_rbytes;
1805 stat->blocks = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001806 stat->blksize = 65536;
Sage Weil232d4b02009-10-21 11:21:49 -07001807 }
Sage Weil355da1e2009-10-06 11:31:08 -07001808 }
1809 return err;
1810}