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