blob: eae41cd732760543f458831a27c3b3a0c1d50a32 [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>
12
13#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040015#include "cache.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070016#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 */
Yehuda Sadehad1fee92011-01-21 16:44:03 -080039static int ceph_set_ino_cb(struct inode *inode, void *data)
40{
41 ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
42 inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
43 return 0;
44}
45
Sage Weil355da1e2009-10-06 11:31:08 -070046struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
47{
48 struct inode *inode;
49 ino_t t = ceph_vino_to_ino(vino);
50
51 inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
52 if (inode == NULL)
53 return ERR_PTR(-ENOMEM);
54 if (inode->i_state & I_NEW) {
55 dout("get_inode created new inode %p %llx.%llx ino %llx\n",
56 inode, ceph_vinop(inode), (u64)inode->i_ino);
57 unlock_new_inode(inode);
58 }
59
60 dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
61 vino.snap, inode);
62 return inode;
63}
64
Yan, Zheng6f60f882013-07-24 12:22:11 +080065struct inode *ceph_lookup_inode(struct super_block *sb, struct ceph_vino vino)
66{
67 struct inode *inode;
68 ino_t t = ceph_vino_to_ino(vino);
69 inode = ilookup5_nowait(sb, t, ceph_ino_compare, &vino);
70 return inode;
71}
72
Sage Weil355da1e2009-10-06 11:31:08 -070073/*
74 * get/constuct snapdir inode for a given directory
75 */
76struct inode *ceph_get_snapdir(struct inode *parent)
77{
78 struct ceph_vino vino = {
79 .ino = ceph_ino(parent),
80 .snap = CEPH_SNAPDIR,
81 };
82 struct inode *inode = ceph_get_inode(parent->i_sb, vino);
Sage Weilb377ff12009-11-11 15:22:37 -080083 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -070084
85 BUG_ON(!S_ISDIR(parent->i_mode));
86 if (IS_ERR(inode))
Julia Lawall7e34bc52010-05-22 12:01:14 +020087 return inode;
Sage Weil355da1e2009-10-06 11:31:08 -070088 inode->i_mode = parent->i_mode;
89 inode->i_uid = parent->i_uid;
90 inode->i_gid = parent->i_gid;
91 inode->i_op = &ceph_dir_iops;
92 inode->i_fop = &ceph_dir_fops;
Sage Weilb377ff12009-11-11 15:22:37 -080093 ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
94 ci->i_rbytes = 0;
Sage Weil355da1e2009-10-06 11:31:08 -070095 return inode;
96}
97
98const struct inode_operations ceph_file_iops = {
99 .permission = ceph_permission,
100 .setattr = ceph_setattr,
101 .getattr = ceph_getattr,
102 .setxattr = ceph_setxattr,
103 .getxattr = ceph_getxattr,
104 .listxattr = ceph_listxattr,
105 .removexattr = ceph_removexattr,
106};
107
108
109/*
110 * We use a 'frag tree' to keep track of the MDS's directory fragments
111 * for a given inode (usually there is just a single fragment). We
112 * need to know when a child frag is delegated to a new MDS, or when
113 * it is flagged as replicated, so we can direct our requests
114 * accordingly.
115 */
116
117/*
118 * find/create a frag in the tree
119 */
120static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
121 u32 f)
122{
123 struct rb_node **p;
124 struct rb_node *parent = NULL;
125 struct ceph_inode_frag *frag;
126 int c;
127
128 p = &ci->i_fragtree.rb_node;
129 while (*p) {
130 parent = *p;
131 frag = rb_entry(parent, struct ceph_inode_frag, node);
132 c = ceph_frag_compare(f, frag->frag);
133 if (c < 0)
134 p = &(*p)->rb_left;
135 else if (c > 0)
136 p = &(*p)->rb_right;
137 else
138 return frag;
139 }
140
141 frag = kmalloc(sizeof(*frag), GFP_NOFS);
142 if (!frag) {
143 pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
144 "frag %x\n", &ci->vfs_inode,
145 ceph_vinop(&ci->vfs_inode), f);
146 return ERR_PTR(-ENOMEM);
147 }
148 frag->frag = f;
149 frag->split_by = 0;
150 frag->mds = -1;
151 frag->ndist = 0;
152
153 rb_link_node(&frag->node, parent, p);
154 rb_insert_color(&frag->node, &ci->i_fragtree);
155
156 dout("get_or_create_frag added %llx.%llx frag %x\n",
157 ceph_vinop(&ci->vfs_inode), f);
158 return frag;
159}
160
161/*
162 * find a specific frag @f
163 */
164struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
165{
166 struct rb_node *n = ci->i_fragtree.rb_node;
167
168 while (n) {
169 struct ceph_inode_frag *frag =
170 rb_entry(n, struct ceph_inode_frag, node);
171 int c = ceph_frag_compare(f, frag->frag);
172 if (c < 0)
173 n = n->rb_left;
174 else if (c > 0)
175 n = n->rb_right;
176 else
177 return frag;
178 }
179 return NULL;
180}
181
182/*
183 * Choose frag containing the given value @v. If @pfrag is
184 * specified, copy the frag delegation info to the caller if
185 * it is present.
186 */
187u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
188 struct ceph_inode_frag *pfrag,
189 int *found)
190{
191 u32 t = ceph_frag_make(0, 0);
192 struct ceph_inode_frag *frag;
193 unsigned nway, i;
194 u32 n;
195
196 if (found)
197 *found = 0;
198
199 mutex_lock(&ci->i_fragtree_mutex);
200 while (1) {
201 WARN_ON(!ceph_frag_contains_value(t, v));
202 frag = __ceph_find_frag(ci, t);
203 if (!frag)
204 break; /* t is a leaf */
205 if (frag->split_by == 0) {
206 if (pfrag)
207 memcpy(pfrag, frag, sizeof(*pfrag));
208 if (found)
209 *found = 1;
210 break;
211 }
212
213 /* choose child */
214 nway = 1 << frag->split_by;
215 dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
216 frag->split_by, nway);
217 for (i = 0; i < nway; i++) {
218 n = ceph_frag_make_child(t, frag->split_by, i);
219 if (ceph_frag_contains_value(n, v)) {
220 t = n;
221 break;
222 }
223 }
224 BUG_ON(i == nway);
225 }
226 dout("choose_frag(%x) = %x\n", v, t);
227
228 mutex_unlock(&ci->i_fragtree_mutex);
229 return t;
230}
231
232/*
233 * Process dirfrag (delegation) info from the mds. Include leaf
234 * fragment in tree ONLY if ndist > 0. Otherwise, only
235 * branches/splits are included in i_fragtree)
236 */
237static int ceph_fill_dirfrag(struct inode *inode,
238 struct ceph_mds_reply_dirfrag *dirinfo)
239{
240 struct ceph_inode_info *ci = ceph_inode(inode);
241 struct ceph_inode_frag *frag;
242 u32 id = le32_to_cpu(dirinfo->frag);
243 int mds = le32_to_cpu(dirinfo->auth);
244 int ndist = le32_to_cpu(dirinfo->ndist);
245 int i;
246 int err = 0;
247
248 mutex_lock(&ci->i_fragtree_mutex);
249 if (ndist == 0) {
250 /* no delegation info needed. */
251 frag = __ceph_find_frag(ci, id);
252 if (!frag)
253 goto out;
254 if (frag->split_by == 0) {
255 /* tree leaf, remove */
256 dout("fill_dirfrag removed %llx.%llx frag %x"
257 " (no ref)\n", ceph_vinop(inode), id);
258 rb_erase(&frag->node, &ci->i_fragtree);
259 kfree(frag);
260 } else {
261 /* tree branch, keep and clear */
262 dout("fill_dirfrag cleared %llx.%llx frag %x"
263 " referral\n", ceph_vinop(inode), id);
264 frag->mds = -1;
265 frag->ndist = 0;
266 }
267 goto out;
268 }
269
270
271 /* find/add this frag to store mds delegation info */
272 frag = __get_or_create_frag(ci, id);
273 if (IS_ERR(frag)) {
274 /* this is not the end of the world; we can continue
275 with bad/inaccurate delegation info */
276 pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
277 ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
278 err = -ENOMEM;
279 goto out;
280 }
281
282 frag->mds = mds;
283 frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
284 for (i = 0; i < frag->ndist; i++)
285 frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
286 dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
287 ceph_vinop(inode), frag->frag, frag->ndist);
288
289out:
290 mutex_unlock(&ci->i_fragtree_mutex);
291 return err;
292}
293
294
295/*
296 * initialize a newly allocated inode.
297 */
298struct inode *ceph_alloc_inode(struct super_block *sb)
299{
300 struct ceph_inode_info *ci;
301 int i;
302
303 ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
304 if (!ci)
305 return NULL;
306
307 dout("alloc_inode %p\n", &ci->vfs_inode);
308
Sage Weilbe655592011-11-30 09:47:09 -0800309 spin_lock_init(&ci->i_ceph_lock);
310
Sage Weil355da1e2009-10-06 11:31:08 -0700311 ci->i_version = 0;
312 ci->i_time_warp_seq = 0;
313 ci->i_ceph_flags = 0;
Yan, Zheng2f276c52013-03-13 19:44:32 +0800314 atomic_set(&ci->i_release_count, 1);
315 atomic_set(&ci->i_complete_count, 0);
Sage Weil355da1e2009-10-06 11:31:08 -0700316 ci->i_symlink = NULL;
317
Sage Weil6c0f3af2010-11-16 11:14:34 -0800318 memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
319
Sage Weil355da1e2009-10-06 11:31:08 -0700320 ci->i_fragtree = RB_ROOT;
321 mutex_init(&ci->i_fragtree_mutex);
322
323 ci->i_xattrs.blob = NULL;
324 ci->i_xattrs.prealloc_blob = NULL;
325 ci->i_xattrs.dirty = false;
326 ci->i_xattrs.index = RB_ROOT;
327 ci->i_xattrs.count = 0;
328 ci->i_xattrs.names_size = 0;
329 ci->i_xattrs.vals_size = 0;
330 ci->i_xattrs.version = 0;
331 ci->i_xattrs.index_version = 0;
332
333 ci->i_caps = RB_ROOT;
334 ci->i_auth_cap = NULL;
335 ci->i_dirty_caps = 0;
336 ci->i_flushing_caps = 0;
337 INIT_LIST_HEAD(&ci->i_dirty_item);
338 INIT_LIST_HEAD(&ci->i_flushing_item);
339 ci->i_cap_flush_seq = 0;
340 ci->i_cap_flush_last_tid = 0;
341 memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
342 init_waitqueue_head(&ci->i_cap_wq);
343 ci->i_hold_caps_min = 0;
344 ci->i_hold_caps_max = 0;
345 INIT_LIST_HEAD(&ci->i_cap_delay_list);
346 ci->i_cap_exporting_mds = 0;
347 ci->i_cap_exporting_mseq = 0;
348 ci->i_cap_exporting_issued = 0;
349 INIT_LIST_HEAD(&ci->i_cap_snaps);
350 ci->i_head_snapc = NULL;
351 ci->i_snap_caps = 0;
352
353 for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
354 ci->i_nr_by_mode[i] = 0;
355
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700356 mutex_init(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -0700357 ci->i_truncate_seq = 0;
358 ci->i_truncate_size = 0;
359 ci->i_truncate_pending = 0;
360
361 ci->i_max_size = 0;
362 ci->i_reported_size = 0;
363 ci->i_wanted_max_size = 0;
364 ci->i_requested_max_size = 0;
365
366 ci->i_pin_ref = 0;
367 ci->i_rd_ref = 0;
368 ci->i_rdcache_ref = 0;
369 ci->i_wr_ref = 0;
Henry C Changd3d07202011-05-11 10:29:54 +0000370 ci->i_wb_ref = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700371 ci->i_wrbuffer_ref = 0;
372 ci->i_wrbuffer_ref_head = 0;
373 ci->i_shared_gen = 0;
374 ci->i_rdcache_gen = 0;
375 ci->i_rdcache_revoking = 0;
376
377 INIT_LIST_HEAD(&ci->i_unsafe_writes);
378 INIT_LIST_HEAD(&ci->i_unsafe_dirops);
379 spin_lock_init(&ci->i_unsafe_lock);
380
381 ci->i_snap_realm = NULL;
382 INIT_LIST_HEAD(&ci->i_snap_realm_item);
383 INIT_LIST_HEAD(&ci->i_snap_flush_item);
384
Sage Weil3c6f6b72010-02-09 15:24:44 -0800385 INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
386 INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
Sage Weil355da1e2009-10-06 11:31:08 -0700387
388 INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
389
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400390 ceph_fscache_inode_init(ci);
391
Sage Weil355da1e2009-10-06 11:31:08 -0700392 return &ci->vfs_inode;
393}
394
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100395static void ceph_i_callback(struct rcu_head *head)
396{
397 struct inode *inode = container_of(head, struct inode, i_rcu);
398 struct ceph_inode_info *ci = ceph_inode(inode);
399
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100400 kmem_cache_free(ceph_inode_cachep, ci);
401}
402
Sage Weil355da1e2009-10-06 11:31:08 -0700403void ceph_destroy_inode(struct inode *inode)
404{
405 struct ceph_inode_info *ci = ceph_inode(inode);
406 struct ceph_inode_frag *frag;
407 struct rb_node *n;
408
409 dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
410
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400411 ceph_fscache_unregister_inode_cookie(ci);
412
Sage Weil355da1e2009-10-06 11:31:08 -0700413 ceph_queue_caps_release(inode);
414
Sage Weil8b218b82010-03-09 12:59:08 -0800415 /*
416 * we may still have a snap_realm reference if there are stray
417 * caps in i_cap_exporting_issued or i_snap_caps.
418 */
419 if (ci->i_snap_realm) {
420 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700421 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil8b218b82010-03-09 12:59:08 -0800422 struct ceph_snap_realm *realm = ci->i_snap_realm;
423
424 dout(" dropping residual ref to snap realm %p\n", realm);
425 spin_lock(&realm->inodes_with_caps_lock);
426 list_del_init(&ci->i_snap_realm_item);
427 spin_unlock(&realm->inodes_with_caps_lock);
428 ceph_put_snap_realm(mdsc, realm);
429 }
430
Sage Weil355da1e2009-10-06 11:31:08 -0700431 kfree(ci->i_symlink);
432 while ((n = rb_first(&ci->i_fragtree)) != NULL) {
433 frag = rb_entry(n, struct ceph_inode_frag, node);
434 rb_erase(n, &ci->i_fragtree);
435 kfree(frag);
436 }
437
438 __ceph_destroy_xattrs(ci);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800439 if (ci->i_xattrs.blob)
440 ceph_buffer_put(ci->i_xattrs.blob);
441 if (ci->i_xattrs.prealloc_blob)
442 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700443
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100444 call_rcu(&inode->i_rcu, ceph_i_callback);
Sage Weil355da1e2009-10-06 11:31:08 -0700445}
446
Sage Weil355da1e2009-10-06 11:31:08 -0700447/*
448 * Helpers to fill in size, ctime, mtime, and atime. We have to be
449 * careful because either the client or MDS may have more up to date
450 * info, depending on which capabilities are held, and whether
451 * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
452 * and size are monotonically increasing, except when utimes() or
453 * truncate() increments the corresponding _seq values.)
454 */
455int ceph_fill_file_size(struct inode *inode, int issued,
456 u32 truncate_seq, u64 truncate_size, u64 size)
457{
458 struct ceph_inode_info *ci = ceph_inode(inode);
459 int queue_trunc = 0;
460
461 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
462 (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
463 dout("size %lld -> %llu\n", inode->i_size, size);
464 inode->i_size = size;
465 inode->i_blocks = (size + (1<<9) - 1) >> 9;
466 ci->i_reported_size = size;
467 if (truncate_seq != ci->i_truncate_seq) {
468 dout("truncate_seq %u -> %u\n",
469 ci->i_truncate_seq, truncate_seq);
470 ci->i_truncate_seq = truncate_seq;
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700471
472 /* the MDS should have revoked these caps */
473 WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
474 CEPH_CAP_FILE_RD |
475 CEPH_CAP_FILE_WR |
476 CEPH_CAP_FILE_LAZYIO));
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800477 /*
478 * If we hold relevant caps, or in the case where we're
479 * not the only client referencing this file and we
480 * don't hold those caps, then we need to check whether
481 * the file is either opened or mmaped
482 */
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700483 if ((issued & (CEPH_CAP_FILE_CACHE|
484 CEPH_CAP_FILE_BUFFER)) ||
Yehuda Sadeh3d497d82010-02-09 11:08:40 -0800485 mapping_mapped(inode->i_mapping) ||
486 __ceph_caps_file_wanted(ci)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700487 ci->i_truncate_pending++;
488 queue_trunc = 1;
489 }
490 }
491 }
492 if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
493 ci->i_truncate_size != truncate_size) {
494 dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
495 truncate_size);
496 ci->i_truncate_size = truncate_size;
497 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400498
499 if (queue_trunc)
500 ceph_fscache_invalidate(inode);
501
Sage Weil355da1e2009-10-06 11:31:08 -0700502 return queue_trunc;
503}
504
505void ceph_fill_file_time(struct inode *inode, int issued,
506 u64 time_warp_seq, struct timespec *ctime,
507 struct timespec *mtime, struct timespec *atime)
508{
509 struct ceph_inode_info *ci = ceph_inode(inode);
510 int warn = 0;
511
512 if (issued & (CEPH_CAP_FILE_EXCL|
513 CEPH_CAP_FILE_WR|
Sage Weild8672d62010-11-08 09:24:34 -0800514 CEPH_CAP_FILE_BUFFER|
515 CEPH_CAP_AUTH_EXCL|
516 CEPH_CAP_XATTR_EXCL)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700517 if (timespec_compare(ctime, &inode->i_ctime) > 0) {
518 dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
519 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
520 ctime->tv_sec, ctime->tv_nsec);
521 inode->i_ctime = *ctime;
522 }
523 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
524 /* the MDS did a utimes() */
525 dout("mtime %ld.%09ld -> %ld.%09ld "
526 "tw %d -> %d\n",
527 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
528 mtime->tv_sec, mtime->tv_nsec,
529 ci->i_time_warp_seq, (int)time_warp_seq);
530
531 inode->i_mtime = *mtime;
532 inode->i_atime = *atime;
533 ci->i_time_warp_seq = time_warp_seq;
534 } else if (time_warp_seq == ci->i_time_warp_seq) {
535 /* nobody did utimes(); take the max */
536 if (timespec_compare(mtime, &inode->i_mtime) > 0) {
537 dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
538 inode->i_mtime.tv_sec,
539 inode->i_mtime.tv_nsec,
540 mtime->tv_sec, mtime->tv_nsec);
541 inode->i_mtime = *mtime;
542 }
543 if (timespec_compare(atime, &inode->i_atime) > 0) {
544 dout("atime %ld.%09ld -> %ld.%09ld inc\n",
545 inode->i_atime.tv_sec,
546 inode->i_atime.tv_nsec,
547 atime->tv_sec, atime->tv_nsec);
548 inode->i_atime = *atime;
549 }
550 } else if (issued & CEPH_CAP_FILE_EXCL) {
551 /* we did a utimes(); ignore mds values */
552 } else {
553 warn = 1;
554 }
555 } else {
Sage Weild8672d62010-11-08 09:24:34 -0800556 /* we have no write|excl caps; whatever the MDS says is true */
Sage Weil355da1e2009-10-06 11:31:08 -0700557 if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
558 inode->i_ctime = *ctime;
559 inode->i_mtime = *mtime;
560 inode->i_atime = *atime;
561 ci->i_time_warp_seq = time_warp_seq;
562 } else {
563 warn = 1;
564 }
565 }
566 if (warn) /* time_warp_seq shouldn't go backwards */
567 dout("%p mds time_warp_seq %llu < %u\n",
568 inode, time_warp_seq, ci->i_time_warp_seq);
569}
570
571/*
572 * Populate an inode based on info from mds. May be called on new or
573 * existing inodes.
574 */
575static int fill_inode(struct inode *inode,
576 struct ceph_mds_reply_info_in *iinfo,
577 struct ceph_mds_reply_dirfrag *dirinfo,
578 struct ceph_mds_session *session,
579 unsigned long ttl_from, int cap_fmode,
580 struct ceph_cap_reservation *caps_reservation)
581{
582 struct ceph_mds_reply_inode *info = iinfo->in;
583 struct ceph_inode_info *ci = ceph_inode(inode);
584 int i;
Sage Weildfabbed62011-07-26 11:30:02 -0700585 int issued = 0, implemented;
Sage Weil355da1e2009-10-06 11:31:08 -0700586 struct timespec mtime, atime, ctime;
587 u32 nsplits;
588 struct ceph_buffer *xattr_blob = NULL;
589 int err = 0;
590 int queue_trunc = 0;
591
592 dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
593 inode, ceph_vinop(inode), le64_to_cpu(info->version),
594 ci->i_version);
595
596 /*
597 * prealloc xattr data, if it looks like we'll need it. only
598 * if len > 4 (meaning there are actually xattrs; the first 4
599 * bytes are the xattr count).
600 */
601 if (iinfo->xattr_len > 4) {
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800602 xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700603 if (!xattr_blob)
604 pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
605 iinfo->xattr_len);
606 }
607
Sage Weilbe655592011-11-30 09:47:09 -0800608 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700609
610 /*
611 * provided version will be odd if inode value is projected,
Sage Weil8bd59e02010-11-08 09:23:12 -0800612 * even if stable. skip the update if we have newer stable
613 * info (ours>=theirs, e.g. due to racing mds replies), unless
614 * we are getting projected (unstable) info (in which case the
615 * version is odd, and we want ours>theirs).
616 * us them
617 * 2 2 skip
618 * 3 2 skip
619 * 3 3 update
Sage Weil355da1e2009-10-06 11:31:08 -0700620 */
621 if (le64_to_cpu(info->version) > 0 &&
Sage Weil8bd59e02010-11-08 09:23:12 -0800622 (ci->i_version & ~1) >= le64_to_cpu(info->version))
Sage Weil355da1e2009-10-06 11:31:08 -0700623 goto no_change;
Sage Weildfabbed62011-07-26 11:30:02 -0700624
Sage Weil355da1e2009-10-06 11:31:08 -0700625 issued = __ceph_caps_issued(ci, &implemented);
626 issued |= implemented | __ceph_caps_dirty(ci);
627
628 /* update inode */
629 ci->i_version = le64_to_cpu(info->version);
630 inode->i_version++;
631 inode->i_rdev = le32_to_cpu(info->rdev);
632
633 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
634 inode->i_mode = le32_to_cpu(info->mode);
Eric W. Biedermanab871b92013-01-31 03:40:12 -0800635 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
636 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
Sage Weil355da1e2009-10-06 11:31:08 -0700637 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -0800638 from_kuid(&init_user_ns, inode->i_uid),
639 from_kgid(&init_user_ns, inode->i_gid));
Sage Weil355da1e2009-10-06 11:31:08 -0700640 }
641
642 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200643 set_nlink(inode, le32_to_cpu(info->nlink));
Sage Weil355da1e2009-10-06 11:31:08 -0700644
645 /* be careful with mtime, atime, size */
646 ceph_decode_timespec(&atime, &info->atime);
647 ceph_decode_timespec(&mtime, &info->mtime);
648 ceph_decode_timespec(&ctime, &info->ctime);
649 queue_trunc = ceph_fill_file_size(inode, issued,
650 le32_to_cpu(info->truncate_seq),
651 le64_to_cpu(info->truncate_size),
Sage Weil355da1e2009-10-06 11:31:08 -0700652 le64_to_cpu(info->size));
653 ceph_fill_file_time(inode, issued,
654 le32_to_cpu(info->time_warp_seq),
655 &ctime, &mtime, &atime);
656
Sage Weil912a9b02010-11-07 09:37:25 -0800657 /* only update max_size on auth cap */
658 if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
659 ci->i_max_size != le64_to_cpu(info->max_size)) {
660 dout("max_size %lld -> %llu\n", ci->i_max_size,
661 le64_to_cpu(info->max_size));
662 ci->i_max_size = le64_to_cpu(info->max_size);
663 }
664
Sage Weil355da1e2009-10-06 11:31:08 -0700665 ci->i_layout = info->layout;
666 inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
667
668 /* xattrs */
669 /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
670 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
671 le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
672 if (ci->i_xattrs.blob)
673 ceph_buffer_put(ci->i_xattrs.blob);
674 ci->i_xattrs.blob = xattr_blob;
675 if (xattr_blob)
676 memcpy(ci->i_xattrs.blob->vec.iov_base,
677 iinfo->xattr_data, iinfo->xattr_len);
678 ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
Sage Weila6424e42010-04-29 09:28:11 -0700679 xattr_blob = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700680 }
681
682 inode->i_mapping->a_ops = &ceph_aops;
683 inode->i_mapping->backing_dev_info =
Cheng Renquan640ef792010-03-26 17:40:33 +0800684 &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
Sage Weil355da1e2009-10-06 11:31:08 -0700685
686 switch (inode->i_mode & S_IFMT) {
687 case S_IFIFO:
688 case S_IFBLK:
689 case S_IFCHR:
690 case S_IFSOCK:
691 init_special_inode(inode, inode->i_mode, inode->i_rdev);
692 inode->i_op = &ceph_file_iops;
693 break;
694 case S_IFREG:
695 inode->i_op = &ceph_file_iops;
696 inode->i_fop = &ceph_file_fops;
697 break;
698 case S_IFLNK:
699 inode->i_op = &ceph_symlink_iops;
700 if (!ci->i_symlink) {
Xi Wang810339ec2012-02-03 09:55:36 -0500701 u32 symlen = iinfo->symlink_len;
Sage Weil355da1e2009-10-06 11:31:08 -0700702 char *sym;
703
Sage Weilbe655592011-11-30 09:47:09 -0800704 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700705
Xi Wang810339ec2012-02-03 09:55:36 -0500706 err = -EINVAL;
707 if (WARN_ON(symlen != inode->i_size))
708 goto out;
709
Sage Weil355da1e2009-10-06 11:31:08 -0700710 err = -ENOMEM;
Xi Wang810339ec2012-02-03 09:55:36 -0500711 sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700712 if (!sym)
713 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700714
Sage Weilbe655592011-11-30 09:47:09 -0800715 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700716 if (!ci->i_symlink)
717 ci->i_symlink = sym;
718 else
719 kfree(sym); /* lost a race */
720 }
721 break;
722 case S_IFDIR:
723 inode->i_op = &ceph_dir_iops;
724 inode->i_fop = &ceph_dir_fops;
725
Sage Weil14303d22010-12-14 17:37:52 -0800726 ci->i_dir_layout = iinfo->dir_layout;
727
Sage Weil355da1e2009-10-06 11:31:08 -0700728 ci->i_files = le64_to_cpu(info->files);
729 ci->i_subdirs = le64_to_cpu(info->subdirs);
730 ci->i_rbytes = le64_to_cpu(info->rbytes);
731 ci->i_rfiles = le64_to_cpu(info->rfiles);
732 ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
733 ceph_decode_timespec(&ci->i_rctime, &info->rctime);
Sage Weil355da1e2009-10-06 11:31:08 -0700734 break;
735 default:
736 pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
737 ceph_vinop(inode), inode->i_mode);
738 }
739
Yan, Zhenga8673d62013-02-18 16:38:14 +0800740 /* set dir completion flag? */
741 if (S_ISDIR(inode->i_mode) &&
742 ci->i_files == 0 && ci->i_subdirs == 0 &&
743 ceph_snap(inode) == CEPH_NOSNAP &&
744 (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
745 (issued & CEPH_CAP_FILE_EXCL) == 0 &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800746 !__ceph_dir_is_complete(ci)) {
Yan, Zhenga8673d62013-02-18 16:38:14 +0800747 dout(" marking %p complete (empty)\n", inode);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800748 __ceph_dir_set_complete(ci, atomic_read(&ci->i_release_count));
Yan, Zhenga8673d62013-02-18 16:38:14 +0800749 ci->i_max_offset = 2;
750 }
Sage Weil355da1e2009-10-06 11:31:08 -0700751no_change:
Sage Weilbe655592011-11-30 09:47:09 -0800752 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700753
754 /* queue truncate if we saw i_size decrease */
755 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -0800756 ceph_queue_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700757
758 /* populate frag tree */
759 /* FIXME: move me up, if/when version reflects fragtree changes */
760 nsplits = le32_to_cpu(info->fragtree.nsplits);
761 mutex_lock(&ci->i_fragtree_mutex);
762 for (i = 0; i < nsplits; i++) {
763 u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
764 struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
765
766 if (IS_ERR(frag))
767 continue;
768 frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
769 dout(" frag %x split by %d\n", frag->frag, frag->split_by);
770 }
771 mutex_unlock(&ci->i_fragtree_mutex);
772
773 /* were we issued a capability? */
774 if (info->cap.caps) {
775 if (ceph_snap(inode) == CEPH_NOSNAP) {
776 ceph_add_cap(inode, session,
777 le64_to_cpu(info->cap.cap_id),
778 cap_fmode,
779 le32_to_cpu(info->cap.caps),
780 le32_to_cpu(info->cap.wanted),
781 le32_to_cpu(info->cap.seq),
782 le32_to_cpu(info->cap.mseq),
783 le64_to_cpu(info->cap.realm),
784 info->cap.flags,
785 caps_reservation);
786 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800787 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700788 dout(" %p got snap_caps %s\n", inode,
789 ceph_cap_string(le32_to_cpu(info->cap.caps)));
790 ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
791 if (cap_fmode >= 0)
792 __ceph_get_fmode(ci, cap_fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800793 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700794 }
Sage Weil04d000e2010-05-07 11:26:34 -0700795 } else if (cap_fmode >= 0) {
796 pr_warning("mds issued no caps on %llx.%llx\n",
797 ceph_vinop(inode));
798 __ceph_get_fmode(ci, cap_fmode);
Sage Weil355da1e2009-10-06 11:31:08 -0700799 }
800
801 /* update delegation info? */
802 if (dirinfo)
803 ceph_fill_dirfrag(inode, dirinfo);
804
805 err = 0;
806
807out:
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800808 if (xattr_blob)
809 ceph_buffer_put(xattr_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700810 return err;
811}
812
813/*
814 * caller should hold session s_mutex.
815 */
816static void update_dentry_lease(struct dentry *dentry,
817 struct ceph_mds_reply_lease *lease,
818 struct ceph_mds_session *session,
819 unsigned long from_time)
820{
821 struct ceph_dentry_info *di = ceph_dentry(dentry);
822 long unsigned duration = le32_to_cpu(lease->duration_ms);
823 long unsigned ttl = from_time + (duration * HZ) / 1000;
824 long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
825 struct inode *dir;
826
827 /* only track leases on regular dentries */
828 if (dentry->d_op != &ceph_dentry_ops)
829 return;
830
831 spin_lock(&dentry->d_lock);
Sage Weil2f90b852011-07-26 11:28:25 -0700832 dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
833 dentry, duration, ttl);
Sage Weil355da1e2009-10-06 11:31:08 -0700834
835 /* make lease_rdcache_gen match directory */
836 dir = dentry->d_parent->d_inode;
837 di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
838
Sage Weil2f90b852011-07-26 11:28:25 -0700839 if (duration == 0)
Sage Weil355da1e2009-10-06 11:31:08 -0700840 goto out_unlock;
841
842 if (di->lease_gen == session->s_cap_gen &&
843 time_before(ttl, dentry->d_time))
844 goto out_unlock; /* we already have a newer lease. */
845
846 if (di->lease_session && di->lease_session != session)
847 goto out_unlock;
848
849 ceph_dentry_lru_touch(dentry);
850
851 if (!di->lease_session)
852 di->lease_session = ceph_get_mds_session(session);
853 di->lease_gen = session->s_cap_gen;
854 di->lease_seq = le32_to_cpu(lease->seq);
855 di->lease_renew_after = half_ttl;
856 di->lease_renew_from = 0;
857 dentry->d_time = ttl;
858out_unlock:
859 spin_unlock(&dentry->d_lock);
860 return;
861}
862
863/*
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800864 * Set dentry's directory position based on the current dir's max, and
865 * order it in d_subdirs, so that dcache_readdir behaves.
Sage Weil4f177262011-07-26 11:31:08 -0700866 *
867 * Always called under directory's i_mutex.
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800868 */
869static void ceph_set_dentry_offset(struct dentry *dn)
870{
871 struct dentry *dir = dn->d_parent;
Sage Weil4f177262011-07-26 11:31:08 -0700872 struct inode *inode = dir->d_inode;
Yehuda Sadehb8cd9522011-12-13 09:56:30 -0800873 struct ceph_inode_info *ci;
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800874 struct ceph_dentry_info *di;
875
876 BUG_ON(!inode);
877
Yehuda Sadehb8cd9522011-12-13 09:56:30 -0800878 ci = ceph_inode(inode);
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800879 di = ceph_dentry(dn);
880
Sage Weilbe655592011-11-30 09:47:09 -0800881 spin_lock(&ci->i_ceph_lock);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800882 if (!__ceph_dir_is_complete(ci)) {
Sage Weilbe655592011-11-30 09:47:09 -0800883 spin_unlock(&ci->i_ceph_lock);
Sage Weile8a74982010-04-15 14:08:49 -0700884 return;
885 }
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800886 di->offset = ceph_inode(inode)->i_max_offset++;
Sage Weilbe655592011-11-30 09:47:09 -0800887 spin_unlock(&ci->i_ceph_lock);
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800888
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100889 spin_lock(&dir->d_lock);
890 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
Henry C Chang13a42142010-06-01 11:31:08 -0700891 list_move(&dn->d_u.d_child, &dir->d_subdirs);
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800892 dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
893 dn->d_u.d_child.prev, dn->d_u.d_child.next);
894 spin_unlock(&dn->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100895 spin_unlock(&dir->d_lock);
Yehuda Sadeh4baa75e2010-01-07 15:36:32 -0800896}
897
898/*
Sage Weil1cd39352010-05-03 22:08:02 -0700899 * splice a dentry to an inode.
900 * caller must hold directory i_mutex for this to be safe.
901 *
902 * we will only rehash the resulting dentry if @prehash is
903 * true; @prehash will be set to false (for the benefit of
904 * the caller) if we fail.
905 */
906static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
Sage Weil467c5252010-09-13 11:39:20 -0700907 bool *prehash, bool set_offset)
Sage Weil1cd39352010-05-03 22:08:02 -0700908{
909 struct dentry *realdn;
910
911 BUG_ON(dn->d_inode);
912
913 /* dn must be unhashed */
914 if (!d_unhashed(dn))
915 d_drop(dn);
916 realdn = d_materialise_unique(dn, in);
917 if (IS_ERR(realdn)) {
Sage Weild69ed052010-06-21 10:38:14 -0700918 pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
919 PTR_ERR(realdn), dn, in, ceph_vinop(in));
Sage Weil1cd39352010-05-03 22:08:02 -0700920 if (prehash)
921 *prehash = false; /* don't rehash on error */
922 dn = realdn; /* note realdn contains the error */
923 goto out;
924 } else if (realdn) {
925 dout("dn %p (%d) spliced with %p (%d) "
926 "inode %p ino %llx.%llx\n",
Al Viro84d08fa2013-07-05 18:59:33 +0400927 dn, d_count(dn),
928 realdn, d_count(realdn),
Sage Weil1cd39352010-05-03 22:08:02 -0700929 realdn->d_inode, ceph_vinop(realdn->d_inode));
930 dput(dn);
931 dn = realdn;
932 } else {
933 BUG_ON(!ceph_dentry(dn));
934 dout("dn %p attached to %p ino %llx.%llx\n",
935 dn, dn->d_inode, ceph_vinop(dn->d_inode));
936 }
937 if ((!prehash || *prehash) && d_unhashed(dn))
938 d_rehash(dn);
Sage Weil467c5252010-09-13 11:39:20 -0700939 if (set_offset)
940 ceph_set_dentry_offset(dn);
Sage Weil1cd39352010-05-03 22:08:02 -0700941out:
942 return dn;
943}
944
945/*
Sage Weil355da1e2009-10-06 11:31:08 -0700946 * Incorporate results into the local cache. This is either just
947 * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
948 * after a lookup).
949 *
950 * A reply may contain
951 * a directory inode along with a dentry.
952 * and/or a target inode
953 *
954 * Called with snap_rwsem (read).
955 */
956int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
957 struct ceph_mds_session *session)
958{
959 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
960 struct inode *in = NULL;
961 struct ceph_mds_reply_inode *ininfo;
962 struct ceph_vino vino;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700963 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700964 int i = 0;
965 int err = 0;
966
967 dout("fill_trace %p is_dentry %d is_target %d\n", req,
968 rinfo->head->is_dentry, rinfo->head->is_target);
969
970#if 0
971 /*
972 * Debugging hook:
973 *
974 * If we resend completed ops to a recovering mds, we get no
975 * trace. Since that is very rare, pretend this is the case
976 * to ensure the 'no trace' handlers in the callers behave.
977 *
978 * Fill in inodes unconditionally to avoid breaking cap
979 * invariants.
980 */
981 if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
982 pr_info("fill_trace faking empty trace on %lld %s\n",
983 req->r_tid, ceph_mds_op_name(rinfo->head->op));
984 if (rinfo->head->is_dentry) {
985 rinfo->head->is_dentry = 0;
986 err = fill_inode(req->r_locked_dir,
987 &rinfo->diri, rinfo->dirfrag,
988 session, req->r_request_started, -1);
989 }
990 if (rinfo->head->is_target) {
991 rinfo->head->is_target = 0;
992 ininfo = rinfo->targeti.in;
993 vino.ino = le64_to_cpu(ininfo->ino);
994 vino.snap = le64_to_cpu(ininfo->snapid);
995 in = ceph_get_inode(sb, vino);
996 err = fill_inode(in, &rinfo->targeti, NULL,
997 session, req->r_request_started,
998 req->r_fmode);
999 iput(in);
1000 }
1001 }
1002#endif
1003
1004 if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1005 dout("fill_trace reply is empty!\n");
Sage Weil167c9e32010-05-14 10:02:57 -07001006 if (rinfo->head->result == 0 && req->r_locked_dir)
1007 ceph_invalidate_dir_request(req);
Sage Weil355da1e2009-10-06 11:31:08 -07001008 return 0;
1009 }
1010
1011 if (rinfo->head->is_dentry) {
Sage Weil5b1daec2010-01-25 11:33:08 -08001012 struct inode *dir = req->r_locked_dir;
1013
Sage Weil6c5e50f2012-08-21 15:55:25 -07001014 if (dir) {
1015 err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
1016 session, req->r_request_started, -1,
1017 &req->r_caps_reservation);
1018 if (err < 0)
1019 return err;
1020 } else {
1021 WARN_ON_ONCE(1);
1022 }
Sage Weil5b1daec2010-01-25 11:33:08 -08001023 }
1024
Sage Weil9358c6d2010-03-30 13:54:41 -07001025 /*
1026 * ignore null lease/binding on snapdir ENOENT, or else we
1027 * will have trouble splicing in the virtual snapdir later
1028 */
1029 if (rinfo->head->is_dentry && !req->r_aborted &&
Sage Weil6c5e50f2012-08-21 15:55:25 -07001030 req->r_locked_dir &&
Sage Weil9358c6d2010-03-30 13:54:41 -07001031 (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001032 fsc->mount_options->snapdir_name,
Sage Weil9358c6d2010-03-30 13:54:41 -07001033 req->r_dentry->d_name.len))) {
Sage Weil355da1e2009-10-06 11:31:08 -07001034 /*
1035 * lookup link rename : null -> possibly existing inode
1036 * mknod symlink mkdir : null -> new inode
1037 * unlink : linked -> null
1038 */
1039 struct inode *dir = req->r_locked_dir;
1040 struct dentry *dn = req->r_dentry;
1041 bool have_dir_cap, have_lease;
1042
1043 BUG_ON(!dn);
1044 BUG_ON(!dir);
1045 BUG_ON(dn->d_parent->d_inode != dir);
1046 BUG_ON(ceph_ino(dir) !=
1047 le64_to_cpu(rinfo->diri.in->ino));
1048 BUG_ON(ceph_snap(dir) !=
1049 le64_to_cpu(rinfo->diri.in->snapid));
1050
Sage Weil355da1e2009-10-06 11:31:08 -07001051 /* do we have a lease on the whole dir? */
1052 have_dir_cap =
1053 (le32_to_cpu(rinfo->diri.in->cap.caps) &
1054 CEPH_CAP_FILE_SHARED);
1055
1056 /* do we have a dn lease? */
1057 have_lease = have_dir_cap ||
Sage Weil2f90b852011-07-26 11:28:25 -07001058 le32_to_cpu(rinfo->dlease->duration_ms);
Sage Weil355da1e2009-10-06 11:31:08 -07001059 if (!have_lease)
1060 dout("fill_trace no dentry lease or dir cap\n");
1061
1062 /* rename? */
1063 if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
1064 dout(" src %p '%.*s' dst %p '%.*s'\n",
1065 req->r_old_dentry,
1066 req->r_old_dentry->d_name.len,
1067 req->r_old_dentry->d_name.name,
1068 dn, dn->d_name.len, dn->d_name.name);
1069 dout("fill_trace doing d_move %p -> %p\n",
1070 req->r_old_dentry, dn);
Sage Weilc10f5e12010-04-16 12:56:11 -07001071
Sage Weil355da1e2009-10-06 11:31:08 -07001072 d_move(req->r_old_dentry, dn);
1073 dout(" src %p '%.*s' dst %p '%.*s'\n",
1074 req->r_old_dentry,
1075 req->r_old_dentry->d_name.len,
1076 req->r_old_dentry->d_name.name,
1077 dn, dn->d_name.len, dn->d_name.name);
Sage Weil81a6cf22010-05-14 09:35:38 -07001078
Sage Weilc4a29f22009-12-21 11:42:18 -08001079 /* ensure target dentry is invalidated, despite
1080 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001081 ceph_invalidate_dentry_lease(dn);
1082
Sage Weil09adc802011-02-04 21:38:47 -08001083 /*
1084 * d_move() puts the renamed dentry at the end of
1085 * d_subdirs. We need to assign it an appropriate
Yan, Zheng2f276c52013-03-13 19:44:32 +08001086 * directory offset so we can behave when dir is
1087 * complete.
Sage Weil09adc802011-02-04 21:38:47 -08001088 */
1089 ceph_set_dentry_offset(req->r_old_dentry);
Milosz Tanski99ccbd22013-08-21 17:29:54 -04001090 dout("dn %p gets new offset %lld\n", req->r_old_dentry,
Sage Weil1cd39352010-05-03 22:08:02 -07001091 ceph_dentry(req->r_old_dentry)->offset);
Sage Weil81a6cf22010-05-14 09:35:38 -07001092
Sage Weil355da1e2009-10-06 11:31:08 -07001093 dn = req->r_old_dentry; /* use old_dentry */
1094 in = dn->d_inode;
1095 }
1096
1097 /* null dentry? */
1098 if (!rinfo->head->is_target) {
1099 dout("fill_trace null dentry\n");
1100 if (dn->d_inode) {
1101 dout("d_delete %p\n", dn);
1102 d_delete(dn);
1103 } else {
1104 dout("d_instantiate %p NULL\n", dn);
1105 d_instantiate(dn, NULL);
1106 if (have_lease && d_unhashed(dn))
1107 d_rehash(dn);
1108 update_dentry_lease(dn, rinfo->dlease,
1109 session,
1110 req->r_request_started);
1111 }
1112 goto done;
1113 }
1114
1115 /* attach proper inode */
1116 ininfo = rinfo->targeti.in;
1117 vino.ino = le64_to_cpu(ininfo->ino);
1118 vino.snap = le64_to_cpu(ininfo->snapid);
Sage Weild8b16b32010-11-06 12:41:16 -07001119 in = dn->d_inode;
1120 if (!in) {
Sage Weil355da1e2009-10-06 11:31:08 -07001121 in = ceph_get_inode(sb, vino);
1122 if (IS_ERR(in)) {
1123 pr_err("fill_trace bad get_inode "
1124 "%llx.%llx\n", vino.ino, vino.snap);
1125 err = PTR_ERR(in);
Al Viro2744c172012-09-26 21:41:05 -04001126 d_drop(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001127 goto done;
1128 }
Sage Weil467c5252010-09-13 11:39:20 -07001129 dn = splice_dentry(dn, in, &have_lease, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001130 if (IS_ERR(dn)) {
1131 err = PTR_ERR(dn);
1132 goto done;
1133 }
1134 req->r_dentry = dn; /* may have spliced */
Sage Weil70b666c2011-05-27 09:24:26 -07001135 ihold(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001136 } else if (ceph_ino(in) == vino.ino &&
1137 ceph_snap(in) == vino.snap) {
Sage Weil70b666c2011-05-27 09:24:26 -07001138 ihold(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001139 } else {
1140 dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
1141 dn, in, ceph_ino(in), ceph_snap(in),
1142 vino.ino, vino.snap);
1143 have_lease = false;
1144 in = NULL;
1145 }
1146
1147 if (have_lease)
1148 update_dentry_lease(dn, rinfo->dlease, session,
1149 req->r_request_started);
1150 dout(" final dn %p\n", dn);
1151 i++;
Sage Weil79f9f992013-01-29 02:55:31 -05001152 } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1153 req->r_op == CEPH_MDS_OP_MKSNAP) && !req->r_aborted) {
Sage Weil355da1e2009-10-06 11:31:08 -07001154 struct dentry *dn = req->r_dentry;
1155
1156 /* fill out a snapdir LOOKUPSNAP dentry */
1157 BUG_ON(!dn);
1158 BUG_ON(!req->r_locked_dir);
1159 BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
1160 ininfo = rinfo->targeti.in;
1161 vino.ino = le64_to_cpu(ininfo->ino);
1162 vino.snap = le64_to_cpu(ininfo->snapid);
1163 in = ceph_get_inode(sb, vino);
1164 if (IS_ERR(in)) {
1165 pr_err("fill_inode get_inode badness %llx.%llx\n",
1166 vino.ino, vino.snap);
1167 err = PTR_ERR(in);
1168 d_delete(dn);
1169 goto done;
1170 }
1171 dout(" linking snapped dir %p to dn %p\n", in, dn);
Sage Weil467c5252010-09-13 11:39:20 -07001172 dn = splice_dentry(dn, in, NULL, true);
Sage Weil355da1e2009-10-06 11:31:08 -07001173 if (IS_ERR(dn)) {
1174 err = PTR_ERR(dn);
1175 goto done;
1176 }
1177 req->r_dentry = dn; /* may have spliced */
Sage Weil70b666c2011-05-27 09:24:26 -07001178 ihold(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001179 rinfo->head->is_dentry = 1; /* fool notrace handlers */
1180 }
1181
1182 if (rinfo->head->is_target) {
1183 vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1184 vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1185
1186 if (in == NULL || ceph_ino(in) != vino.ino ||
1187 ceph_snap(in) != vino.snap) {
1188 in = ceph_get_inode(sb, vino);
1189 if (IS_ERR(in)) {
1190 err = PTR_ERR(in);
1191 goto done;
1192 }
1193 }
1194 req->r_target_inode = in;
1195
1196 err = fill_inode(in,
1197 &rinfo->targeti, NULL,
1198 session, req->r_request_started,
1199 (le32_to_cpu(rinfo->head->result) == 0) ?
1200 req->r_fmode : -1,
1201 &req->r_caps_reservation);
1202 if (err < 0) {
1203 pr_err("fill_inode badness %p %llx.%llx\n",
1204 in, ceph_vinop(in));
1205 goto done;
1206 }
1207 }
1208
1209done:
1210 dout("fill_trace done err=%d\n", err);
1211 return err;
1212}
1213
1214/*
1215 * Prepopulate our cache with readdir results, leases, etc.
1216 */
Sage Weil79f9f992013-01-29 02:55:31 -05001217static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
1218 struct ceph_mds_session *session)
1219{
1220 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1221 int i, err = 0;
1222
1223 for (i = 0; i < rinfo->dir_nr; i++) {
1224 struct ceph_vino vino;
1225 struct inode *in;
1226 int rc;
1227
1228 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1229 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1230
1231 in = ceph_get_inode(req->r_dentry->d_sb, vino);
1232 if (IS_ERR(in)) {
1233 err = PTR_ERR(in);
1234 dout("new_inode badness got %d\n", err);
1235 continue;
1236 }
1237 rc = fill_inode(in, &rinfo->dir_in[i], NULL, session,
1238 req->r_request_started, -1,
1239 &req->r_caps_reservation);
1240 if (rc < 0) {
1241 pr_err("fill_inode badness on %p got %d\n", in, rc);
1242 err = rc;
1243 continue;
1244 }
1245 }
1246
1247 return err;
1248}
1249
Sage Weil355da1e2009-10-06 11:31:08 -07001250int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1251 struct ceph_mds_session *session)
1252{
1253 struct dentry *parent = req->r_dentry;
1254 struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1255 struct qstr dname;
1256 struct dentry *dn;
1257 struct inode *in;
1258 int err = 0, i;
1259 struct inode *snapdir = NULL;
1260 struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
1261 u64 frag = le32_to_cpu(rhead->args.readdir.frag);
1262 struct ceph_dentry_info *di;
1263
Sage Weil79f9f992013-01-29 02:55:31 -05001264 if (req->r_aborted)
1265 return readdir_prepopulate_inodes_only(req, session);
1266
Sage Weil355da1e2009-10-06 11:31:08 -07001267 if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1268 snapdir = ceph_get_snapdir(parent->d_inode);
1269 parent = d_find_alias(snapdir);
1270 dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1271 rinfo->dir_nr, parent);
1272 } else {
1273 dout("readdir_prepopulate %d items under dn %p\n",
1274 rinfo->dir_nr, parent);
1275 if (rinfo->dir_dir)
1276 ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
1277 }
1278
1279 for (i = 0; i < rinfo->dir_nr; i++) {
1280 struct ceph_vino vino;
1281
1282 dname.name = rinfo->dir_dname[i];
1283 dname.len = rinfo->dir_dname_len[i];
1284 dname.hash = full_name_hash(dname.name, dname.len);
1285
1286 vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
1287 vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
1288
1289retry_lookup:
1290 dn = d_lookup(parent, &dname);
1291 dout("d_lookup on parent=%p name=%.*s got %p\n",
1292 parent, dname.len, dname.name, dn);
1293
1294 if (!dn) {
1295 dn = d_alloc(parent, &dname);
1296 dout("d_alloc %p '%.*s' = %p\n", parent,
1297 dname.len, dname.name, dn);
1298 if (dn == NULL) {
1299 dout("d_alloc badness\n");
1300 err = -ENOMEM;
1301 goto out;
1302 }
1303 err = ceph_init_dentry(dn);
Sage Weil8c696732010-07-22 14:11:56 -07001304 if (err < 0) {
1305 dput(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001306 goto out;
Sage Weil8c696732010-07-22 14:11:56 -07001307 }
Sage Weil355da1e2009-10-06 11:31:08 -07001308 } else if (dn->d_inode &&
1309 (ceph_ino(dn->d_inode) != vino.ino ||
1310 ceph_snap(dn->d_inode) != vino.snap)) {
1311 dout(" dn %p points to wrong inode %p\n",
1312 dn, dn->d_inode);
1313 d_delete(dn);
1314 dput(dn);
1315 goto retry_lookup;
1316 } else {
1317 /* reorder parent's d_subdirs */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001318 spin_lock(&parent->d_lock);
1319 spin_lock_nested(&dn->d_lock, DENTRY_D_LOCK_NESTED);
Sage Weil355da1e2009-10-06 11:31:08 -07001320 list_move(&dn->d_u.d_child, &parent->d_subdirs);
1321 spin_unlock(&dn->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001322 spin_unlock(&parent->d_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001323 }
1324
1325 di = dn->d_fsdata;
1326 di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
1327
1328 /* inode */
1329 if (dn->d_inode) {
1330 in = dn->d_inode;
1331 } else {
1332 in = ceph_get_inode(parent->d_sb, vino);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001333 if (IS_ERR(in)) {
Sage Weil355da1e2009-10-06 11:31:08 -07001334 dout("new_inode badness\n");
Al Viro2744c172012-09-26 21:41:05 -04001335 d_drop(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001336 dput(dn);
Dan Carpenterac1f12e2010-08-25 09:11:35 +02001337 err = PTR_ERR(in);
Sage Weil355da1e2009-10-06 11:31:08 -07001338 goto out;
1339 }
Sage Weil467c5252010-09-13 11:39:20 -07001340 dn = splice_dentry(dn, in, NULL, false);
Sage Weild69ed052010-06-21 10:38:14 -07001341 if (IS_ERR(dn))
1342 dn = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -07001343 }
1344
1345 if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
1346 req->r_request_started, -1,
1347 &req->r_caps_reservation) < 0) {
1348 pr_err("fill_inode badness on %p\n", in);
Sage Weild69ed052010-06-21 10:38:14 -07001349 goto next_item;
Sage Weil355da1e2009-10-06 11:31:08 -07001350 }
Sage Weild69ed052010-06-21 10:38:14 -07001351 if (dn)
1352 update_dentry_lease(dn, rinfo->dir_dlease[i],
1353 req->r_session,
1354 req->r_request_started);
1355next_item:
1356 if (dn)
1357 dput(dn);
Sage Weil355da1e2009-10-06 11:31:08 -07001358 }
1359 req->r_did_prepopulate = true;
1360
1361out:
1362 if (snapdir) {
1363 iput(snapdir);
1364 dput(parent);
1365 }
1366 dout("readdir_prepopulate done\n");
1367 return err;
1368}
1369
1370int ceph_inode_set_size(struct inode *inode, loff_t size)
1371{
1372 struct ceph_inode_info *ci = ceph_inode(inode);
1373 int ret = 0;
1374
Sage Weilbe655592011-11-30 09:47:09 -08001375 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001376 dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
1377 inode->i_size = size;
1378 inode->i_blocks = (size + (1 << 9) - 1) >> 9;
1379
1380 /* tell the MDS if we are approaching max_size */
1381 if ((size << 1) >= ci->i_max_size &&
1382 (ci->i_reported_size << 1) < ci->i_max_size)
1383 ret = 1;
1384
Sage Weilbe655592011-11-30 09:47:09 -08001385 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001386 return ret;
1387}
1388
1389/*
1390 * Write back inode data in a worker thread. (This can't be done
1391 * in the message handler context.)
1392 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001393void ceph_queue_writeback(struct inode *inode)
1394{
Sage Weil15a20152011-11-05 22:06:31 -07001395 ihold(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001396 if (queue_work(ceph_inode_to_client(inode)->wb_wq,
1397 &ceph_inode(inode)->i_wb_work)) {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001398 dout("ceph_queue_writeback %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001399 } else {
Sage Weil2c27c9a2010-02-17 15:45:51 -08001400 dout("ceph_queue_writeback %p failed\n", inode);
Sage Weil15a20152011-11-05 22:06:31 -07001401 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001402 }
1403}
1404
1405static void ceph_writeback_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001406{
1407 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1408 i_wb_work);
1409 struct inode *inode = &ci->vfs_inode;
1410
1411 dout("writeback %p\n", inode);
1412 filemap_fdatawrite(&inode->i_data);
1413 iput(inode);
1414}
1415
1416/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001417 * queue an async invalidation
1418 */
1419void ceph_queue_invalidate(struct inode *inode)
1420{
Sage Weil15a20152011-11-05 22:06:31 -07001421 ihold(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001422 if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
1423 &ceph_inode(inode)->i_pg_inv_work)) {
1424 dout("ceph_queue_invalidate %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001425 } else {
1426 dout("ceph_queue_invalidate %p failed\n", inode);
Sage Weil15a20152011-11-05 22:06:31 -07001427 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001428 }
1429}
1430
1431/*
Sage Weil355da1e2009-10-06 11:31:08 -07001432 * Invalidate inode pages in a worker thread. (This can't be done
1433 * in the message handler context.)
1434 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001435static void ceph_invalidate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001436{
1437 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1438 i_pg_inv_work);
1439 struct inode *inode = &ci->vfs_inode;
1440 u32 orig_gen;
1441 int check = 0;
1442
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001443 mutex_lock(&ci->i_truncate_mutex);
Sage Weilbe655592011-11-30 09:47:09 -08001444 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001445 dout("invalidate_pages %p gen %d revoking %d\n", inode,
1446 ci->i_rdcache_gen, ci->i_rdcache_revoking);
Sage Weilcd045cb2010-11-04 11:05:05 -07001447 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Sage Weil355da1e2009-10-06 11:31:08 -07001448 /* nevermind! */
Sage Weilbe655592011-11-30 09:47:09 -08001449 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001450 mutex_unlock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001451 goto out;
1452 }
1453 orig_gen = ci->i_rdcache_gen;
Sage Weilbe655592011-11-30 09:47:09 -08001454 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001455
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001456 truncate_inode_pages(inode->i_mapping, 0);
Sage Weil355da1e2009-10-06 11:31:08 -07001457
Sage Weilbe655592011-11-30 09:47:09 -08001458 spin_lock(&ci->i_ceph_lock);
Sage Weilcd045cb2010-11-04 11:05:05 -07001459 if (orig_gen == ci->i_rdcache_gen &&
1460 orig_gen == ci->i_rdcache_revoking) {
Sage Weil355da1e2009-10-06 11:31:08 -07001461 dout("invalidate_pages %p gen %d successful\n", inode,
1462 ci->i_rdcache_gen);
Sage Weilcd045cb2010-11-04 11:05:05 -07001463 ci->i_rdcache_revoking--;
Sage Weil355da1e2009-10-06 11:31:08 -07001464 check = 1;
1465 } else {
Sage Weilcd045cb2010-11-04 11:05:05 -07001466 dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
1467 inode, orig_gen, ci->i_rdcache_gen,
1468 ci->i_rdcache_revoking);
Sage Weil355da1e2009-10-06 11:31:08 -07001469 }
Sage Weilbe655592011-11-30 09:47:09 -08001470 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001471 mutex_unlock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001472
1473 if (check)
1474 ceph_check_caps(ci, 0, NULL);
1475out:
1476 iput(inode);
1477}
1478
1479
1480/*
Yan, Zheng3f999692013-03-01 10:57:54 +08001481 * called by trunc_wq;
Sage Weil355da1e2009-10-06 11:31:08 -07001482 *
1483 * We also truncate in a separate thread as well.
1484 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08001485static void ceph_vmtruncate_work(struct work_struct *work)
Sage Weil355da1e2009-10-06 11:31:08 -07001486{
1487 struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
1488 i_vmtruncate_work);
1489 struct inode *inode = &ci->vfs_inode;
1490
1491 dout("vmtruncate_work %p\n", inode);
Yan, Zhengb415bf42013-07-02 12:40:19 +08001492 __ceph_do_pending_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001493 iput(inode);
1494}
1495
1496/*
Sage Weil3c6f6b72010-02-09 15:24:44 -08001497 * Queue an async vmtruncate. If we fail to queue work, we will handle
1498 * the truncation the next time we call __ceph_do_pending_vmtruncate.
1499 */
1500void ceph_queue_vmtruncate(struct inode *inode)
1501{
1502 struct ceph_inode_info *ci = ceph_inode(inode);
1503
Sage Weil15a20152011-11-05 22:06:31 -07001504 ihold(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -04001505
Cheng Renquan640ef792010-03-26 17:40:33 +08001506 if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
Sage Weil3c6f6b72010-02-09 15:24:44 -08001507 &ci->i_vmtruncate_work)) {
1508 dout("ceph_queue_vmtruncate %p\n", inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001509 } else {
1510 dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
1511 inode, ci->i_truncate_pending);
Sage Weil15a20152011-11-05 22:06:31 -07001512 iput(inode);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001513 }
1514}
1515
1516/*
Sage Weil355da1e2009-10-06 11:31:08 -07001517 * Make sure any pending truncation is applied before doing anything
1518 * that may depend on it.
1519 */
Yan, Zhengb415bf42013-07-02 12:40:19 +08001520void __ceph_do_pending_vmtruncate(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -07001521{
1522 struct ceph_inode_info *ci = ceph_inode(inode);
1523 u64 to;
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001524 int wrbuffer_refs, finish = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001525
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001526 mutex_lock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001527retry:
Sage Weilbe655592011-11-30 09:47:09 -08001528 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001529 if (ci->i_truncate_pending == 0) {
1530 dout("__do_pending_vmtruncate %p none pending\n", inode);
Sage Weilbe655592011-11-30 09:47:09 -08001531 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001532 mutex_unlock(&ci->i_truncate_mutex);
Sage Weil355da1e2009-10-06 11:31:08 -07001533 return;
1534 }
1535
1536 /*
1537 * make sure any dirty snapped pages are flushed before we
1538 * possibly truncate them.. so write AND block!
1539 */
1540 if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
1541 dout("__do_pending_vmtruncate %p flushing snaps first\n",
1542 inode);
Sage Weilbe655592011-11-30 09:47:09 -08001543 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001544 filemap_write_and_wait_range(&inode->i_data, 0,
1545 inode->i_sb->s_maxbytes);
1546 goto retry;
1547 }
1548
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001549 /* there should be no reader or writer */
1550 WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
1551
Sage Weil355da1e2009-10-06 11:31:08 -07001552 to = ci->i_truncate_size;
1553 wrbuffer_refs = ci->i_wrbuffer_ref;
1554 dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
1555 ci->i_truncate_pending, to);
Sage Weilbe655592011-11-30 09:47:09 -08001556 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001557
1558 truncate_inode_pages(inode->i_mapping, to);
1559
Sage Weilbe655592011-11-30 09:47:09 -08001560 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001561 if (to == ci->i_truncate_size) {
1562 ci->i_truncate_pending = 0;
1563 finish = 1;
1564 }
Sage Weilbe655592011-11-30 09:47:09 -08001565 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001566 if (!finish)
1567 goto retry;
Sage Weil355da1e2009-10-06 11:31:08 -07001568
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001569 mutex_unlock(&ci->i_truncate_mutex);
1570
Sage Weil355da1e2009-10-06 11:31:08 -07001571 if (wrbuffer_refs == 0)
1572 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
Yan, Zhenga85f50b2012-11-19 10:49:08 +08001573
1574 wake_up_all(&ci->i_cap_wq);
Sage Weil355da1e2009-10-06 11:31:08 -07001575}
1576
Sage Weil355da1e2009-10-06 11:31:08 -07001577/*
1578 * symlinks
1579 */
1580static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
1581{
1582 struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
1583 nd_set_link(nd, ci->i_symlink);
1584 return NULL;
1585}
1586
1587static const struct inode_operations ceph_symlink_iops = {
1588 .readlink = generic_readlink,
1589 .follow_link = ceph_sym_follow_link,
Yan, Zheng0b932672013-04-07 16:28:49 +08001590 .setattr = ceph_setattr,
1591 .getattr = ceph_getattr,
1592 .setxattr = ceph_setxattr,
1593 .getxattr = ceph_getxattr,
1594 .listxattr = ceph_listxattr,
1595 .removexattr = ceph_removexattr,
Sage Weil355da1e2009-10-06 11:31:08 -07001596};
1597
1598/*
1599 * setattr
1600 */
1601int ceph_setattr(struct dentry *dentry, struct iattr *attr)
1602{
1603 struct inode *inode = dentry->d_inode;
1604 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -07001605 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -07001606 const unsigned int ia_valid = attr->ia_valid;
1607 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001608 struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001609 int issued;
1610 int release = 0, dirtied = 0;
1611 int mask = 0;
1612 int err = 0;
Sage Weilfca65b42011-05-04 11:33:47 -07001613 int inode_dirty_flags = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001614
1615 if (ceph_snap(inode) != CEPH_NOSNAP)
1616 return -EROFS;
1617
Sage Weil355da1e2009-10-06 11:31:08 -07001618 err = inode_change_ok(inode, attr);
1619 if (err != 0)
1620 return err;
1621
1622 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
1623 USE_AUTH_MDS);
1624 if (IS_ERR(req))
1625 return PTR_ERR(req);
1626
Sage Weilbe655592011-11-30 09:47:09 -08001627 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001628 issued = __ceph_caps_issued(ci, NULL);
1629 dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
1630
1631 if (ia_valid & ATTR_UID) {
1632 dout("setattr %p uid %d -> %d\n", inode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08001633 from_kuid(&init_user_ns, inode->i_uid),
1634 from_kuid(&init_user_ns, attr->ia_uid));
Sage Weil355da1e2009-10-06 11:31:08 -07001635 if (issued & CEPH_CAP_AUTH_EXCL) {
1636 inode->i_uid = attr->ia_uid;
1637 dirtied |= CEPH_CAP_AUTH_EXCL;
1638 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
Eric W. Biedermanab871b92013-01-31 03:40:12 -08001639 !uid_eq(attr->ia_uid, inode->i_uid)) {
1640 req->r_args.setattr.uid = cpu_to_le32(
1641 from_kuid(&init_user_ns, attr->ia_uid));
Sage Weil355da1e2009-10-06 11:31:08 -07001642 mask |= CEPH_SETATTR_UID;
1643 release |= CEPH_CAP_AUTH_SHARED;
1644 }
1645 }
1646 if (ia_valid & ATTR_GID) {
1647 dout("setattr %p gid %d -> %d\n", inode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08001648 from_kgid(&init_user_ns, inode->i_gid),
1649 from_kgid(&init_user_ns, attr->ia_gid));
Sage Weil355da1e2009-10-06 11:31:08 -07001650 if (issued & CEPH_CAP_AUTH_EXCL) {
1651 inode->i_gid = attr->ia_gid;
1652 dirtied |= CEPH_CAP_AUTH_EXCL;
1653 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
Eric W. Biedermanab871b92013-01-31 03:40:12 -08001654 !gid_eq(attr->ia_gid, inode->i_gid)) {
1655 req->r_args.setattr.gid = cpu_to_le32(
1656 from_kgid(&init_user_ns, attr->ia_gid));
Sage Weil355da1e2009-10-06 11:31:08 -07001657 mask |= CEPH_SETATTR_GID;
1658 release |= CEPH_CAP_AUTH_SHARED;
1659 }
1660 }
1661 if (ia_valid & ATTR_MODE) {
1662 dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
1663 attr->ia_mode);
1664 if (issued & CEPH_CAP_AUTH_EXCL) {
1665 inode->i_mode = attr->ia_mode;
1666 dirtied |= CEPH_CAP_AUTH_EXCL;
1667 } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
1668 attr->ia_mode != inode->i_mode) {
1669 req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
1670 mask |= CEPH_SETATTR_MODE;
1671 release |= CEPH_CAP_AUTH_SHARED;
1672 }
1673 }
1674
1675 if (ia_valid & ATTR_ATIME) {
1676 dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
1677 inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
1678 attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
1679 if (issued & CEPH_CAP_FILE_EXCL) {
1680 ci->i_time_warp_seq++;
1681 inode->i_atime = attr->ia_atime;
1682 dirtied |= CEPH_CAP_FILE_EXCL;
1683 } else if ((issued & CEPH_CAP_FILE_WR) &&
1684 timespec_compare(&inode->i_atime,
1685 &attr->ia_atime) < 0) {
1686 inode->i_atime = attr->ia_atime;
1687 dirtied |= CEPH_CAP_FILE_WR;
1688 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1689 !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
1690 ceph_encode_timespec(&req->r_args.setattr.atime,
1691 &attr->ia_atime);
1692 mask |= CEPH_SETATTR_ATIME;
1693 release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
1694 CEPH_CAP_FILE_WR;
1695 }
1696 }
1697 if (ia_valid & ATTR_MTIME) {
1698 dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
1699 inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
1700 attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
1701 if (issued & CEPH_CAP_FILE_EXCL) {
1702 ci->i_time_warp_seq++;
1703 inode->i_mtime = attr->ia_mtime;
1704 dirtied |= CEPH_CAP_FILE_EXCL;
1705 } else if ((issued & CEPH_CAP_FILE_WR) &&
1706 timespec_compare(&inode->i_mtime,
1707 &attr->ia_mtime) < 0) {
1708 inode->i_mtime = attr->ia_mtime;
1709 dirtied |= CEPH_CAP_FILE_WR;
1710 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1711 !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
1712 ceph_encode_timespec(&req->r_args.setattr.mtime,
1713 &attr->ia_mtime);
1714 mask |= CEPH_SETATTR_MTIME;
1715 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1716 CEPH_CAP_FILE_WR;
1717 }
1718 }
1719 if (ia_valid & ATTR_SIZE) {
1720 dout("setattr %p size %lld -> %lld\n", inode,
1721 inode->i_size, attr->ia_size);
1722 if (attr->ia_size > inode->i_sb->s_maxbytes) {
1723 err = -EINVAL;
1724 goto out;
1725 }
1726 if ((issued & CEPH_CAP_FILE_EXCL) &&
1727 attr->ia_size > inode->i_size) {
1728 inode->i_size = attr->ia_size;
Sage Weil355da1e2009-10-06 11:31:08 -07001729 inode->i_blocks =
1730 (attr->ia_size + (1 << 9) - 1) >> 9;
1731 inode->i_ctime = attr->ia_ctime;
1732 ci->i_reported_size = attr->ia_size;
1733 dirtied |= CEPH_CAP_FILE_EXCL;
1734 } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
1735 attr->ia_size != inode->i_size) {
1736 req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
1737 req->r_args.setattr.old_size =
1738 cpu_to_le64(inode->i_size);
1739 mask |= CEPH_SETATTR_SIZE;
1740 release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
1741 CEPH_CAP_FILE_WR;
1742 }
1743 }
1744
1745 /* these do nothing */
1746 if (ia_valid & ATTR_CTIME) {
1747 bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
1748 ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
1749 dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
1750 inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
1751 attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
1752 only ? "ctime only" : "ignored");
1753 inode->i_ctime = attr->ia_ctime;
1754 if (only) {
1755 /*
1756 * if kernel wants to dirty ctime but nothing else,
1757 * we need to choose a cap to dirty under, or do
1758 * a almost-no-op setattr
1759 */
1760 if (issued & CEPH_CAP_AUTH_EXCL)
1761 dirtied |= CEPH_CAP_AUTH_EXCL;
1762 else if (issued & CEPH_CAP_FILE_EXCL)
1763 dirtied |= CEPH_CAP_FILE_EXCL;
1764 else if (issued & CEPH_CAP_XATTR_EXCL)
1765 dirtied |= CEPH_CAP_XATTR_EXCL;
1766 else
1767 mask |= CEPH_SETATTR_CTIME;
1768 }
1769 }
1770 if (ia_valid & ATTR_FILE)
1771 dout("setattr %p ATTR_FILE ... hrm!\n", inode);
1772
1773 if (dirtied) {
Sage Weilfca65b42011-05-04 11:33:47 -07001774 inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied);
Sage Weil355da1e2009-10-06 11:31:08 -07001775 inode->i_ctime = CURRENT_TIME;
1776 }
1777
1778 release &= issued;
Sage Weilbe655592011-11-30 09:47:09 -08001779 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001780
Sage Weilfca65b42011-05-04 11:33:47 -07001781 if (inode_dirty_flags)
1782 __mark_inode_dirty(inode, inode_dirty_flags);
1783
Sage Weil355da1e2009-10-06 11:31:08 -07001784 if (mask) {
Sage Weil70b666c2011-05-27 09:24:26 -07001785 req->r_inode = inode;
1786 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001787 req->r_inode_drop = release;
1788 req->r_args.setattr.mask = cpu_to_le32(mask);
1789 req->r_num_caps = 1;
Sage Weil5f21c962011-07-26 11:30:29 -07001790 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -07001791 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -07001792 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001793 }
1794 dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
1795 ceph_cap_string(dirtied), mask);
1796
1797 ceph_mdsc_put_request(req);
Yan, Zhengb0d7c222013-08-12 21:42:15 -07001798 if (mask & CEPH_SETATTR_SIZE)
1799 __ceph_do_pending_vmtruncate(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001800 return err;
1801out:
Sage Weilbe655592011-11-30 09:47:09 -08001802 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -07001803 ceph_mdsc_put_request(req);
1804 return err;
1805}
1806
1807/*
1808 * Verify that we have a lease on the given mask. If not,
1809 * do a getattr against an mds.
1810 */
1811int ceph_do_getattr(struct inode *inode, int mask)
1812{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001813 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
1814 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -07001815 struct ceph_mds_request *req;
1816 int err;
1817
1818 if (ceph_snap(inode) == CEPH_SNAPDIR) {
1819 dout("do_getattr inode %p SNAPDIR\n", inode);
1820 return 0;
1821 }
1822
Sage Weilb7495fc2010-11-09 12:43:12 -08001823 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 -07001824 if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
1825 return 0;
1826
1827 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1828 if (IS_ERR(req))
1829 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -07001830 req->r_inode = inode;
1831 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001832 req->r_num_caps = 1;
1833 req->r_args.getattr.mask = cpu_to_le32(mask);
1834 err = ceph_mdsc_do_request(mdsc, NULL, req);
1835 ceph_mdsc_put_request(req);
1836 dout("do_getattr result=%d\n", err);
1837 return err;
1838}
1839
1840
1841/*
1842 * Check inode permissions. We verify we have a valid value for
1843 * the AUTH cap, then call the generic handler.
1844 */
Al Viro10556cb2011-06-20 19:28:19 -04001845int ceph_permission(struct inode *inode, int mask)
Sage Weil355da1e2009-10-06 11:31:08 -07001846{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001847 int err;
1848
Al Viro10556cb2011-06-20 19:28:19 -04001849 if (mask & MAY_NOT_BLOCK)
Nick Pigginb74c79e2011-01-07 17:49:58 +11001850 return -ECHILD;
1851
1852 err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
Sage Weil355da1e2009-10-06 11:31:08 -07001853
1854 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001855 err = generic_permission(inode, mask);
Sage Weil355da1e2009-10-06 11:31:08 -07001856 return err;
1857}
1858
1859/*
1860 * Get all attributes. Hopefully somedata we'll have a statlite()
1861 * and can limit the fields we require to be accurate.
1862 */
1863int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
1864 struct kstat *stat)
1865{
1866 struct inode *inode = dentry->d_inode;
Sage Weil232d4b02009-10-21 11:21:49 -07001867 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -07001868 int err;
1869
1870 err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
1871 if (!err) {
1872 generic_fillattr(inode, stat);
Yehuda Sadehad1fee92011-01-21 16:44:03 -08001873 stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
Sage Weil355da1e2009-10-06 11:31:08 -07001874 if (ceph_snap(inode) != CEPH_NOSNAP)
1875 stat->dev = ceph_snap(inode);
1876 else
1877 stat->dev = 0;
Sage Weil232d4b02009-10-21 11:21:49 -07001878 if (S_ISDIR(inode->i_mode)) {
Yehuda Sadeh1c1266b2011-01-12 16:53:27 -08001879 if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
1880 RBYTES))
1881 stat->size = ci->i_rbytes;
1882 else
1883 stat->size = ci->i_files + ci->i_subdirs;
Sage Weil232d4b02009-10-21 11:21:49 -07001884 stat->blocks = 0;
Sage Weil355da1e2009-10-06 11:31:08 -07001885 stat->blksize = 65536;
Sage Weil232d4b02009-10-21 11:21:49 -07001886 }
Sage Weil355da1e2009-10-06 11:31:08 -07001887 }
1888 return err;
1889}