| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
#include <linux/ceph/ceph_debug.h> |
| 3 |
|
| 4 |
#include <linux/spinlock.h> |
| 5 |
#include <linux/namei.h> |
| 6 |
#include <linux/slab.h> |
| 7 |
#include <linux/sched.h> |
| 8 |
#include <linux/xattr.h> |
| 9 |
|
| 10 |
#include "super.h" |
| 11 |
#include "mds_client.h" |
| 12 |
#include "crypto.h" |
| 13 |
|
| 14 |
/* |
| 15 |
* Directory operations: readdir, lookup, create, link, unlink, |
| 16 |
* rename, etc. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* Ceph MDS operations are specified in terms of a base ino and |
| 21 |
* relative path. Thus, the client can specify an operation on a |
| 22 |
* specific inode (e.g., a getattr due to fstat(2)), or as a path |
| 23 |
* relative to, say, the root directory. |
| 24 |
* |
| 25 |
* Normally, we limit ourselves to strict inode ops (no path component) |
| 26 |
* or dentry operations (a single path component relative to an ino). The |
| 27 |
* exception to this is open_root_dentry(), which will open the mount |
| 28 |
* point by name. |
| 29 |
*/ |
| 30 |
|
| 31 |
const struct dentry_operations ceph_dentry_ops; |
| 32 |
|
| 33 |
static bool __dentry_lease_is_valid(struct ceph_dentry_info *di); |
| 34 |
static int __dir_lease_try_check(const struct dentry *dentry); |
| 35 |
|
| 36 |
/* |
| 37 |
* Initialize ceph dentry state. |
| 38 |
*/ |
| 39 |
static int ceph_d_init(struct dentry *dentry) |
| 40 |
{ |
| 41 |
struct ceph_dentry_info *di; |
| 42 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb); |
| 43 |
|
| 44 |
di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL); |
| 45 |
if (!di) |
| 46 |
return -ENOMEM; /* oh well */ |
| 47 |
|
| 48 |
di->dentry = dentry; |
| 49 |
di->lease_session = NULL; |
| 50 |
di->time = jiffies; |
| 51 |
dentry->d_fsdata = di; |
| 52 |
INIT_LIST_HEAD(&di->lease_list); |
| 53 |
|
| 54 |
atomic64_inc(&mdsc->metric.total_dentries); |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* for f_pos for readdir: |
| 61 |
* - hash order: |
| 62 |
* (0xff << 52) | ((24 bits hash) << 28) | |
| 63 |
* (the nth entry has hash collision); |
| 64 |
* - frag+name order; |
| 65 |
* ((frag value) << 28) | (the nth entry in frag); |
| 66 |
*/ |
| 67 |
#define OFFSET_BITS 28 |
| 68 |
#define OFFSET_MASK ((1 << OFFSET_BITS) - 1) |
| 69 |
#define HASH_ORDER (0xffull << (OFFSET_BITS + 24)) |
| 70 |
loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order) |
| 71 |
{ |
| 72 |
loff_t fpos = ((loff_t)high << 28) | (loff_t)off; |
| 73 |
if (hash_order) |
| 74 |
fpos |= HASH_ORDER; |
| 75 |
return fpos; |
| 76 |
} |
| 77 |
|
| 78 |
static bool is_hash_order(loff_t p) |
| 79 |
{ |
| 80 |
return (p & HASH_ORDER) == HASH_ORDER; |
| 81 |
} |
| 82 |
|
| 83 |
static unsigned fpos_frag(loff_t p) |
| 84 |
{ |
| 85 |
return p >> OFFSET_BITS; |
| 86 |
} |
| 87 |
|
| 88 |
static unsigned fpos_hash(loff_t p) |
| 89 |
{ |
| 90 |
return ceph_frag_value(fpos_frag(p)); |
| 91 |
} |
| 92 |
|
| 93 |
static unsigned fpos_off(loff_t p) |
| 94 |
{ |
| 95 |
return p & OFFSET_MASK; |
| 96 |
} |
| 97 |
|
| 98 |
static int fpos_cmp(loff_t l, loff_t r) |
| 99 |
{ |
| 100 |
int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r)); |
| 101 |
if (v) |
| 102 |
return v; |
| 103 |
return (int)(fpos_off(l) - fpos_off(r)); |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* make note of the last dentry we read, so we can |
| 108 |
* continue at the same lexicographical point, |
| 109 |
* regardless of what dir changes take place on the |
| 110 |
* server. |
| 111 |
*/ |
| 112 |
static int note_last_dentry(struct ceph_fs_client *fsc, |
| 113 |
struct ceph_dir_file_info *dfi, |
| 114 |
const char *name, |
| 115 |
int len, unsigned next_offset) |
| 116 |
{ |
| 117 |
char *buf = kmalloc(len+1, GFP_KERNEL); |
| 118 |
if (!buf) |
| 119 |
return -ENOMEM; |
| 120 |
kfree(dfi->last_name); |
| 121 |
dfi->last_name = buf; |
| 122 |
memcpy(dfi->last_name, name, len); |
| 123 |
dfi->last_name[len] = 0; |
| 124 |
dfi->next_offset = next_offset; |
| 125 |
doutc(fsc->client, "'%s'\n", dfi->last_name); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static struct dentry * |
| 131 |
__dcache_find_get_entry(struct dentry *parent, u64 idx, |
| 132 |
struct ceph_readdir_cache_control *cache_ctl) |
| 133 |
{ |
| 134 |
struct inode *dir = d_inode(parent); |
| 135 |
struct ceph_client *cl = ceph_inode_to_client(dir); |
| 136 |
struct dentry *dentry; |
| 137 |
unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1; |
| 138 |
loff_t ptr_pos = idx * sizeof(struct dentry *); |
| 139 |
pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT; |
| 140 |
|
| 141 |
if (ptr_pos >= i_size_read(dir)) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
if (!cache_ctl->folio || ptr_pgoff != cache_ctl->folio->index) { |
| 145 |
ceph_readdir_cache_release(cache_ctl); |
| 146 |
cache_ctl->folio = filemap_lock_folio(&dir->i_data, ptr_pgoff); |
| 147 |
if (IS_ERR(cache_ctl->folio)) { |
| 148 |
cache_ctl->folio = NULL; |
| 149 |
doutc(cl, " folio %lu not found\n", ptr_pgoff); |
| 150 |
return ERR_PTR(-EAGAIN); |
| 151 |
} |
| 152 |
/* reading/filling the cache are serialized by |
| 153 |
i_rwsem, no need to use folio lock */ |
| 154 |
folio_unlock(cache_ctl->folio); |
| 155 |
cache_ctl->dentries = kmap_local_folio(cache_ctl->folio, 0); |
| 156 |
} |
| 157 |
|
| 158 |
cache_ctl->index = idx & idx_mask; |
| 159 |
|
| 160 |
rcu_read_lock(); |
| 161 |
spin_lock(&parent->d_lock); |
| 162 |
/* check i_size again here, because empty directory can be |
| 163 |
* marked as complete while not holding the i_rwsem. */ |
| 164 |
if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir)) |
| 165 |
dentry = cache_ctl->dentries[cache_ctl->index]; |
| 166 |
else |
| 167 |
dentry = NULL; |
| 168 |
spin_unlock(&parent->d_lock); |
| 169 |
if (dentry && !lockref_get_not_dead(&dentry->d_lockref)) |
| 170 |
dentry = NULL; |
| 171 |
rcu_read_unlock(); |
| 172 |
return dentry ? : ERR_PTR(-EAGAIN); |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* When possible, we try to satisfy a readdir by peeking at the |
| 177 |
* dcache. We make this work by carefully ordering dentries on |
| 178 |
* d_children when we initially get results back from the MDS, and |
| 179 |
* falling back to a "normal" sync readdir if any dentries in the dir |
| 180 |
* are dropped. |
| 181 |
* |
| 182 |
* Complete dir indicates that we have all dentries in the dir. It is |
| 183 |
* defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by |
| 184 |
* the MDS if/when the directory is modified). |
| 185 |
*/ |
| 186 |
static int __dcache_readdir(struct file *file, struct dir_context *ctx, |
| 187 |
int shared_gen) |
| 188 |
{ |
| 189 |
struct ceph_dir_file_info *dfi = file->private_data; |
| 190 |
struct dentry *parent = file->f_path.dentry; |
| 191 |
struct inode *dir = d_inode(parent); |
| 192 |
struct ceph_fs_client *fsc = ceph_inode_to_fs_client(dir); |
| 193 |
struct ceph_client *cl = ceph_inode_to_client(dir); |
| 194 |
struct dentry *dentry, *last = NULL; |
| 195 |
struct ceph_dentry_info *di; |
| 196 |
struct ceph_readdir_cache_control cache_ctl = {}; |
| 197 |
u64 idx = 0; |
| 198 |
int err = 0; |
| 199 |
|
| 200 |
doutc(cl, "%p %llx.%llx v%u at %llx\n", dir, ceph_vinop(dir), |
| 201 |
(unsigned)shared_gen, ctx->pos); |
| 202 |
|
| 203 |
/* search start position */ |
| 204 |
if (ctx->pos > 2) { |
| 205 |
u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *)); |
| 206 |
while (count > 0) { |
| 207 |
u64 step = count >> 1; |
| 208 |
dentry = __dcache_find_get_entry(parent, idx + step, |
| 209 |
&cache_ctl); |
| 210 |
if (!dentry) { |
| 211 |
/* use linear search */ |
| 212 |
idx = 0; |
| 213 |
break; |
| 214 |
} |
| 215 |
if (IS_ERR(dentry)) { |
| 216 |
err = PTR_ERR(dentry); |
| 217 |
goto out; |
| 218 |
} |
| 219 |
di = ceph_dentry(dentry); |
| 220 |
spin_lock(&dentry->d_lock); |
| 221 |
if (fpos_cmp(di->offset, ctx->pos) < 0) { |
| 222 |
idx += step + 1; |
| 223 |
count -= step + 1; |
| 224 |
} else { |
| 225 |
count = step; |
| 226 |
} |
| 227 |
spin_unlock(&dentry->d_lock); |
| 228 |
dput(dentry); |
| 229 |
} |
| 230 |
|
| 231 |
doutc(cl, "%p %llx.%llx cache idx %llu\n", dir, |
| 232 |
ceph_vinop(dir), idx); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
for (;;) { |
| 237 |
bool emit_dentry = false; |
| 238 |
dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl); |
| 239 |
if (!dentry) { |
| 240 |
dfi->file_info.flags |= CEPH_F_ATEND; |
| 241 |
err = 0; |
| 242 |
break; |
| 243 |
} |
| 244 |
if (IS_ERR(dentry)) { |
| 245 |
err = PTR_ERR(dentry); |
| 246 |
goto out; |
| 247 |
} |
| 248 |
|
| 249 |
spin_lock(&dentry->d_lock); |
| 250 |
di = ceph_dentry(dentry); |
| 251 |
if (d_unhashed(dentry) || |
| 252 |
d_really_is_negative(dentry) || |
| 253 |
di->lease_shared_gen != shared_gen || |
| 254 |
((dentry->d_flags & DCACHE_NOKEY_NAME) && |
| 255 |
fscrypt_has_encryption_key(dir))) { |
| 256 |
spin_unlock(&dentry->d_lock); |
| 257 |
dput(dentry); |
| 258 |
err = -EAGAIN; |
| 259 |
goto out; |
| 260 |
} |
| 261 |
if (fpos_cmp(ctx->pos, di->offset) <= 0) { |
| 262 |
__ceph_dentry_dir_lease_touch(di); |
| 263 |
emit_dentry = true; |
| 264 |
} |
| 265 |
spin_unlock(&dentry->d_lock); |
| 266 |
|
| 267 |
if (emit_dentry) { |
| 268 |
doutc(cl, " %llx dentry %p %pd %p\n", di->offset, |
| 269 |
dentry, dentry, d_inode(dentry)); |
| 270 |
ctx->pos = di->offset; |
| 271 |
if (!dir_emit(ctx, dentry->d_name.name, |
| 272 |
dentry->d_name.len, ceph_present_inode(d_inode(dentry)), |
| 273 |
d_inode(dentry)->i_mode >> 12)) { |
| 274 |
dput(dentry); |
| 275 |
err = 0; |
| 276 |
break; |
| 277 |
} |
| 278 |
ctx->pos++; |
| 279 |
|
| 280 |
if (last) |
| 281 |
dput(last); |
| 282 |
last = dentry; |
| 283 |
} else { |
| 284 |
dput(dentry); |
| 285 |
} |
| 286 |
} |
| 287 |
out: |
| 288 |
ceph_readdir_cache_release(&cache_ctl); |
| 289 |
if (last) { |
| 290 |
int ret; |
| 291 |
di = ceph_dentry(last); |
| 292 |
ret = note_last_dentry(fsc, dfi, last->d_name.name, |
| 293 |
last->d_name.len, |
| 294 |
fpos_off(di->offset) + 1); |
| 295 |
if (ret < 0) |
| 296 |
err = ret; |
| 297 |
dput(last); |
| 298 |
/* last_name no longer match cache index */ |
| 299 |
if (dfi->readdir_cache_idx >= 0) { |
| 300 |
dfi->readdir_cache_idx = -1; |
| 301 |
dfi->dir_release_count = 0; |
| 302 |
} |
| 303 |
} |
| 304 |
return err; |
| 305 |
} |
| 306 |
|
| 307 |
static bool need_send_readdir(struct ceph_dir_file_info *dfi, loff_t pos) |
| 308 |
{ |
| 309 |
if (!dfi->last_readdir) |
| 310 |
return true; |
| 311 |
if (is_hash_order(pos)) |
| 312 |
return !ceph_frag_contains_value(dfi->frag, fpos_hash(pos)); |
| 313 |
else |
| 314 |
return dfi->frag != fpos_frag(pos); |
| 315 |
} |
| 316 |
|
| 317 |
static int ceph_readdir(struct file *file, struct dir_context *ctx) |
| 318 |
{ |
| 319 |
struct ceph_dir_file_info *dfi = file->private_data; |
| 320 |
struct inode *inode = file_inode(file); |
| 321 |
struct ceph_inode_info *ci = ceph_inode(inode); |
| 322 |
struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); |
| 323 |
struct ceph_mds_client *mdsc = fsc->mdsc; |
| 324 |
struct ceph_client *cl = fsc->client; |
| 325 |
int i; |
| 326 |
int err; |
| 327 |
unsigned frag = -1; |
| 328 |
struct ceph_mds_reply_info_parsed *rinfo; |
| 329 |
|
| 330 |
doutc(cl, "%p %llx.%llx file %p pos %llx\n", inode, |
| 331 |
ceph_vinop(inode), file, ctx->pos); |
| 332 |
if (dfi->file_info.flags & CEPH_F_ATEND) |
| 333 |
return 0; |
| 334 |
|
| 335 |
/* always start with . and .. */ |
| 336 |
if (ctx->pos == 0) { |
| 337 |
doutc(cl, "%p %llx.%llx off 0 -> '.'\n", inode, |
| 338 |
ceph_vinop(inode)); |
| 339 |
if (!dir_emit(ctx, ".", 1, ceph_present_inode(inode), |
| 340 |
inode->i_mode >> 12)) |
| 341 |
return 0; |
| 342 |
ctx->pos = 1; |
| 343 |
} |
| 344 |
if (ctx->pos == 1) { |
| 345 |
u64 ino; |
| 346 |
struct dentry *dentry = file->f_path.dentry; |
| 347 |
|
| 348 |
spin_lock(&dentry->d_lock); |
| 349 |
ino = ceph_present_inode(dentry->d_parent->d_inode); |
| 350 |
spin_unlock(&dentry->d_lock); |
| 351 |
|
| 352 |
doutc(cl, "%p %llx.%llx off 1 -> '..'\n", inode, |
| 353 |
ceph_vinop(inode)); |
| 354 |
if (!dir_emit(ctx, "..", 2, ino, inode->i_mode >> 12)) |
| 355 |
return 0; |
| 356 |
ctx->pos = 2; |
| 357 |
} |
| 358 |
|
| 359 |
err = ceph_fscrypt_prepare_readdir(inode); |
| 360 |
if (err < 0) |
| 361 |
return err; |
| 362 |
|
| 363 |
spin_lock(&ci->i_ceph_lock); |
| 364 |
/* request Fx cap. if have Fx, we don't need to release Fs cap |
| 365 |
* for later create/unlink. */ |
| 366 |
__ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_WR); |
| 367 |
/* can we use the dcache? */ |
| 368 |
if (ceph_test_mount_opt(fsc, DCACHE) && |
| 369 |
!ceph_test_mount_opt(fsc, NOASYNCREADDIR) && |
| 370 |
ceph_snap(inode) != CEPH_SNAPDIR && |
| 371 |
__ceph_dir_is_complete_ordered(ci) && |
| 372 |
__ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) { |
| 373 |
int shared_gen = atomic_read(&ci->i_shared_gen); |
| 374 |
|
| 375 |
spin_unlock(&ci->i_ceph_lock); |
| 376 |
err = __dcache_readdir(file, ctx, shared_gen); |
| 377 |
if (err != -EAGAIN) |
| 378 |
return err; |
| 379 |
} else { |
| 380 |
spin_unlock(&ci->i_ceph_lock); |
| 381 |
} |
| 382 |
|
| 383 |
/* proceed with a normal readdir */ |
| 384 |
more: |
| 385 |
/* do we have the correct frag content buffered? */ |
| 386 |
if (need_send_readdir(dfi, ctx->pos)) { |
| 387 |
struct ceph_mds_request *req; |
| 388 |
int op = ceph_snap(inode) == CEPH_SNAPDIR ? |
| 389 |
CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR; |
| 390 |
|
| 391 |
/* discard old result, if any */ |
| 392 |
if (dfi->last_readdir) { |
| 393 |
ceph_mdsc_put_request(dfi->last_readdir); |
| 394 |
dfi->last_readdir = NULL; |
| 395 |
} |
| 396 |
|
| 397 |
if (is_hash_order(ctx->pos)) { |
| 398 |
/* fragtree isn't always accurate. choose frag |
| 399 |
* based on previous reply when possible. */ |
| 400 |
if (frag == (unsigned)-1) |
| 401 |
frag = ceph_choose_frag(ci, fpos_hash(ctx->pos), |
| 402 |
NULL, NULL); |
| 403 |
} else { |
| 404 |
frag = fpos_frag(ctx->pos); |
| 405 |
} |
| 406 |
|
| 407 |
doutc(cl, "fetching %p %llx.%llx frag %x offset '%s'\n", |
| 408 |
inode, ceph_vinop(inode), frag, dfi->last_name); |
| 409 |
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 410 |
if (IS_ERR(req)) |
| 411 |
return PTR_ERR(req); |
| 412 |
|
| 413 |
err = ceph_alloc_readdir_reply_buffer(req, inode); |
| 414 |
if (err) { |
| 415 |
ceph_mdsc_put_request(req); |
| 416 |
return err; |
| 417 |
} |
| 418 |
/* hints to request -> mds selection code */ |
| 419 |
req->r_direct_mode = USE_AUTH_MDS; |
| 420 |
if (op == CEPH_MDS_OP_READDIR) { |
| 421 |
req->r_direct_hash = ceph_frag_value(frag); |
| 422 |
__set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags); |
| 423 |
req->r_inode_drop = CEPH_CAP_FILE_EXCL; |
| 424 |
} |
| 425 |
if (dfi->last_name) { |
| 426 |
int len = strlen(dfi->last_name); |
| 427 |
|
| 428 |
req->r_path2 = kzalloc(NAME_MAX + 1, GFP_KERNEL); |
| 429 |
if (!req->r_path2) { |
| 430 |
ceph_mdsc_put_request(req); |
| 431 |
return -ENOMEM; |
| 432 |
} |
| 433 |
memcpy(req->r_path2, dfi->last_name, len); |
| 434 |
|
| 435 |
err = ceph_encode_encrypted_dname(inode, req->r_path2, len); |
| 436 |
if (err < 0) { |
| 437 |
ceph_mdsc_put_request(req); |
| 438 |
return err; |
| 439 |
} |
| 440 |
} else if (is_hash_order(ctx->pos)) { |
| 441 |
req->r_args.readdir.offset_hash = |
| 442 |
cpu_to_le32(fpos_hash(ctx->pos)); |
| 443 |
} |
| 444 |
|
| 445 |
req->r_dir_release_cnt = dfi->dir_release_count; |
| 446 |
req->r_dir_ordered_cnt = dfi->dir_ordered_count; |
| 447 |
req->r_readdir_cache_idx = dfi->readdir_cache_idx; |
| 448 |
req->r_readdir_offset = dfi->next_offset; |
| 449 |
req->r_args.readdir.frag = cpu_to_le32(frag); |
| 450 |
req->r_args.readdir.flags = |
| 451 |
cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS); |
| 452 |
|
| 453 |
req->r_inode = inode; |
| 454 |
ihold(inode); |
| 455 |
req->r_dentry = dget(file->f_path.dentry); |
| 456 |
err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 457 |
if (err < 0) { |
| 458 |
ceph_mdsc_put_request(req); |
| 459 |
return err; |
| 460 |
} |
| 461 |
doutc(cl, "%p %llx.%llx got and parsed readdir result=%d" |
| 462 |
"on frag %x, end=%d, complete=%d, hash_order=%d\n", |
| 463 |
inode, ceph_vinop(inode), err, frag, |
| 464 |
(int)req->r_reply_info.dir_end, |
| 465 |
(int)req->r_reply_info.dir_complete, |
| 466 |
(int)req->r_reply_info.hash_order); |
| 467 |
|
| 468 |
rinfo = &req->r_reply_info; |
| 469 |
if (le32_to_cpu(rinfo->dir_dir->frag) != frag) { |
| 470 |
frag = le32_to_cpu(rinfo->dir_dir->frag); |
| 471 |
if (!rinfo->hash_order) { |
| 472 |
dfi->next_offset = req->r_readdir_offset; |
| 473 |
/* adjust ctx->pos to beginning of frag */ |
| 474 |
ctx->pos = ceph_make_fpos(frag, |
| 475 |
dfi->next_offset, |
| 476 |
false); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
dfi->frag = frag; |
| 481 |
dfi->last_readdir = req; |
| 482 |
|
| 483 |
if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) { |
| 484 |
dfi->readdir_cache_idx = req->r_readdir_cache_idx; |
| 485 |
if (dfi->readdir_cache_idx < 0) { |
| 486 |
/* preclude from marking dir ordered */ |
| 487 |
dfi->dir_ordered_count = 0; |
| 488 |
} else if (ceph_frag_is_leftmost(frag) && |
| 489 |
dfi->next_offset == 2) { |
| 490 |
/* note dir version at start of readdir so |
| 491 |
* we can tell if any dentries get dropped */ |
| 492 |
dfi->dir_release_count = req->r_dir_release_cnt; |
| 493 |
dfi->dir_ordered_count = req->r_dir_ordered_cnt; |
| 494 |
} |
| 495 |
} else { |
| 496 |
doutc(cl, "%p %llx.%llx !did_prepopulate\n", inode, |
| 497 |
ceph_vinop(inode)); |
| 498 |
/* disable readdir cache */ |
| 499 |
dfi->readdir_cache_idx = -1; |
| 500 |
/* preclude from marking dir complete */ |
| 501 |
dfi->dir_release_count = 0; |
| 502 |
} |
| 503 |
|
| 504 |
/* note next offset and last dentry name */ |
| 505 |
if (rinfo->dir_nr > 0) { |
| 506 |
struct ceph_mds_reply_dir_entry *rde = |
| 507 |
rinfo->dir_entries + (rinfo->dir_nr-1); |
| 508 |
unsigned next_offset = req->r_reply_info.dir_end ? |
| 509 |
2 : (fpos_off(rde->offset) + 1); |
| 510 |
err = note_last_dentry(fsc, dfi, rde->name, |
| 511 |
rde->name_len, next_offset); |
| 512 |
if (err) { |
| 513 |
ceph_mdsc_put_request(dfi->last_readdir); |
| 514 |
dfi->last_readdir = NULL; |
| 515 |
return err; |
| 516 |
} |
| 517 |
} else if (req->r_reply_info.dir_end) { |
| 518 |
dfi->next_offset = 2; |
| 519 |
/* keep last name */ |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
rinfo = &dfi->last_readdir->r_reply_info; |
| 524 |
doutc(cl, "%p %llx.%llx frag %x num %d pos %llx chunk first %llx\n", |
| 525 |
inode, ceph_vinop(inode), dfi->frag, rinfo->dir_nr, ctx->pos, |
| 526 |
rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL); |
| 527 |
|
| 528 |
i = 0; |
| 529 |
/* search start position */ |
| 530 |
if (rinfo->dir_nr > 0) { |
| 531 |
int step, nr = rinfo->dir_nr; |
| 532 |
while (nr > 0) { |
| 533 |
step = nr >> 1; |
| 534 |
if (rinfo->dir_entries[i + step].offset < ctx->pos) { |
| 535 |
i += step + 1; |
| 536 |
nr -= step + 1; |
| 537 |
} else { |
| 538 |
nr = step; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
for (; i < rinfo->dir_nr; i++) { |
| 543 |
struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
| 544 |
|
| 545 |
if (rde->offset < ctx->pos) { |
| 546 |
pr_warn_client(cl, |
| 547 |
"%p %llx.%llx rde->offset 0x%llx ctx->pos 0x%llx\n", |
| 548 |
inode, ceph_vinop(inode), rde->offset, ctx->pos); |
| 549 |
return -EIO; |
| 550 |
} |
| 551 |
|
| 552 |
if (WARN_ON_ONCE(!rde->inode.in)) |
| 553 |
return -EIO; |
| 554 |
|
| 555 |
ctx->pos = rde->offset; |
| 556 |
doutc(cl, "%p %llx.%llx (%d/%d) -> %llx '%.*s' %p\n", inode, |
| 557 |
ceph_vinop(inode), i, rinfo->dir_nr, ctx->pos, |
| 558 |
rde->name_len, rde->name, &rde->inode.in); |
| 559 |
|
| 560 |
if (!dir_emit(ctx, rde->name, rde->name_len, |
| 561 |
ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)), |
| 562 |
le32_to_cpu(rde->inode.in->mode) >> 12)) { |
| 563 |
/* |
| 564 |
* NOTE: Here no need to put the 'dfi->last_readdir', |
| 565 |
* because when dir_emit stops us it's most likely |
| 566 |
* doesn't have enough memory, etc. So for next readdir |
| 567 |
* it will continue. |
| 568 |
*/ |
| 569 |
doutc(cl, "filldir stopping us...\n"); |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
/* Reset the lengths to their original allocated vals */ |
| 574 |
ctx->pos++; |
| 575 |
} |
| 576 |
|
| 577 |
ceph_mdsc_put_request(dfi->last_readdir); |
| 578 |
dfi->last_readdir = NULL; |
| 579 |
|
| 580 |
if (dfi->next_offset > 2) { |
| 581 |
frag = dfi->frag; |
| 582 |
goto more; |
| 583 |
} |
| 584 |
|
| 585 |
/* more frags? */ |
| 586 |
if (!ceph_frag_is_rightmost(dfi->frag)) { |
| 587 |
frag = ceph_frag_next(dfi->frag); |
| 588 |
if (is_hash_order(ctx->pos)) { |
| 589 |
loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag), |
| 590 |
dfi->next_offset, true); |
| 591 |
if (new_pos > ctx->pos) |
| 592 |
ctx->pos = new_pos; |
| 593 |
/* keep last_name */ |
| 594 |
} else { |
| 595 |
ctx->pos = ceph_make_fpos(frag, dfi->next_offset, |
| 596 |
false); |
| 597 |
kfree(dfi->last_name); |
| 598 |
dfi->last_name = NULL; |
| 599 |
} |
| 600 |
doutc(cl, "%p %llx.%llx next frag is %x\n", inode, |
| 601 |
ceph_vinop(inode), frag); |
| 602 |
goto more; |
| 603 |
} |
| 604 |
dfi->file_info.flags |= CEPH_F_ATEND; |
| 605 |
|
| 606 |
/* |
| 607 |
* if dir_release_count still matches the dir, no dentries |
| 608 |
* were released during the whole readdir, and we should have |
| 609 |
* the complete dir contents in our cache. |
| 610 |
*/ |
| 611 |
if (atomic64_read(&ci->i_release_count) == |
| 612 |
dfi->dir_release_count) { |
| 613 |
spin_lock(&ci->i_ceph_lock); |
| 614 |
if (dfi->dir_ordered_count == |
| 615 |
atomic64_read(&ci->i_ordered_count)) { |
| 616 |
doutc(cl, " marking %p %llx.%llx complete and ordered\n", |
| 617 |
inode, ceph_vinop(inode)); |
| 618 |
/* use i_size to track number of entries in |
| 619 |
* readdir cache */ |
| 620 |
BUG_ON(dfi->readdir_cache_idx < 0); |
| 621 |
i_size_write(inode, dfi->readdir_cache_idx * |
| 622 |
sizeof(struct dentry*)); |
| 623 |
} else { |
| 624 |
doutc(cl, " marking %llx.%llx complete\n", |
| 625 |
ceph_vinop(inode)); |
| 626 |
} |
| 627 |
__ceph_dir_set_complete(ci, dfi->dir_release_count, |
| 628 |
dfi->dir_ordered_count); |
| 629 |
spin_unlock(&ci->i_ceph_lock); |
| 630 |
} |
| 631 |
doutc(cl, "%p %llx.%llx file %p done.\n", inode, ceph_vinop(inode), |
| 632 |
file); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
static void reset_readdir(struct ceph_dir_file_info *dfi) |
| 637 |
{ |
| 638 |
if (dfi->last_readdir) { |
| 639 |
ceph_mdsc_put_request(dfi->last_readdir); |
| 640 |
dfi->last_readdir = NULL; |
| 641 |
} |
| 642 |
kfree(dfi->last_name); |
| 643 |
dfi->last_name = NULL; |
| 644 |
dfi->dir_release_count = 0; |
| 645 |
dfi->readdir_cache_idx = -1; |
| 646 |
dfi->next_offset = 2; /* compensate for . and .. */ |
| 647 |
dfi->file_info.flags &= ~CEPH_F_ATEND; |
| 648 |
} |
| 649 |
|
| 650 |
/* |
| 651 |
* discard buffered readdir content on seekdir(0), or seek to new frag, |
| 652 |
* or seek prior to current chunk |
| 653 |
*/ |
| 654 |
static bool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos) |
| 655 |
{ |
| 656 |
struct ceph_mds_reply_info_parsed *rinfo; |
| 657 |
loff_t chunk_offset; |
| 658 |
if (new_pos == 0) |
| 659 |
return true; |
| 660 |
if (is_hash_order(new_pos)) { |
| 661 |
/* no need to reset last_name for a forward seek when |
| 662 |
* dentries are sorted in hash order */ |
| 663 |
} else if (dfi->frag != fpos_frag(new_pos)) { |
| 664 |
return true; |
| 665 |
} |
| 666 |
rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL; |
| 667 |
if (!rinfo || !rinfo->dir_nr) |
| 668 |
return true; |
| 669 |
chunk_offset = rinfo->dir_entries[0].offset; |
| 670 |
return new_pos < chunk_offset || |
| 671 |
is_hash_order(new_pos) != is_hash_order(chunk_offset); |
| 672 |
} |
| 673 |
|
| 674 |
static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence) |
| 675 |
{ |
| 676 |
struct ceph_dir_file_info *dfi = file->private_data; |
| 677 |
struct inode *inode = file->f_mapping->host; |
| 678 |
struct ceph_client *cl = ceph_inode_to_client(inode); |
| 679 |
loff_t retval; |
| 680 |
|
| 681 |
inode_lock(inode); |
| 682 |
retval = -EINVAL; |
| 683 |
switch (whence) { |
| 684 |
case SEEK_CUR: |
| 685 |
offset += file->f_pos; |
| 686 |
break; |
| 687 |
case SEEK_SET: |
| 688 |
break; |
| 689 |
case SEEK_END: |
| 690 |
retval = -EOPNOTSUPP; |
| 691 |
goto out; |
| 692 |
default: |
| 693 |
goto out; |
| 694 |
} |
| 695 |
|
| 696 |
if (offset >= 0) { |
| 697 |
if (need_reset_readdir(dfi, offset)) { |
| 698 |
doutc(cl, "%p %llx.%llx dropping %p content\n", |
| 699 |
inode, ceph_vinop(inode), file); |
| 700 |
reset_readdir(dfi); |
| 701 |
} else if (is_hash_order(offset) && offset > file->f_pos) { |
| 702 |
/* for hash offset, we don't know if a forward seek |
| 703 |
* is within same frag */ |
| 704 |
dfi->dir_release_count = 0; |
| 705 |
dfi->readdir_cache_idx = -1; |
| 706 |
} |
| 707 |
|
| 708 |
if (offset != file->f_pos) { |
| 709 |
file->f_pos = offset; |
| 710 |
dfi->file_info.flags &= ~CEPH_F_ATEND; |
| 711 |
} |
| 712 |
retval = offset; |
| 713 |
} |
| 714 |
out: |
| 715 |
inode_unlock(inode); |
| 716 |
return retval; |
| 717 |
} |
| 718 |
|
| 719 |
/* |
| 720 |
* Handle lookups for the hidden .snap directory. |
| 721 |
*/ |
| 722 |
struct dentry *ceph_handle_snapdir(struct ceph_mds_request *req, |
| 723 |
struct dentry *dentry) |
| 724 |
{ |
| 725 |
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb); |
| 726 |
struct inode *parent = d_inode(dentry->d_parent); /* we hold i_rwsem */ |
| 727 |
struct ceph_client *cl = ceph_inode_to_client(parent); |
| 728 |
|
| 729 |
/* .snap dir? */ |
| 730 |
if (ceph_snap(parent) == CEPH_NOSNAP && |
| 731 |
strcmp(dentry->d_name.name, fsc->mount_options->snapdir_name) == 0) { |
| 732 |
struct dentry *res; |
| 733 |
struct inode *inode = ceph_get_snapdir(parent); |
| 734 |
|
| 735 |
res = d_splice_alias(inode, dentry); |
| 736 |
doutc(cl, "ENOENT on snapdir %p '%pd', linking to " |
| 737 |
"snapdir %p %llx.%llx. Spliced dentry %p\n", |
| 738 |
dentry, dentry, inode, ceph_vinop(inode), res); |
| 739 |
if (res) |
| 740 |
dentry = res; |
| 741 |
} |
| 742 |
return dentry; |
| 743 |
} |
| 744 |
|
| 745 |
/* |
| 746 |
* Figure out final result of a lookup/open request. |
| 747 |
* |
| 748 |
* Mainly, make sure we return the final req->r_dentry (if it already |
| 749 |
* existed) in place of the original VFS-provided dentry when they |
| 750 |
* differ. |
| 751 |
* |
| 752 |
* Gracefully handle the case where the MDS replies with -ENOENT and |
| 753 |
* no trace (which it may do, at its discretion, e.g., if it doesn't |
| 754 |
* care to issue a lease on the negative dentry). |
| 755 |
*/ |
| 756 |
struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, |
| 757 |
struct dentry *dentry, int err) |
| 758 |
{ |
| 759 |
struct ceph_client *cl = req->r_mdsc->fsc->client; |
| 760 |
|
| 761 |
if (err == -ENOENT) { |
| 762 |
/* no trace? */ |
| 763 |
err = 0; |
| 764 |
if (!req->r_reply_info.head->is_dentry) { |
| 765 |
doutc(cl, |
| 766 |
"ENOENT and no trace, dentry %p inode %llx.%llx\n", |
| 767 |
dentry, ceph_vinop(d_inode(dentry))); |
| 768 |
if (d_really_is_positive(dentry)) { |
| 769 |
d_drop(dentry); |
| 770 |
err = -ENOENT; |
| 771 |
} else { |
| 772 |
if (d_unhashed(dentry)) |
| 773 |
d_add(dentry, NULL); |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
if (err) |
| 778 |
dentry = ERR_PTR(err); |
| 779 |
else if (dentry != req->r_dentry) |
| 780 |
dentry = dget(req->r_dentry); /* we got spliced */ |
| 781 |
else |
| 782 |
dentry = NULL; |
| 783 |
return dentry; |
| 784 |
} |
| 785 |
|
| 786 |
static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry) |
| 787 |
{ |
| 788 |
return ceph_ino(inode) == CEPH_INO_ROOT && |
| 789 |
strncmp(dentry->d_name.name, ".ceph", 5) == 0; |
| 790 |
} |
| 791 |
|
| 792 |
/* |
| 793 |
* Look up a single dir entry. If there is a lookup intent, inform |
| 794 |
* the MDS so that it gets our 'caps wanted' value in a single op. |
| 795 |
*/ |
| 796 |
static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, |
| 797 |
unsigned int flags) |
| 798 |
{ |
| 799 |
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb); |
| 800 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); |
| 801 |
struct ceph_client *cl = fsc->client; |
| 802 |
struct ceph_mds_request *req; |
| 803 |
int op; |
| 804 |
int mask; |
| 805 |
int err; |
| 806 |
|
| 807 |
doutc(cl, "%p %llx.%llx/'%pd' dentry %p\n", dir, ceph_vinop(dir), |
| 808 |
dentry, dentry); |
| 809 |
|
| 810 |
if (dentry->d_name.len > NAME_MAX) |
| 811 |
return ERR_PTR(-ENAMETOOLONG); |
| 812 |
|
| 813 |
if (IS_ENCRYPTED(dir)) { |
| 814 |
bool had_key = fscrypt_has_encryption_key(dir); |
| 815 |
|
| 816 |
err = fscrypt_prepare_lookup_partial(dir, dentry); |
| 817 |
if (err < 0) |
| 818 |
return ERR_PTR(err); |
| 819 |
|
| 820 |
/* mark directory as incomplete if it has been unlocked */ |
| 821 |
if (!had_key && fscrypt_has_encryption_key(dir)) |
| 822 |
ceph_dir_clear_complete(dir); |
| 823 |
} |
| 824 |
|
| 825 |
/* can we conclude ENOENT locally? */ |
| 826 |
if (d_really_is_negative(dentry)) { |
| 827 |
struct ceph_inode_info *ci = ceph_inode(dir); |
| 828 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 829 |
|
| 830 |
spin_lock(&ci->i_ceph_lock); |
| 831 |
doutc(cl, " dir %llx.%llx flags are 0x%lx\n", |
| 832 |
ceph_vinop(dir), ci->i_ceph_flags); |
| 833 |
if (strncmp(dentry->d_name.name, |
| 834 |
fsc->mount_options->snapdir_name, |
| 835 |
dentry->d_name.len) && |
| 836 |
!is_root_ceph_dentry(dir, dentry) && |
| 837 |
ceph_test_mount_opt(fsc, DCACHE) && |
| 838 |
__ceph_dir_is_complete(ci) && |
| 839 |
__ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) { |
| 840 |
__ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD); |
| 841 |
spin_unlock(&ci->i_ceph_lock); |
| 842 |
doutc(cl, " dir %llx.%llx complete, -ENOENT\n", |
| 843 |
ceph_vinop(dir)); |
| 844 |
if (d_unhashed(dentry)) |
| 845 |
d_add(dentry, NULL); |
| 846 |
di->lease_shared_gen = atomic_read(&ci->i_shared_gen); |
| 847 |
return NULL; |
| 848 |
} |
| 849 |
spin_unlock(&ci->i_ceph_lock); |
| 850 |
} |
| 851 |
|
| 852 |
op = ceph_snap(dir) == CEPH_SNAPDIR ? |
| 853 |
CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; |
| 854 |
req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); |
| 855 |
if (IS_ERR(req)) |
| 856 |
return ERR_CAST(req); |
| 857 |
req->r_dentry = dget(dentry); |
| 858 |
req->r_num_caps = 2; |
| 859 |
|
| 860 |
mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED; |
| 861 |
if (ceph_security_xattr_wanted(dir)) |
| 862 |
mask |= CEPH_CAP_XATTR_SHARED; |
| 863 |
req->r_args.getattr.mask = cpu_to_le32(mask); |
| 864 |
|
| 865 |
ihold(dir); |
| 866 |
req->r_parent = dir; |
| 867 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 868 |
err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 869 |
if (err == -ENOENT) { |
| 870 |
struct dentry *res; |
| 871 |
|
| 872 |
res = ceph_handle_snapdir(req, dentry); |
| 873 |
if (IS_ERR(res)) { |
| 874 |
err = PTR_ERR(res); |
| 875 |
} else { |
| 876 |
dentry = res; |
| 877 |
err = 0; |
| 878 |
} |
| 879 |
} |
| 880 |
dentry = ceph_finish_lookup(req, dentry, err); |
| 881 |
ceph_mdsc_put_request(req); /* will dput(dentry) */ |
| 882 |
doutc(cl, "result=%p\n", dentry); |
| 883 |
return dentry; |
| 884 |
} |
| 885 |
|
| 886 |
/* |
| 887 |
* If we do a create but get no trace back from the MDS, follow up with |
| 888 |
* a lookup (the VFS expects us to link up the provided dentry). |
| 889 |
*/ |
| 890 |
int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry) |
| 891 |
{ |
| 892 |
struct dentry *result = ceph_lookup(dir, dentry, 0); |
| 893 |
|
| 894 |
if (result && !IS_ERR(result)) { |
| 895 |
/* |
| 896 |
* We created the item, then did a lookup, and found |
| 897 |
* it was already linked to another inode we already |
| 898 |
* had in our cache (and thus got spliced). To not |
| 899 |
* confuse VFS (especially when inode is a directory), |
| 900 |
* we don't link our dentry to that inode, return an |
| 901 |
* error instead. |
| 902 |
* |
| 903 |
* This event should be rare and it happens only when |
| 904 |
* we talk to old MDS. Recent MDS does not send traceless |
| 905 |
* reply for request that creates new inode. |
| 906 |
*/ |
| 907 |
d_drop(result); |
| 908 |
return -ESTALE; |
| 909 |
} |
| 910 |
return PTR_ERR(result); |
| 911 |
} |
| 912 |
|
| 913 |
static int ceph_mknod(struct mnt_idmap *idmap, struct inode *dir, |
| 914 |
struct dentry *dentry, umode_t mode, dev_t rdev) |
| 915 |
{ |
| 916 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); |
| 917 |
struct ceph_client *cl = mdsc->fsc->client; |
| 918 |
struct ceph_mds_request *req; |
| 919 |
struct ceph_acl_sec_ctx as_ctx = {}; |
| 920 |
int err; |
| 921 |
|
| 922 |
if (ceph_snap(dir) != CEPH_NOSNAP) |
| 923 |
return -EROFS; |
| 924 |
|
| 925 |
err = ceph_wait_on_conflict_unlink(dentry); |
| 926 |
if (err) |
| 927 |
return err; |
| 928 |
|
| 929 |
if (ceph_quota_is_max_files_exceeded(dir)) { |
| 930 |
err = -EDQUOT; |
| 931 |
goto out; |
| 932 |
} |
| 933 |
|
| 934 |
doutc(cl, "%p %llx.%llx/'%pd' dentry %p mode 0%ho rdev %d\n", |
| 935 |
dir, ceph_vinop(dir), dentry, dentry, mode, rdev); |
| 936 |
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS); |
| 937 |
if (IS_ERR(req)) { |
| 938 |
err = PTR_ERR(req); |
| 939 |
goto out; |
| 940 |
} |
| 941 |
|
| 942 |
req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx); |
| 943 |
if (IS_ERR(req->r_new_inode)) { |
| 944 |
err = PTR_ERR(req->r_new_inode); |
| 945 |
req->r_new_inode = NULL; |
| 946 |
goto out_req; |
| 947 |
} |
| 948 |
|
| 949 |
if (S_ISREG(mode) && IS_ENCRYPTED(dir)) |
| 950 |
set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags); |
| 951 |
|
| 952 |
req->r_dentry = dget(dentry); |
| 953 |
req->r_num_caps = 2; |
| 954 |
req->r_parent = dir; |
| 955 |
ihold(dir); |
| 956 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 957 |
req->r_mnt_idmap = mnt_idmap_get(idmap); |
| 958 |
req->r_args.mknod.mode = cpu_to_le32(mode); |
| 959 |
req->r_args.mknod.rdev = cpu_to_le32(rdev); |
| 960 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL | |
| 961 |
CEPH_CAP_XATTR_EXCL; |
| 962 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 963 |
|
| 964 |
ceph_as_ctx_to_req(req, &as_ctx); |
| 965 |
|
| 966 |
err = ceph_mdsc_do_request(mdsc, dir, req); |
| 967 |
if (!err && !req->r_reply_info.head->is_dentry) |
| 968 |
err = ceph_handle_notrace_create(dir, dentry); |
| 969 |
out_req: |
| 970 |
ceph_mdsc_put_request(req); |
| 971 |
out: |
| 972 |
if (!err) |
| 973 |
ceph_init_inode_acls(d_inode(dentry), &as_ctx); |
| 974 |
else |
| 975 |
d_drop(dentry); |
| 976 |
ceph_release_acl_sec_ctx(&as_ctx); |
| 977 |
return err; |
| 978 |
} |
| 979 |
|
| 980 |
static int ceph_create(struct mnt_idmap *idmap, struct inode *dir, |
| 981 |
struct dentry *dentry, umode_t mode, bool excl) |
| 982 |
{ |
| 983 |
return ceph_mknod(idmap, dir, dentry, mode, 0); |
| 984 |
} |
| 985 |
|
| 986 |
#if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 987 |
static int prep_encrypted_symlink_target(struct ceph_mds_request *req, |
| 988 |
const char *dest) |
| 989 |
{ |
| 990 |
int err; |
| 991 |
int len = strlen(dest); |
| 992 |
struct fscrypt_str osd_link = FSTR_INIT(NULL, 0); |
| 993 |
|
| 994 |
err = fscrypt_prepare_symlink(req->r_parent, dest, len, PATH_MAX, |
| 995 |
&osd_link); |
| 996 |
if (err) |
| 997 |
goto out; |
| 998 |
|
| 999 |
err = fscrypt_encrypt_symlink(req->r_new_inode, dest, len, &osd_link); |
| 1000 |
if (err) |
| 1001 |
goto out; |
| 1002 |
|
| 1003 |
req->r_path2 = kmalloc(BASE64_CHARS(osd_link.len) + 1, GFP_KERNEL); |
| 1004 |
if (!req->r_path2) { |
| 1005 |
err = -ENOMEM; |
| 1006 |
goto out; |
| 1007 |
} |
| 1008 |
|
| 1009 |
len = base64_encode(osd_link.name, osd_link.len, |
| 1010 |
req->r_path2, false, BASE64_IMAP); |
| 1011 |
req->r_path2[len] = '\0'; |
| 1012 |
out: |
| 1013 |
fscrypt_fname_free_buffer(&osd_link); |
| 1014 |
return err; |
| 1015 |
} |
| 1016 |
#else |
| 1017 |
static int prep_encrypted_symlink_target(struct ceph_mds_request *req, |
| 1018 |
const char *dest) |
| 1019 |
{ |
| 1020 |
return -EOPNOTSUPP; |
| 1021 |
} |
| 1022 |
#endif |
| 1023 |
|
| 1024 |
static int ceph_symlink(struct mnt_idmap *idmap, struct inode *dir, |
| 1025 |
struct dentry *dentry, const char *dest) |
| 1026 |
{ |
| 1027 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); |
| 1028 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1029 |
struct ceph_mds_request *req; |
| 1030 |
struct ceph_acl_sec_ctx as_ctx = {}; |
| 1031 |
umode_t mode = S_IFLNK | 0777; |
| 1032 |
int err; |
| 1033 |
|
| 1034 |
if (ceph_snap(dir) != CEPH_NOSNAP) |
| 1035 |
return -EROFS; |
| 1036 |
|
| 1037 |
err = ceph_wait_on_conflict_unlink(dentry); |
| 1038 |
if (err) |
| 1039 |
return err; |
| 1040 |
|
| 1041 |
if (ceph_quota_is_max_files_exceeded(dir)) { |
| 1042 |
err = -EDQUOT; |
| 1043 |
goto out; |
| 1044 |
} |
| 1045 |
|
| 1046 |
doutc(cl, "%p %llx.%llx/'%pd' to '%s'\n", dir, ceph_vinop(dir), dentry, |
| 1047 |
dest); |
| 1048 |
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS); |
| 1049 |
if (IS_ERR(req)) { |
| 1050 |
err = PTR_ERR(req); |
| 1051 |
goto out; |
| 1052 |
} |
| 1053 |
|
| 1054 |
req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx); |
| 1055 |
if (IS_ERR(req->r_new_inode)) { |
| 1056 |
err = PTR_ERR(req->r_new_inode); |
| 1057 |
req->r_new_inode = NULL; |
| 1058 |
goto out_req; |
| 1059 |
} |
| 1060 |
|
| 1061 |
req->r_parent = dir; |
| 1062 |
ihold(dir); |
| 1063 |
|
| 1064 |
if (IS_ENCRYPTED(req->r_new_inode)) { |
| 1065 |
err = prep_encrypted_symlink_target(req, dest); |
| 1066 |
if (err) |
| 1067 |
goto out_req; |
| 1068 |
} else { |
| 1069 |
req->r_path2 = kstrdup(dest, GFP_KERNEL); |
| 1070 |
if (!req->r_path2) { |
| 1071 |
err = -ENOMEM; |
| 1072 |
goto out_req; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 1077 |
req->r_mnt_idmap = mnt_idmap_get(idmap); |
| 1078 |
req->r_dentry = dget(dentry); |
| 1079 |
req->r_num_caps = 2; |
| 1080 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL | |
| 1081 |
CEPH_CAP_XATTR_EXCL; |
| 1082 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1083 |
|
| 1084 |
ceph_as_ctx_to_req(req, &as_ctx); |
| 1085 |
|
| 1086 |
err = ceph_mdsc_do_request(mdsc, dir, req); |
| 1087 |
if (!err && !req->r_reply_info.head->is_dentry) |
| 1088 |
err = ceph_handle_notrace_create(dir, dentry); |
| 1089 |
out_req: |
| 1090 |
ceph_mdsc_put_request(req); |
| 1091 |
out: |
| 1092 |
if (err) |
| 1093 |
d_drop(dentry); |
| 1094 |
ceph_release_acl_sec_ctx(&as_ctx); |
| 1095 |
return err; |
| 1096 |
} |
| 1097 |
|
| 1098 |
static struct dentry *ceph_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
| 1099 |
struct dentry *dentry, umode_t mode) |
| 1100 |
{ |
| 1101 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); |
| 1102 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1103 |
struct ceph_mds_request *req; |
| 1104 |
struct ceph_acl_sec_ctx as_ctx = {}; |
| 1105 |
struct dentry *ret; |
| 1106 |
int err; |
| 1107 |
int op; |
| 1108 |
|
| 1109 |
err = ceph_wait_on_conflict_unlink(dentry); |
| 1110 |
if (err) |
| 1111 |
return ERR_PTR(err); |
| 1112 |
|
| 1113 |
if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 1114 |
/* mkdir .snap/foo is a MKSNAP */ |
| 1115 |
op = CEPH_MDS_OP_MKSNAP; |
| 1116 |
doutc(cl, "mksnap %llx.%llx/'%pd' dentry %p\n", |
| 1117 |
ceph_vinop(dir), dentry, dentry); |
| 1118 |
} else if (ceph_snap(dir) == CEPH_NOSNAP) { |
| 1119 |
doutc(cl, "mkdir %llx.%llx/'%pd' dentry %p mode 0%ho\n", |
| 1120 |
ceph_vinop(dir), dentry, dentry, mode); |
| 1121 |
op = CEPH_MDS_OP_MKDIR; |
| 1122 |
} else { |
| 1123 |
ret = ERR_PTR(-EROFS); |
| 1124 |
goto out; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (op == CEPH_MDS_OP_MKDIR && |
| 1128 |
ceph_quota_is_max_files_exceeded(dir)) { |
| 1129 |
ret = ERR_PTR(-EDQUOT); |
| 1130 |
goto out; |
| 1131 |
} |
| 1132 |
if ((op == CEPH_MDS_OP_MKSNAP) && IS_ENCRYPTED(dir) && |
| 1133 |
!fscrypt_has_encryption_key(dir)) { |
| 1134 |
ret = ERR_PTR(-ENOKEY); |
| 1135 |
goto out; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 1140 |
if (IS_ERR(req)) { |
| 1141 |
ret = ERR_CAST(req); |
| 1142 |
goto out; |
| 1143 |
} |
| 1144 |
|
| 1145 |
mode |= S_IFDIR; |
| 1146 |
req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx); |
| 1147 |
if (IS_ERR(req->r_new_inode)) { |
| 1148 |
ret = ERR_CAST(req->r_new_inode); |
| 1149 |
req->r_new_inode = NULL; |
| 1150 |
goto out_req; |
| 1151 |
} |
| 1152 |
|
| 1153 |
req->r_dentry = dget(dentry); |
| 1154 |
req->r_num_caps = 2; |
| 1155 |
req->r_parent = dir; |
| 1156 |
ihold(dir); |
| 1157 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 1158 |
if (op == CEPH_MDS_OP_MKDIR) |
| 1159 |
req->r_mnt_idmap = mnt_idmap_get(idmap); |
| 1160 |
req->r_args.mkdir.mode = cpu_to_le32(mode); |
| 1161 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL | |
| 1162 |
CEPH_CAP_XATTR_EXCL; |
| 1163 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1164 |
|
| 1165 |
ceph_as_ctx_to_req(req, &as_ctx); |
| 1166 |
|
| 1167 |
err = ceph_mdsc_do_request(mdsc, dir, req); |
| 1168 |
if (!err && |
| 1169 |
!req->r_reply_info.head->is_target && |
| 1170 |
!req->r_reply_info.head->is_dentry) |
| 1171 |
err = ceph_handle_notrace_create(dir, dentry); |
| 1172 |
ret = ERR_PTR(err); |
| 1173 |
out_req: |
| 1174 |
if (!IS_ERR(ret) && req->r_dentry != dentry) |
| 1175 |
/* Some other dentry was spliced in */ |
| 1176 |
ret = dget(req->r_dentry); |
| 1177 |
ceph_mdsc_put_request(req); |
| 1178 |
out: |
| 1179 |
if (!IS_ERR(ret)) { |
| 1180 |
if (ret) |
| 1181 |
dentry = ret; |
| 1182 |
ceph_init_inode_acls(d_inode(dentry), &as_ctx); |
| 1183 |
} else { |
| 1184 |
d_drop(dentry); |
| 1185 |
} |
| 1186 |
ceph_release_acl_sec_ctx(&as_ctx); |
| 1187 |
return ret; |
| 1188 |
} |
| 1189 |
|
| 1190 |
static int ceph_link(struct dentry *old_dentry, struct inode *dir, |
| 1191 |
struct dentry *dentry) |
| 1192 |
{ |
| 1193 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb); |
| 1194 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1195 |
struct ceph_mds_request *req; |
| 1196 |
int err; |
| 1197 |
|
| 1198 |
if (dentry->d_flags & DCACHE_DISCONNECTED) |
| 1199 |
return -EINVAL; |
| 1200 |
|
| 1201 |
err = ceph_wait_on_conflict_unlink(dentry); |
| 1202 |
if (err) |
| 1203 |
return err; |
| 1204 |
|
| 1205 |
if (ceph_snap(dir) != CEPH_NOSNAP) |
| 1206 |
return -EROFS; |
| 1207 |
|
| 1208 |
err = fscrypt_prepare_link(old_dentry, dir, dentry); |
| 1209 |
if (err) |
| 1210 |
return err; |
| 1211 |
|
| 1212 |
doutc(cl, "%p %llx.%llx/'%pd' to '%pd'\n", dir, ceph_vinop(dir), |
| 1213 |
old_dentry, dentry); |
| 1214 |
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS); |
| 1215 |
if (IS_ERR(req)) { |
| 1216 |
d_drop(dentry); |
| 1217 |
return PTR_ERR(req); |
| 1218 |
} |
| 1219 |
req->r_dentry = dget(dentry); |
| 1220 |
req->r_num_caps = 2; |
| 1221 |
req->r_old_dentry = dget(old_dentry); |
| 1222 |
/* |
| 1223 |
* The old_dentry maybe a DCACHE_DISCONNECTED dentry, then we |
| 1224 |
* will just pass the ino# to MDSs. |
| 1225 |
*/ |
| 1226 |
if (old_dentry->d_flags & DCACHE_DISCONNECTED) |
| 1227 |
req->r_ino2 = ceph_vino(d_inode(old_dentry)); |
| 1228 |
req->r_parent = dir; |
| 1229 |
ihold(dir); |
| 1230 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 1231 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL; |
| 1232 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1233 |
/* release LINK_SHARED on source inode (mds will lock it) */ |
| 1234 |
req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
| 1235 |
err = ceph_mdsc_do_request(mdsc, dir, req); |
| 1236 |
if (err) { |
| 1237 |
d_drop(dentry); |
| 1238 |
} else if (!req->r_reply_info.head->is_dentry) { |
| 1239 |
ihold(d_inode(old_dentry)); |
| 1240 |
d_instantiate(dentry, d_inode(old_dentry)); |
| 1241 |
} |
| 1242 |
ceph_mdsc_put_request(req); |
| 1243 |
return err; |
| 1244 |
} |
| 1245 |
|
| 1246 |
static void ceph_async_unlink_cb(struct ceph_mds_client *mdsc, |
| 1247 |
struct ceph_mds_request *req) |
| 1248 |
{ |
| 1249 |
struct dentry *dentry = req->r_dentry; |
| 1250 |
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb); |
| 1251 |
struct ceph_client *cl = fsc->client; |
| 1252 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1253 |
int result = req->r_err ? req->r_err : |
| 1254 |
le32_to_cpu(req->r_reply_info.head->result); |
| 1255 |
|
| 1256 |
if (!test_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags)) |
| 1257 |
pr_warn_client(cl, |
| 1258 |
"dentry %p:%pd async unlink bit is not set\n", |
| 1259 |
dentry, dentry); |
| 1260 |
|
| 1261 |
spin_lock(&fsc->async_unlink_conflict_lock); |
| 1262 |
hash_del_rcu(&di->hnode); |
| 1263 |
spin_unlock(&fsc->async_unlink_conflict_lock); |
| 1264 |
|
| 1265 |
spin_lock(&dentry->d_lock); |
| 1266 |
clear_and_wake_up_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags); |
| 1267 |
spin_unlock(&dentry->d_lock); |
| 1268 |
|
| 1269 |
synchronize_rcu(); |
| 1270 |
|
| 1271 |
if (result == -EJUKEBOX) |
| 1272 |
goto out; |
| 1273 |
|
| 1274 |
/* If op failed, mark everyone involved for errors */ |
| 1275 |
if (result) { |
| 1276 |
struct ceph_path_info path_info = {0}; |
| 1277 |
char *path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0); |
| 1278 |
|
| 1279 |
/* mark error on parent + clear complete */ |
| 1280 |
mapping_set_error(req->r_parent->i_mapping, result); |
| 1281 |
ceph_dir_clear_complete(req->r_parent); |
| 1282 |
|
| 1283 |
/* drop the dentry -- we don't know its status */ |
| 1284 |
if (!d_unhashed(dentry)) |
| 1285 |
d_drop(dentry); |
| 1286 |
|
| 1287 |
/* mark inode itself for an error (since metadata is bogus) */ |
| 1288 |
mapping_set_error(req->r_old_inode->i_mapping, result); |
| 1289 |
|
| 1290 |
pr_warn_client(cl, "failure path=(%llx)%s result=%d!\n", |
| 1291 |
path_info.vino.ino, IS_ERR(path) ? "<<bad>>" : path, result); |
| 1292 |
ceph_mdsc_free_path_info(&path_info); |
| 1293 |
} |
| 1294 |
out: |
| 1295 |
iput(req->r_old_inode); |
| 1296 |
ceph_mdsc_release_dir_caps(req); |
| 1297 |
} |
| 1298 |
|
| 1299 |
static int get_caps_for_async_unlink(struct inode *dir, struct dentry *dentry) |
| 1300 |
{ |
| 1301 |
struct ceph_inode_info *ci = ceph_inode(dir); |
| 1302 |
struct ceph_dentry_info *di; |
| 1303 |
int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_UNLINK; |
| 1304 |
|
| 1305 |
spin_lock(&ci->i_ceph_lock); |
| 1306 |
if ((__ceph_caps_issued(ci, NULL) & want) == want) { |
| 1307 |
ceph_take_cap_refs(ci, want, false); |
| 1308 |
got = want; |
| 1309 |
} |
| 1310 |
spin_unlock(&ci->i_ceph_lock); |
| 1311 |
|
| 1312 |
/* If we didn't get anything, return 0 */ |
| 1313 |
if (!got) |
| 1314 |
return 0; |
| 1315 |
|
| 1316 |
spin_lock(&dentry->d_lock); |
| 1317 |
di = ceph_dentry(dentry); |
| 1318 |
/* |
| 1319 |
* - We are holding Fx, which implies Fs caps. |
| 1320 |
* - Only support async unlink for primary linkage |
| 1321 |
*/ |
| 1322 |
if (atomic_read(&ci->i_shared_gen) != di->lease_shared_gen || |
| 1323 |
!(di->flags & CEPH_DENTRY_PRIMARY_LINK)) |
| 1324 |
want = 0; |
| 1325 |
spin_unlock(&dentry->d_lock); |
| 1326 |
|
| 1327 |
/* Do we still want what we've got? */ |
| 1328 |
if (want == got) |
| 1329 |
return got; |
| 1330 |
|
| 1331 |
ceph_put_cap_refs(ci, got); |
| 1332 |
return 0; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/* |
| 1336 |
* rmdir and unlink are differ only by the metadata op code |
| 1337 |
*/ |
| 1338 |
static int ceph_unlink(struct inode *dir, struct dentry *dentry) |
| 1339 |
{ |
| 1340 |
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb); |
| 1341 |
struct ceph_client *cl = fsc->client; |
| 1342 |
struct ceph_mds_client *mdsc = fsc->mdsc; |
| 1343 |
struct inode *inode = d_inode(dentry); |
| 1344 |
struct ceph_inode_info *ci = ceph_inode(inode); |
| 1345 |
struct ceph_mds_request *req; |
| 1346 |
bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS); |
| 1347 |
struct dentry *dn; |
| 1348 |
int err = -EROFS; |
| 1349 |
int op; |
| 1350 |
char *path; |
| 1351 |
|
| 1352 |
if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 1353 |
/* rmdir .snap/foo is RMSNAP */ |
| 1354 |
doutc(cl, "rmsnap %llx.%llx/'%pd' dn\n", ceph_vinop(dir), |
| 1355 |
dentry); |
| 1356 |
op = CEPH_MDS_OP_RMSNAP; |
| 1357 |
} else if (ceph_snap(dir) == CEPH_NOSNAP) { |
| 1358 |
doutc(cl, "unlink/rmdir %llx.%llx/'%pd' inode %llx.%llx\n", |
| 1359 |
ceph_vinop(dir), dentry, ceph_vinop(inode)); |
| 1360 |
op = d_is_dir(dentry) ? |
| 1361 |
CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK; |
| 1362 |
} else |
| 1363 |
goto out; |
| 1364 |
|
| 1365 |
dn = d_find_alias(dir); |
| 1366 |
if (!dn) { |
| 1367 |
try_async = false; |
| 1368 |
} else { |
| 1369 |
struct ceph_path_info path_info = {0}; |
| 1370 |
path = ceph_mdsc_build_path(mdsc, dn, &path_info, 0); |
| 1371 |
if (IS_ERR(path)) { |
| 1372 |
try_async = false; |
| 1373 |
err = 0; |
| 1374 |
} else { |
| 1375 |
err = ceph_mds_check_access(mdsc, path, MAY_WRITE); |
| 1376 |
} |
| 1377 |
ceph_mdsc_free_path_info(&path_info); |
| 1378 |
dput(dn); |
| 1379 |
|
| 1380 |
/* For none EACCES cases will let the MDS do the mds auth check */ |
| 1381 |
if (err == -EACCES) { |
| 1382 |
return err; |
| 1383 |
} else if (err < 0) { |
| 1384 |
try_async = false; |
| 1385 |
err = 0; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
retry: |
| 1390 |
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 1391 |
if (IS_ERR(req)) { |
| 1392 |
err = PTR_ERR(req); |
| 1393 |
goto out; |
| 1394 |
} |
| 1395 |
req->r_dentry = dget(dentry); |
| 1396 |
req->r_num_caps = 2; |
| 1397 |
req->r_parent = dir; |
| 1398 |
ihold(dir); |
| 1399 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL; |
| 1400 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1401 |
req->r_inode_drop = ceph_drop_caps_for_unlink(inode); |
| 1402 |
|
| 1403 |
if (try_async && op == CEPH_MDS_OP_UNLINK && |
| 1404 |
(req->r_dir_caps = get_caps_for_async_unlink(dir, dentry))) { |
| 1405 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1406 |
|
| 1407 |
doutc(cl, "async unlink on %llx.%llx/'%pd' caps=%s", |
| 1408 |
ceph_vinop(dir), dentry, |
| 1409 |
ceph_cap_string(req->r_dir_caps)); |
| 1410 |
set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags); |
| 1411 |
req->r_callback = ceph_async_unlink_cb; |
| 1412 |
req->r_old_inode = d_inode(dentry); |
| 1413 |
ihold(req->r_old_inode); |
| 1414 |
|
| 1415 |
spin_lock(&dentry->d_lock); |
| 1416 |
di->flags |= CEPH_DENTRY_ASYNC_UNLINK; |
| 1417 |
spin_unlock(&dentry->d_lock); |
| 1418 |
|
| 1419 |
spin_lock(&fsc->async_unlink_conflict_lock); |
| 1420 |
hash_add_rcu(fsc->async_unlink_conflict, &di->hnode, |
| 1421 |
dentry->d_name.hash); |
| 1422 |
spin_unlock(&fsc->async_unlink_conflict_lock); |
| 1423 |
|
| 1424 |
err = ceph_mdsc_submit_request(mdsc, dir, req); |
| 1425 |
if (!err) { |
| 1426 |
/* |
| 1427 |
* We have enough caps, so we assume that the unlink |
| 1428 |
* will succeed. Fix up the target inode and dcache. |
| 1429 |
*/ |
| 1430 |
|
| 1431 |
/* |
| 1432 |
* Protect the i_nlink update with i_ceph_lock |
| 1433 |
* to precent racing against ceph_fill_inode() |
| 1434 |
* handling our completion on a worker thread |
| 1435 |
* and don't decrement if i_nlink has already |
| 1436 |
* been updated to zero by this completion. |
| 1437 |
*/ |
| 1438 |
spin_lock(&ci->i_ceph_lock); |
| 1439 |
if (inode->i_nlink > 0) |
| 1440 |
drop_nlink(inode); |
| 1441 |
spin_unlock(&ci->i_ceph_lock); |
| 1442 |
|
| 1443 |
d_delete(dentry); |
| 1444 |
} else { |
| 1445 |
spin_lock(&fsc->async_unlink_conflict_lock); |
| 1446 |
hash_del_rcu(&di->hnode); |
| 1447 |
spin_unlock(&fsc->async_unlink_conflict_lock); |
| 1448 |
|
| 1449 |
spin_lock(&dentry->d_lock); |
| 1450 |
di->flags &= ~CEPH_DENTRY_ASYNC_UNLINK; |
| 1451 |
spin_unlock(&dentry->d_lock); |
| 1452 |
|
| 1453 |
if (err == -EJUKEBOX) { |
| 1454 |
try_async = false; |
| 1455 |
ceph_mdsc_put_request(req); |
| 1456 |
goto retry; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} else { |
| 1460 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 1461 |
err = ceph_mdsc_do_request(mdsc, dir, req); |
| 1462 |
if (!err && !req->r_reply_info.head->is_dentry) |
| 1463 |
d_delete(dentry); |
| 1464 |
} |
| 1465 |
|
| 1466 |
ceph_mdsc_put_request(req); |
| 1467 |
out: |
| 1468 |
return err; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static int ceph_rename(struct mnt_idmap *idmap, struct inode *old_dir, |
| 1472 |
struct dentry *old_dentry, struct inode *new_dir, |
| 1473 |
struct dentry *new_dentry, unsigned int flags) |
| 1474 |
{ |
| 1475 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old_dir->i_sb); |
| 1476 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1477 |
struct ceph_mds_request *req; |
| 1478 |
int op = CEPH_MDS_OP_RENAME; |
| 1479 |
int err; |
| 1480 |
|
| 1481 |
if (flags) |
| 1482 |
return -EINVAL; |
| 1483 |
|
| 1484 |
if (ceph_snap(old_dir) != ceph_snap(new_dir)) |
| 1485 |
return -EXDEV; |
| 1486 |
if (ceph_snap(old_dir) != CEPH_NOSNAP) { |
| 1487 |
if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR) |
| 1488 |
op = CEPH_MDS_OP_RENAMESNAP; |
| 1489 |
else |
| 1490 |
return -EROFS; |
| 1491 |
} |
| 1492 |
/* don't allow cross-quota renames */ |
| 1493 |
if ((old_dir != new_dir) && |
| 1494 |
(!ceph_quota_is_same_realm(old_dir, new_dir))) |
| 1495 |
return -EXDEV; |
| 1496 |
|
| 1497 |
err = ceph_wait_on_conflict_unlink(new_dentry); |
| 1498 |
if (err) |
| 1499 |
return err; |
| 1500 |
|
| 1501 |
err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 1502 |
flags); |
| 1503 |
if (err) |
| 1504 |
return err; |
| 1505 |
|
| 1506 |
doutc(cl, "%llx.%llx/'%pd' to %llx.%llx/'%pd'\n", |
| 1507 |
ceph_vinop(old_dir), old_dentry, ceph_vinop(new_dir), |
| 1508 |
new_dentry); |
| 1509 |
req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 1510 |
if (IS_ERR(req)) |
| 1511 |
return PTR_ERR(req); |
| 1512 |
ihold(old_dir); |
| 1513 |
req->r_dentry = dget(new_dentry); |
| 1514 |
req->r_num_caps = 2; |
| 1515 |
req->r_old_dentry = dget(old_dentry); |
| 1516 |
req->r_old_dentry_dir = old_dir; |
| 1517 |
req->r_parent = new_dir; |
| 1518 |
ihold(new_dir); |
| 1519 |
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
| 1520 |
req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL; |
| 1521 |
req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1522 |
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL; |
| 1523 |
req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1524 |
/* release LINK_RDCACHE on source inode (mds will lock it) */ |
| 1525 |
req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
| 1526 |
if (d_really_is_positive(new_dentry)) { |
| 1527 |
req->r_inode_drop = |
| 1528 |
ceph_drop_caps_for_unlink(d_inode(new_dentry)); |
| 1529 |
} |
| 1530 |
err = ceph_mdsc_do_request(mdsc, old_dir, req); |
| 1531 |
if (!err && !req->r_reply_info.head->is_dentry) { |
| 1532 |
/* |
| 1533 |
* Normally d_move() is done by fill_trace (called by |
| 1534 |
* do_request, above). If there is no trace, we need |
| 1535 |
* to do it here. |
| 1536 |
*/ |
| 1537 |
d_move(old_dentry, new_dentry); |
| 1538 |
} |
| 1539 |
ceph_mdsc_put_request(req); |
| 1540 |
return err; |
| 1541 |
} |
| 1542 |
|
| 1543 |
/* |
| 1544 |
* Move dentry to tail of mdsc->dentry_leases list when lease is updated. |
| 1545 |
* Leases at front of the list will expire first. (Assume all leases have |
| 1546 |
* similar duration) |
| 1547 |
* |
| 1548 |
* Called under dentry->d_lock. |
| 1549 |
*/ |
| 1550 |
void __ceph_dentry_lease_touch(struct ceph_dentry_info *di) |
| 1551 |
{ |
| 1552 |
struct dentry *dn = di->dentry; |
| 1553 |
struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc; |
| 1554 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1555 |
|
| 1556 |
doutc(cl, "%p %p '%pd'\n", di, dn, dn); |
| 1557 |
|
| 1558 |
di->flags |= CEPH_DENTRY_LEASE_LIST; |
| 1559 |
if (di->flags & CEPH_DENTRY_SHRINK_LIST) { |
| 1560 |
di->flags |= CEPH_DENTRY_REFERENCED; |
| 1561 |
return; |
| 1562 |
} |
| 1563 |
|
| 1564 |
spin_lock(&mdsc->dentry_list_lock); |
| 1565 |
list_move_tail(&di->lease_list, &mdsc->dentry_leases); |
| 1566 |
spin_unlock(&mdsc->dentry_list_lock); |
| 1567 |
} |
| 1568 |
|
| 1569 |
static void __dentry_dir_lease_touch(struct ceph_mds_client* mdsc, |
| 1570 |
struct ceph_dentry_info *di) |
| 1571 |
{ |
| 1572 |
di->flags &= ~(CEPH_DENTRY_LEASE_LIST | CEPH_DENTRY_REFERENCED); |
| 1573 |
di->lease_gen = 0; |
| 1574 |
di->time = jiffies; |
| 1575 |
list_move_tail(&di->lease_list, &mdsc->dentry_dir_leases); |
| 1576 |
} |
| 1577 |
|
| 1578 |
/* |
| 1579 |
* When dir lease is used, add dentry to tail of mdsc->dentry_dir_leases |
| 1580 |
* list if it's not in the list, otherwise set 'referenced' flag. |
| 1581 |
* |
| 1582 |
* Called under dentry->d_lock. |
| 1583 |
*/ |
| 1584 |
void __ceph_dentry_dir_lease_touch(struct ceph_dentry_info *di) |
| 1585 |
{ |
| 1586 |
struct dentry *dn = di->dentry; |
| 1587 |
struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc; |
| 1588 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1589 |
|
| 1590 |
doutc(cl, "%p %p '%pd' (offset 0x%llx)\n", di, dn, dn, di->offset); |
| 1591 |
|
| 1592 |
if (!list_empty(&di->lease_list)) { |
| 1593 |
if (di->flags & CEPH_DENTRY_LEASE_LIST) { |
| 1594 |
/* don't remove dentry from dentry lease list |
| 1595 |
* if its lease is valid */ |
| 1596 |
if (__dentry_lease_is_valid(di)) |
| 1597 |
return; |
| 1598 |
} else { |
| 1599 |
di->flags |= CEPH_DENTRY_REFERENCED; |
| 1600 |
return; |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
if (di->flags & CEPH_DENTRY_SHRINK_LIST) { |
| 1605 |
di->flags |= CEPH_DENTRY_REFERENCED; |
| 1606 |
di->flags &= ~CEPH_DENTRY_LEASE_LIST; |
| 1607 |
return; |
| 1608 |
} |
| 1609 |
|
| 1610 |
spin_lock(&mdsc->dentry_list_lock); |
| 1611 |
__dentry_dir_lease_touch(mdsc, di); |
| 1612 |
spin_unlock(&mdsc->dentry_list_lock); |
| 1613 |
} |
| 1614 |
|
| 1615 |
static void __dentry_lease_unlist(struct ceph_dentry_info *di) |
| 1616 |
{ |
| 1617 |
struct ceph_mds_client *mdsc; |
| 1618 |
if (di->flags & CEPH_DENTRY_SHRINK_LIST) |
| 1619 |
return; |
| 1620 |
if (list_empty(&di->lease_list)) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
mdsc = ceph_sb_to_fs_client(di->dentry->d_sb)->mdsc; |
| 1624 |
spin_lock(&mdsc->dentry_list_lock); |
| 1625 |
list_del_init(&di->lease_list); |
| 1626 |
spin_unlock(&mdsc->dentry_list_lock); |
| 1627 |
} |
| 1628 |
|
| 1629 |
enum { |
| 1630 |
KEEP = 0, |
| 1631 |
DELETE = 1, |
| 1632 |
TOUCH = 2, |
| 1633 |
STOP = 4, |
| 1634 |
}; |
| 1635 |
|
| 1636 |
struct ceph_lease_walk_control { |
| 1637 |
bool dir_lease; |
| 1638 |
bool expire_dir_lease; |
| 1639 |
unsigned long nr_to_scan; |
| 1640 |
unsigned long dir_lease_ttl; |
| 1641 |
}; |
| 1642 |
|
| 1643 |
static int __dir_lease_check(const struct dentry *, struct ceph_lease_walk_control *); |
| 1644 |
static int __dentry_lease_check(const struct dentry *); |
| 1645 |
|
| 1646 |
static unsigned long |
| 1647 |
__dentry_leases_walk(struct ceph_mds_client *mdsc, |
| 1648 |
struct ceph_lease_walk_control *lwc) |
| 1649 |
{ |
| 1650 |
struct ceph_dentry_info *di, *tmp; |
| 1651 |
struct dentry *dentry, *last = NULL; |
| 1652 |
struct list_head* list; |
| 1653 |
LIST_HEAD(dispose); |
| 1654 |
unsigned long freed = 0; |
| 1655 |
int ret = 0; |
| 1656 |
|
| 1657 |
list = lwc->dir_lease ? &mdsc->dentry_dir_leases : &mdsc->dentry_leases; |
| 1658 |
spin_lock(&mdsc->dentry_list_lock); |
| 1659 |
list_for_each_entry_safe(di, tmp, list, lease_list) { |
| 1660 |
if (!lwc->nr_to_scan) |
| 1661 |
break; |
| 1662 |
--lwc->nr_to_scan; |
| 1663 |
|
| 1664 |
dentry = di->dentry; |
| 1665 |
if (last == dentry) |
| 1666 |
break; |
| 1667 |
|
| 1668 |
if (!spin_trylock(&dentry->d_lock)) |
| 1669 |
continue; |
| 1670 |
|
| 1671 |
if (__lockref_is_dead(&dentry->d_lockref)) { |
| 1672 |
list_del_init(&di->lease_list); |
| 1673 |
goto next; |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (lwc->dir_lease) |
| 1677 |
ret = __dir_lease_check(dentry, lwc); |
| 1678 |
else |
| 1679 |
ret = __dentry_lease_check(dentry); |
| 1680 |
if (ret & TOUCH) { |
| 1681 |
/* move it into tail of dir lease list */ |
| 1682 |
__dentry_dir_lease_touch(mdsc, di); |
| 1683 |
if (!last) |
| 1684 |
last = dentry; |
| 1685 |
} |
| 1686 |
if (ret & DELETE) { |
| 1687 |
/* stale lease */ |
| 1688 |
di->flags &= ~CEPH_DENTRY_REFERENCED; |
| 1689 |
if (dentry->d_lockref.count > 0) { |
| 1690 |
/* update_dentry_lease() will re-add |
| 1691 |
* it to lease list, or |
| 1692 |
* ceph_d_delete() will return 1 when |
| 1693 |
* last reference is dropped */ |
| 1694 |
list_del_init(&di->lease_list); |
| 1695 |
} else { |
| 1696 |
di->flags |= CEPH_DENTRY_SHRINK_LIST; |
| 1697 |
list_move_tail(&di->lease_list, &dispose); |
| 1698 |
dget_dlock(dentry); |
| 1699 |
} |
| 1700 |
} |
| 1701 |
next: |
| 1702 |
spin_unlock(&dentry->d_lock); |
| 1703 |
if (ret & STOP) |
| 1704 |
break; |
| 1705 |
} |
| 1706 |
spin_unlock(&mdsc->dentry_list_lock); |
| 1707 |
|
| 1708 |
while (!list_empty(&dispose)) { |
| 1709 |
di = list_first_entry(&dispose, struct ceph_dentry_info, |
| 1710 |
lease_list); |
| 1711 |
dentry = di->dentry; |
| 1712 |
spin_lock(&dentry->d_lock); |
| 1713 |
|
| 1714 |
list_del_init(&di->lease_list); |
| 1715 |
di->flags &= ~CEPH_DENTRY_SHRINK_LIST; |
| 1716 |
if (di->flags & CEPH_DENTRY_REFERENCED) { |
| 1717 |
spin_lock(&mdsc->dentry_list_lock); |
| 1718 |
if (di->flags & CEPH_DENTRY_LEASE_LIST) { |
| 1719 |
list_add_tail(&di->lease_list, |
| 1720 |
&mdsc->dentry_leases); |
| 1721 |
} else { |
| 1722 |
__dentry_dir_lease_touch(mdsc, di); |
| 1723 |
} |
| 1724 |
spin_unlock(&mdsc->dentry_list_lock); |
| 1725 |
} else { |
| 1726 |
freed++; |
| 1727 |
} |
| 1728 |
|
| 1729 |
spin_unlock(&dentry->d_lock); |
| 1730 |
/* ceph_d_delete() does the trick */ |
| 1731 |
dput(dentry); |
| 1732 |
} |
| 1733 |
return freed; |
| 1734 |
} |
| 1735 |
|
| 1736 |
static int __dentry_lease_check(const struct dentry *dentry) |
| 1737 |
{ |
| 1738 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1739 |
int ret; |
| 1740 |
|
| 1741 |
if (__dentry_lease_is_valid(di)) |
| 1742 |
return STOP; |
| 1743 |
ret = __dir_lease_try_check(dentry); |
| 1744 |
if (ret == -EBUSY) |
| 1745 |
return KEEP; |
| 1746 |
if (ret > 0) |
| 1747 |
return TOUCH; |
| 1748 |
return DELETE; |
| 1749 |
} |
| 1750 |
|
| 1751 |
static int __dir_lease_check(const struct dentry *dentry, |
| 1752 |
struct ceph_lease_walk_control *lwc) |
| 1753 |
{ |
| 1754 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1755 |
|
| 1756 |
int ret = __dir_lease_try_check(dentry); |
| 1757 |
if (ret == -EBUSY) |
| 1758 |
return KEEP; |
| 1759 |
if (ret > 0) { |
| 1760 |
if (time_before(jiffies, di->time + lwc->dir_lease_ttl)) |
| 1761 |
return STOP; |
| 1762 |
/* Move dentry to tail of dir lease list if we don't want |
| 1763 |
* to delete it. So dentries in the list are checked in a |
| 1764 |
* round robin manner */ |
| 1765 |
if (!lwc->expire_dir_lease) |
| 1766 |
return TOUCH; |
| 1767 |
if (dentry->d_lockref.count > 0 || |
| 1768 |
(di->flags & CEPH_DENTRY_REFERENCED)) |
| 1769 |
return TOUCH; |
| 1770 |
/* invalidate dir lease */ |
| 1771 |
di->lease_shared_gen = 0; |
| 1772 |
} |
| 1773 |
return DELETE; |
| 1774 |
} |
| 1775 |
|
| 1776 |
int ceph_trim_dentries(struct ceph_mds_client *mdsc) |
| 1777 |
{ |
| 1778 |
struct ceph_lease_walk_control lwc; |
| 1779 |
unsigned long count; |
| 1780 |
unsigned long freed; |
| 1781 |
|
| 1782 |
spin_lock(&mdsc->caps_list_lock); |
| 1783 |
if (mdsc->caps_use_max > 0 && |
| 1784 |
mdsc->caps_use_count > mdsc->caps_use_max) |
| 1785 |
count = mdsc->caps_use_count - mdsc->caps_use_max; |
| 1786 |
else |
| 1787 |
count = 0; |
| 1788 |
spin_unlock(&mdsc->caps_list_lock); |
| 1789 |
|
| 1790 |
lwc.dir_lease = false; |
| 1791 |
lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE * 2; |
| 1792 |
freed = __dentry_leases_walk(mdsc, &lwc); |
| 1793 |
if (!lwc.nr_to_scan) /* more invalid leases */ |
| 1794 |
return -EAGAIN; |
| 1795 |
|
| 1796 |
if (lwc.nr_to_scan < CEPH_CAPS_PER_RELEASE) |
| 1797 |
lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE; |
| 1798 |
|
| 1799 |
lwc.dir_lease = true; |
| 1800 |
lwc.expire_dir_lease = freed < count; |
| 1801 |
lwc.dir_lease_ttl = mdsc->fsc->mount_options->caps_wanted_delay_max * HZ; |
| 1802 |
freed +=__dentry_leases_walk(mdsc, &lwc); |
| 1803 |
if (!lwc.nr_to_scan) /* more to check */ |
| 1804 |
return -EAGAIN; |
| 1805 |
|
| 1806 |
return freed > 0 ? 1 : 0; |
| 1807 |
} |
| 1808 |
|
| 1809 |
/* |
| 1810 |
* Ensure a dentry lease will no longer revalidate. |
| 1811 |
*/ |
| 1812 |
void ceph_invalidate_dentry_lease(struct dentry *dentry) |
| 1813 |
{ |
| 1814 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1815 |
spin_lock(&dentry->d_lock); |
| 1816 |
di->time = jiffies; |
| 1817 |
di->lease_shared_gen = 0; |
| 1818 |
di->flags &= ~CEPH_DENTRY_PRIMARY_LINK; |
| 1819 |
__dentry_lease_unlist(di); |
| 1820 |
spin_unlock(&dentry->d_lock); |
| 1821 |
} |
| 1822 |
|
| 1823 |
/* |
| 1824 |
* Check if dentry lease is valid. If not, delete the lease. Try to |
| 1825 |
* renew if the least is more than half up. |
| 1826 |
*/ |
| 1827 |
static bool __dentry_lease_is_valid(struct ceph_dentry_info *di) |
| 1828 |
{ |
| 1829 |
struct ceph_mds_session *session; |
| 1830 |
|
| 1831 |
if (!di->lease_gen) |
| 1832 |
return false; |
| 1833 |
|
| 1834 |
session = di->lease_session; |
| 1835 |
if (session) { |
| 1836 |
u32 gen; |
| 1837 |
unsigned long ttl; |
| 1838 |
|
| 1839 |
gen = atomic_read(&session->s_cap_gen); |
| 1840 |
ttl = session->s_cap_ttl; |
| 1841 |
|
| 1842 |
if (di->lease_gen == gen && |
| 1843 |
time_before(jiffies, ttl) && |
| 1844 |
time_before(jiffies, di->time)) |
| 1845 |
return true; |
| 1846 |
} |
| 1847 |
di->lease_gen = 0; |
| 1848 |
return false; |
| 1849 |
} |
| 1850 |
|
| 1851 |
static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags) |
| 1852 |
{ |
| 1853 |
struct ceph_dentry_info *di; |
| 1854 |
struct ceph_mds_session *session = NULL; |
| 1855 |
struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dentry->d_sb)->mdsc; |
| 1856 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1857 |
u32 seq = 0; |
| 1858 |
int valid = 0; |
| 1859 |
|
| 1860 |
spin_lock(&dentry->d_lock); |
| 1861 |
di = ceph_dentry(dentry); |
| 1862 |
if (di && __dentry_lease_is_valid(di)) { |
| 1863 |
valid = 1; |
| 1864 |
|
| 1865 |
if (di->lease_renew_after && |
| 1866 |
time_after(jiffies, di->lease_renew_after)) { |
| 1867 |
/* |
| 1868 |
* We should renew. If we're in RCU walk mode |
| 1869 |
* though, we can't do that so just return |
| 1870 |
* -ECHILD. |
| 1871 |
*/ |
| 1872 |
if (flags & LOOKUP_RCU) { |
| 1873 |
valid = -ECHILD; |
| 1874 |
} else { |
| 1875 |
session = ceph_get_mds_session(di->lease_session); |
| 1876 |
seq = di->lease_seq; |
| 1877 |
di->lease_renew_after = 0; |
| 1878 |
di->lease_renew_from = jiffies; |
| 1879 |
} |
| 1880 |
} |
| 1881 |
} |
| 1882 |
spin_unlock(&dentry->d_lock); |
| 1883 |
|
| 1884 |
if (session) { |
| 1885 |
ceph_mdsc_lease_send_msg(session, dentry, |
| 1886 |
CEPH_MDS_LEASE_RENEW, seq); |
| 1887 |
ceph_put_mds_session(session); |
| 1888 |
} |
| 1889 |
doutc(cl, "dentry %p = %d\n", dentry, valid); |
| 1890 |
return valid; |
| 1891 |
} |
| 1892 |
|
| 1893 |
/* |
| 1894 |
* Called under dentry->d_lock. |
| 1895 |
*/ |
| 1896 |
static int __dir_lease_try_check(const struct dentry *dentry) |
| 1897 |
{ |
| 1898 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1899 |
struct inode *dir; |
| 1900 |
struct ceph_inode_info *ci; |
| 1901 |
int valid = 0; |
| 1902 |
|
| 1903 |
if (!di->lease_shared_gen) |
| 1904 |
return 0; |
| 1905 |
if (IS_ROOT(dentry)) |
| 1906 |
return 0; |
| 1907 |
|
| 1908 |
dir = d_inode(dentry->d_parent); |
| 1909 |
ci = ceph_inode(dir); |
| 1910 |
|
| 1911 |
if (spin_trylock(&ci->i_ceph_lock)) { |
| 1912 |
if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen && |
| 1913 |
__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 0)) |
| 1914 |
valid = 1; |
| 1915 |
spin_unlock(&ci->i_ceph_lock); |
| 1916 |
} else { |
| 1917 |
valid = -EBUSY; |
| 1918 |
} |
| 1919 |
|
| 1920 |
if (!valid) |
| 1921 |
di->lease_shared_gen = 0; |
| 1922 |
return valid; |
| 1923 |
} |
| 1924 |
|
| 1925 |
/* |
| 1926 |
* Check if directory-wide content lease/cap is valid. |
| 1927 |
*/ |
| 1928 |
static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry, |
| 1929 |
struct ceph_mds_client *mdsc) |
| 1930 |
{ |
| 1931 |
struct ceph_inode_info *ci = ceph_inode(dir); |
| 1932 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1933 |
int valid; |
| 1934 |
int shared_gen; |
| 1935 |
|
| 1936 |
spin_lock(&ci->i_ceph_lock); |
| 1937 |
valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1); |
| 1938 |
if (valid) { |
| 1939 |
__ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD); |
| 1940 |
shared_gen = atomic_read(&ci->i_shared_gen); |
| 1941 |
} |
| 1942 |
spin_unlock(&ci->i_ceph_lock); |
| 1943 |
if (valid) { |
| 1944 |
struct ceph_dentry_info *di; |
| 1945 |
spin_lock(&dentry->d_lock); |
| 1946 |
di = ceph_dentry(dentry); |
| 1947 |
if (dir == d_inode(dentry->d_parent) && |
| 1948 |
di && di->lease_shared_gen == shared_gen) |
| 1949 |
__ceph_dentry_dir_lease_touch(di); |
| 1950 |
else |
| 1951 |
valid = 0; |
| 1952 |
spin_unlock(&dentry->d_lock); |
| 1953 |
} |
| 1954 |
doutc(cl, "dir %p %llx.%llx v%u dentry %p '%pd' = %d\n", dir, |
| 1955 |
ceph_vinop(dir), (unsigned)atomic_read(&ci->i_shared_gen), |
| 1956 |
dentry, dentry, valid); |
| 1957 |
return valid; |
| 1958 |
} |
| 1959 |
|
| 1960 |
/* |
| 1961 |
* Check if cached dentry can be trusted. |
| 1962 |
*/ |
| 1963 |
static int ceph_d_revalidate(struct inode *dir, const struct qstr *name, |
| 1964 |
struct dentry *dentry, unsigned int flags) |
| 1965 |
{ |
| 1966 |
struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dentry->d_sb)->mdsc; |
| 1967 |
struct ceph_client *cl = mdsc->fsc->client; |
| 1968 |
int valid = 0; |
| 1969 |
struct inode *inode; |
| 1970 |
|
| 1971 |
valid = fscrypt_d_revalidate(dir, name, dentry, flags); |
| 1972 |
if (valid <= 0) |
| 1973 |
return valid; |
| 1974 |
|
| 1975 |
inode = d_inode_rcu(dentry); |
| 1976 |
|
| 1977 |
doutc(cl, "%p '%pd' inode %p offset 0x%llx nokey %d\n", |
| 1978 |
dentry, dentry, inode, ceph_dentry(dentry)->offset, |
| 1979 |
!!(dentry->d_flags & DCACHE_NOKEY_NAME)); |
| 1980 |
|
| 1981 |
mdsc = ceph_sb_to_fs_client(dir->i_sb)->mdsc; |
| 1982 |
|
| 1983 |
/* always trust cached snapped dentries, snapdir dentry */ |
| 1984 |
if (ceph_snap(dir) != CEPH_NOSNAP) { |
| 1985 |
doutc(cl, "%p '%pd' inode %p is SNAPPED\n", dentry, |
| 1986 |
dentry, inode); |
| 1987 |
valid = 1; |
| 1988 |
} else if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { |
| 1989 |
valid = 1; |
| 1990 |
} else { |
| 1991 |
valid = dentry_lease_is_valid(dentry, flags); |
| 1992 |
if (valid == -ECHILD) |
| 1993 |
return valid; |
| 1994 |
if (valid || dir_lease_is_valid(dir, dentry, mdsc)) { |
| 1995 |
if (inode) |
| 1996 |
valid = ceph_is_any_caps(inode); |
| 1997 |
else |
| 1998 |
valid = 1; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
if (!valid) { |
| 2003 |
struct ceph_mds_request *req; |
| 2004 |
int op, err; |
| 2005 |
u32 mask; |
| 2006 |
|
| 2007 |
if (flags & LOOKUP_RCU) |
| 2008 |
return -ECHILD; |
| 2009 |
|
| 2010 |
percpu_counter_inc(&mdsc->metric.d_lease_mis); |
| 2011 |
|
| 2012 |
op = ceph_snap(dir) == CEPH_SNAPDIR ? |
| 2013 |
CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; |
| 2014 |
req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); |
| 2015 |
if (!IS_ERR(req)) { |
| 2016 |
req->r_dentry = dget(dentry); |
| 2017 |
req->r_num_caps = 2; |
| 2018 |
req->r_parent = dir; |
| 2019 |
ihold(dir); |
| 2020 |
|
| 2021 |
req->r_dname = name; |
| 2022 |
|
| 2023 |
mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED; |
| 2024 |
if (ceph_security_xattr_wanted(dir)) |
| 2025 |
mask |= CEPH_CAP_XATTR_SHARED; |
| 2026 |
req->r_args.getattr.mask = cpu_to_le32(mask); |
| 2027 |
|
| 2028 |
err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 2029 |
switch (err) { |
| 2030 |
case 0: |
| 2031 |
if (d_really_is_positive(dentry) && |
| 2032 |
d_inode(dentry) == req->r_target_inode) |
| 2033 |
valid = 1; |
| 2034 |
break; |
| 2035 |
case -ENOENT: |
| 2036 |
if (d_really_is_negative(dentry)) |
| 2037 |
valid = 1; |
| 2038 |
fallthrough; |
| 2039 |
default: |
| 2040 |
break; |
| 2041 |
} |
| 2042 |
ceph_mdsc_put_request(req); |
| 2043 |
doutc(cl, "%p '%pd', lookup result=%d\n", dentry, |
| 2044 |
dentry, err); |
| 2045 |
} |
| 2046 |
} else { |
| 2047 |
percpu_counter_inc(&mdsc->metric.d_lease_hit); |
| 2048 |
} |
| 2049 |
|
| 2050 |
doutc(cl, "%p '%pd' %s\n", dentry, dentry, valid ? "valid" : "invalid"); |
| 2051 |
if (!valid) |
| 2052 |
ceph_dir_clear_complete(dir); |
| 2053 |
return valid; |
| 2054 |
} |
| 2055 |
|
| 2056 |
/* |
| 2057 |
* Delete unused dentry that doesn't have valid lease |
| 2058 |
* |
| 2059 |
* Called under dentry->d_lock. |
| 2060 |
*/ |
| 2061 |
static int ceph_d_delete(const struct dentry *dentry) |
| 2062 |
{ |
| 2063 |
struct ceph_dentry_info *di; |
| 2064 |
|
| 2065 |
/* won't release caps */ |
| 2066 |
if (d_really_is_negative(dentry)) |
| 2067 |
return 0; |
| 2068 |
if (ceph_snap(d_inode(dentry)) != CEPH_NOSNAP) |
| 2069 |
return 0; |
| 2070 |
/* valid lease? */ |
| 2071 |
di = ceph_dentry(dentry); |
| 2072 |
if (di) { |
| 2073 |
if (__dentry_lease_is_valid(di)) |
| 2074 |
return 0; |
| 2075 |
if (__dir_lease_try_check(dentry)) |
| 2076 |
return 0; |
| 2077 |
} |
| 2078 |
return 1; |
| 2079 |
} |
| 2080 |
|
| 2081 |
/* |
| 2082 |
* Release our ceph_dentry_info. |
| 2083 |
*/ |
| 2084 |
static void ceph_d_release(struct dentry *dentry) |
| 2085 |
{ |
| 2086 |
struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 2087 |
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb); |
| 2088 |
|
| 2089 |
doutc(fsc->client, "dentry %p '%pd'\n", dentry, dentry); |
| 2090 |
|
| 2091 |
atomic64_dec(&fsc->mdsc->metric.total_dentries); |
| 2092 |
|
| 2093 |
spin_lock(&dentry->d_lock); |
| 2094 |
__dentry_lease_unlist(di); |
| 2095 |
dentry->d_fsdata = NULL; |
| 2096 |
spin_unlock(&dentry->d_lock); |
| 2097 |
|
| 2098 |
ceph_put_mds_session(di->lease_session); |
| 2099 |
kmem_cache_free(ceph_dentry_cachep, di); |
| 2100 |
} |
| 2101 |
|
| 2102 |
/* |
| 2103 |
* When the VFS prunes a dentry from the cache, we need to clear the |
| 2104 |
* complete flag on the parent directory. |
| 2105 |
* |
| 2106 |
* Called under dentry->d_lock. |
| 2107 |
*/ |
| 2108 |
static void ceph_d_prune(struct dentry *dentry) |
| 2109 |
{ |
| 2110 |
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb); |
| 2111 |
struct ceph_client *cl = mdsc->fsc->client; |
| 2112 |
struct ceph_inode_info *dir_ci; |
| 2113 |
struct ceph_dentry_info *di; |
| 2114 |
|
| 2115 |
doutc(cl, "dentry %p '%pd'\n", dentry, dentry); |
| 2116 |
|
| 2117 |
/* do we have a valid parent? */ |
| 2118 |
if (IS_ROOT(dentry)) |
| 2119 |
return; |
| 2120 |
|
| 2121 |
/* we hold d_lock, so d_parent is stable */ |
| 2122 |
dir_ci = ceph_inode(d_inode(dentry->d_parent)); |
| 2123 |
if (dir_ci->i_vino.snap == CEPH_SNAPDIR) |
| 2124 |
return; |
| 2125 |
|
| 2126 |
/* who calls d_delete() should also disable dcache readdir */ |
| 2127 |
if (d_really_is_negative(dentry)) |
| 2128 |
return; |
| 2129 |
|
| 2130 |
/* d_fsdata does not get cleared until d_release */ |
| 2131 |
if (!d_unhashed(dentry)) { |
| 2132 |
__ceph_dir_clear_complete(dir_ci); |
| 2133 |
return; |
| 2134 |
} |
| 2135 |
|
| 2136 |
/* Disable dcache readdir just in case that someone called d_drop() |
| 2137 |
* or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED |
| 2138 |
* properly (dcache readdir is still enabled) */ |
| 2139 |
di = ceph_dentry(dentry); |
| 2140 |
if (di->offset > 0 && |
| 2141 |
di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen)) |
| 2142 |
__ceph_dir_clear_ordered(dir_ci); |
| 2143 |
} |
| 2144 |
|
| 2145 |
/* |
| 2146 |
* read() on a dir. This weird interface hack only works if mounted |
| 2147 |
* with '-o dirstat'. |
| 2148 |
*/ |
| 2149 |
static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size, |
| 2150 |
loff_t *ppos) |
| 2151 |
{ |
| 2152 |
struct ceph_dir_file_info *dfi = file->private_data; |
| 2153 |
struct inode *inode = file_inode(file); |
| 2154 |
struct ceph_inode_info *ci = ceph_inode(inode); |
| 2155 |
int left; |
| 2156 |
const int bufsize = 1024; |
| 2157 |
|
| 2158 |
if (!ceph_test_mount_opt(ceph_sb_to_fs_client(inode->i_sb), DIRSTAT)) |
| 2159 |
return -EISDIR; |
| 2160 |
|
| 2161 |
if (!dfi->dir_info) { |
| 2162 |
dfi->dir_info = kmalloc(bufsize, GFP_KERNEL); |
| 2163 |
if (!dfi->dir_info) |
| 2164 |
return -ENOMEM; |
| 2165 |
dfi->dir_info_len = |
| 2166 |
snprintf(dfi->dir_info, bufsize, |
| 2167 |
"entries: %20lld\n" |
| 2168 |
" files: %20lld\n" |
| 2169 |
" subdirs: %20lld\n" |
| 2170 |
"rentries: %20lld\n" |
| 2171 |
" rfiles: %20lld\n" |
| 2172 |
" rsubdirs: %20lld\n" |
| 2173 |
"rbytes: %20lld\n" |
| 2174 |
"rctime: %ptSp\n", |
| 2175 |
ci->i_files + ci->i_subdirs, |
| 2176 |
ci->i_files, |
| 2177 |
ci->i_subdirs, |
| 2178 |
ci->i_rfiles + ci->i_rsubdirs, |
| 2179 |
ci->i_rfiles, |
| 2180 |
ci->i_rsubdirs, |
| 2181 |
ci->i_rbytes, |
| 2182 |
&ci->i_rctime); |
| 2183 |
} |
| 2184 |
|
| 2185 |
if (*ppos >= dfi->dir_info_len) |
| 2186 |
return 0; |
| 2187 |
size = min_t(unsigned, size, dfi->dir_info_len-*ppos); |
| 2188 |
left = copy_to_user(buf, dfi->dir_info + *ppos, size); |
| 2189 |
if (left == size) |
| 2190 |
return -EFAULT; |
| 2191 |
*ppos += (size - left); |
| 2192 |
return size - left; |
| 2193 |
} |
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
/* |
| 2198 |
* Return name hash for a given dentry. This is dependent on |
| 2199 |
* the parent directory's hash function. |
| 2200 |
*/ |
| 2201 |
unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn) |
| 2202 |
{ |
| 2203 |
struct ceph_inode_info *dci = ceph_inode(dir); |
| 2204 |
unsigned hash; |
| 2205 |
|
| 2206 |
switch (dci->i_dir_layout.dl_dir_hash) { |
| 2207 |
case 0: /* for backward compat */ |
| 2208 |
case CEPH_STR_HASH_LINUX: |
| 2209 |
return dn->d_name.hash; |
| 2210 |
|
| 2211 |
default: |
| 2212 |
spin_lock(&dn->d_lock); |
| 2213 |
hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash, |
| 2214 |
dn->d_name.name, dn->d_name.len); |
| 2215 |
spin_unlock(&dn->d_lock); |
| 2216 |
return hash; |
| 2217 |
} |
| 2218 |
} |
| 2219 |
|
| 2220 |
WRAP_DIR_ITER(ceph_readdir) // FIXME! |
| 2221 |
const struct file_operations ceph_dir_fops = { |
| 2222 |
.read = ceph_read_dir, |
| 2223 |
.iterate_shared = shared_ceph_readdir, |
| 2224 |
.llseek = ceph_dir_llseek, |
| 2225 |
.open = ceph_open, |
| 2226 |
.release = ceph_release, |
| 2227 |
.unlocked_ioctl = ceph_ioctl, |
| 2228 |
.compat_ioctl = compat_ptr_ioctl, |
| 2229 |
.fsync = ceph_fsync, |
| 2230 |
.lock = ceph_lock, |
| 2231 |
.flock = ceph_flock, |
| 2232 |
}; |
| 2233 |
|
| 2234 |
const struct file_operations ceph_snapdir_fops = { |
| 2235 |
.iterate_shared = shared_ceph_readdir, |
| 2236 |
.llseek = ceph_dir_llseek, |
| 2237 |
.open = ceph_open, |
| 2238 |
.release = ceph_release, |
| 2239 |
}; |
| 2240 |
|
| 2241 |
const struct inode_operations ceph_dir_iops = { |
| 2242 |
.lookup = ceph_lookup, |
| 2243 |
.permission = ceph_permission, |
| 2244 |
.getattr = ceph_getattr, |
| 2245 |
.setattr = ceph_setattr, |
| 2246 |
.listxattr = ceph_listxattr, |
| 2247 |
.get_inode_acl = ceph_get_acl, |
| 2248 |
.set_acl = ceph_set_acl, |
| 2249 |
.mknod = ceph_mknod, |
| 2250 |
.symlink = ceph_symlink, |
| 2251 |
.mkdir = ceph_mkdir, |
| 2252 |
.link = ceph_link, |
| 2253 |
.unlink = ceph_unlink, |
| 2254 |
.rmdir = ceph_unlink, |
| 2255 |
.rename = ceph_rename, |
| 2256 |
.create = ceph_create, |
| 2257 |
.atomic_open = ceph_atomic_open, |
| 2258 |
}; |
| 2259 |
|
| 2260 |
const struct inode_operations ceph_snapdir_iops = { |
| 2261 |
.lookup = ceph_lookup, |
| 2262 |
.permission = ceph_permission, |
| 2263 |
.getattr = ceph_getattr, |
| 2264 |
.mkdir = ceph_mkdir, |
| 2265 |
.rmdir = ceph_unlink, |
| 2266 |
.rename = ceph_rename, |
| 2267 |
}; |
| 2268 |
|
| 2269 |
const struct dentry_operations ceph_dentry_ops = { |
| 2270 |
.d_revalidate = ceph_d_revalidate, |
| 2271 |
.d_delete = ceph_d_delete, |
| 2272 |
.d_release = ceph_d_release, |
| 2273 |
.d_prune = ceph_d_prune, |
| 2274 |
.d_init = ceph_d_init, |
| 2275 |
}; |