dir.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/spinlock.h>
  3. #include <linux/fs_struct.h>
  4. #include <linux/namei.h>
  5. #include <linux/slab.h>
  6. #include <linux/sched.h>
  7. #include "super.h"
  8. #include "mds_client.h"
  9. /*
  10. * Directory operations: readdir, lookup, create, link, unlink,
  11. * rename, etc.
  12. */
  13. /*
  14. * Ceph MDS operations are specified in terms of a base ino and
  15. * relative path. Thus, the client can specify an operation on a
  16. * specific inode (e.g., a getattr due to fstat(2)), or as a path
  17. * relative to, say, the root directory.
  18. *
  19. * Normally, we limit ourselves to strict inode ops (no path component)
  20. * or dentry operations (a single path component relative to an ino). The
  21. * exception to this is open_root_dentry(), which will open the mount
  22. * point by name.
  23. */
  24. const struct inode_operations ceph_dir_iops;
  25. const struct file_operations ceph_dir_fops;
  26. const struct dentry_operations ceph_dentry_ops;
  27. /*
  28. * Initialize ceph dentry state.
  29. */
  30. int ceph_init_dentry(struct dentry *dentry)
  31. {
  32. struct ceph_dentry_info *di;
  33. if (dentry->d_fsdata)
  34. return 0;
  35. di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS | __GFP_ZERO);
  36. if (!di)
  37. return -ENOMEM; /* oh well */
  38. spin_lock(&dentry->d_lock);
  39. if (dentry->d_fsdata) {
  40. /* lost a race */
  41. kmem_cache_free(ceph_dentry_cachep, di);
  42. goto out_unlock;
  43. }
  44. if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP)
  45. d_set_d_op(dentry, &ceph_dentry_ops);
  46. else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR)
  47. d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
  48. else
  49. d_set_d_op(dentry, &ceph_snap_dentry_ops);
  50. di->dentry = dentry;
  51. di->lease_session = NULL;
  52. dentry->d_time = jiffies;
  53. /* avoid reordering d_fsdata setup so that the check above is safe */
  54. smp_mb();
  55. dentry->d_fsdata = di;
  56. ceph_dentry_lru_add(dentry);
  57. out_unlock:
  58. spin_unlock(&dentry->d_lock);
  59. return 0;
  60. }
  61. struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry)
  62. {
  63. struct inode *inode = NULL;
  64. if (!dentry)
  65. return NULL;
  66. spin_lock(&dentry->d_lock);
  67. if (!IS_ROOT(dentry)) {
  68. inode = dentry->d_parent->d_inode;
  69. ihold(inode);
  70. }
  71. spin_unlock(&dentry->d_lock);
  72. return inode;
  73. }
  74. /*
  75. * for readdir, we encode the directory frag and offset within that
  76. * frag into f_pos.
  77. */
  78. static unsigned fpos_frag(loff_t p)
  79. {
  80. return p >> 32;
  81. }
  82. static unsigned fpos_off(loff_t p)
  83. {
  84. return p & 0xffffffff;
  85. }
  86. /*
  87. * When possible, we try to satisfy a readdir by peeking at the
  88. * dcache. We make this work by carefully ordering dentries on
  89. * d_u.d_child when we initially get results back from the MDS, and
  90. * falling back to a "normal" sync readdir if any dentries in the dir
  91. * are dropped.
  92. *
  93. * Complete dir indicates that we have all dentries in the dir. It is
  94. * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
  95. * the MDS if/when the directory is modified).
  96. */
  97. static int __dcache_readdir(struct file *file, struct dir_context *ctx)
  98. {
  99. struct ceph_file_info *fi = file->private_data;
  100. struct dentry *parent = file->f_dentry;
  101. struct inode *dir = parent->d_inode;
  102. struct list_head *p;
  103. struct dentry *dentry, *last;
  104. struct ceph_dentry_info *di;
  105. int err = 0;
  106. /* claim ref on last dentry we returned */
  107. last = fi->dentry;
  108. fi->dentry = NULL;
  109. dout("__dcache_readdir %p at %llu (last %p)\n", dir, ctx->pos,
  110. last);
  111. spin_lock(&parent->d_lock);
  112. /* start at beginning? */
  113. if (ctx->pos == 2 || last == NULL ||
  114. ctx->pos < ceph_dentry(last)->offset) {
  115. if (list_empty(&parent->d_subdirs))
  116. goto out_unlock;
  117. p = parent->d_subdirs.prev;
  118. dout(" initial p %p/%p\n", p->prev, p->next);
  119. } else {
  120. p = last->d_u.d_child.prev;
  121. }
  122. more:
  123. dentry = list_entry(p, struct dentry, d_u.d_child);
  124. di = ceph_dentry(dentry);
  125. while (1) {
  126. dout(" p %p/%p %s d_subdirs %p/%p\n", p->prev, p->next,
  127. d_unhashed(dentry) ? "!hashed" : "hashed",
  128. parent->d_subdirs.prev, parent->d_subdirs.next);
  129. if (p == &parent->d_subdirs) {
  130. fi->flags |= CEPH_F_ATEND;
  131. goto out_unlock;
  132. }
  133. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  134. if (!d_unhashed(dentry) && dentry->d_inode &&
  135. ceph_snap(dentry->d_inode) != CEPH_SNAPDIR &&
  136. ceph_ino(dentry->d_inode) != CEPH_INO_CEPH &&
  137. ctx->pos <= di->offset)
  138. break;
  139. dout(" skipping %p %.*s at %llu (%llu)%s%s\n", dentry,
  140. dentry->d_name.len, dentry->d_name.name, di->offset,
  141. ctx->pos, d_unhashed(dentry) ? " unhashed" : "",
  142. !dentry->d_inode ? " null" : "");
  143. spin_unlock(&dentry->d_lock);
  144. p = p->prev;
  145. dentry = list_entry(p, struct dentry, d_u.d_child);
  146. di = ceph_dentry(dentry);
  147. }
  148. dget_dlock(dentry);
  149. spin_unlock(&dentry->d_lock);
  150. spin_unlock(&parent->d_lock);
  151. dout(" %llu (%llu) dentry %p %.*s %p\n", di->offset, ctx->pos,
  152. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  153. ctx->pos = di->offset;
  154. if (!dir_emit(ctx, dentry->d_name.name,
  155. dentry->d_name.len,
  156. ceph_translate_ino(dentry->d_sb, dentry->d_inode->i_ino),
  157. dentry->d_inode->i_mode >> 12)) {
  158. if (last) {
  159. /* remember our position */
  160. fi->dentry = last;
  161. fi->next_offset = di->offset;
  162. }
  163. dput(dentry);
  164. return 0;
  165. }
  166. if (last)
  167. dput(last);
  168. last = dentry;
  169. ctx->pos++;
  170. /* make sure a dentry wasn't dropped while we didn't have parent lock */
  171. if (!ceph_dir_is_complete(dir)) {
  172. dout(" lost dir complete on %p; falling back to mds\n", dir);
  173. err = -EAGAIN;
  174. goto out;
  175. }
  176. spin_lock(&parent->d_lock);
  177. p = p->prev; /* advance to next dentry */
  178. goto more;
  179. out_unlock:
  180. spin_unlock(&parent->d_lock);
  181. out:
  182. if (last)
  183. dput(last);
  184. return err;
  185. }
  186. /*
  187. * make note of the last dentry we read, so we can
  188. * continue at the same lexicographical point,
  189. * regardless of what dir changes take place on the
  190. * server.
  191. */
  192. static int note_last_dentry(struct ceph_file_info *fi, const char *name,
  193. int len)
  194. {
  195. kfree(fi->last_name);
  196. fi->last_name = kmalloc(len+1, GFP_NOFS);
  197. if (!fi->last_name)
  198. return -ENOMEM;
  199. memcpy(fi->last_name, name, len);
  200. fi->last_name[len] = 0;
  201. dout("note_last_dentry '%s'\n", fi->last_name);
  202. return 0;
  203. }
  204. static int ceph_readdir(struct file *file, struct dir_context *ctx)
  205. {
  206. struct ceph_file_info *fi = file->private_data;
  207. struct inode *inode = file_inode(file);
  208. struct ceph_inode_info *ci = ceph_inode(inode);
  209. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  210. struct ceph_mds_client *mdsc = fsc->mdsc;
  211. unsigned frag = fpos_frag(ctx->pos);
  212. int off = fpos_off(ctx->pos);
  213. int err;
  214. u32 ftype;
  215. struct ceph_mds_reply_info_parsed *rinfo;
  216. const int max_entries = fsc->mount_options->max_readdir;
  217. const int max_bytes = fsc->mount_options->max_readdir_bytes;
  218. dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
  219. if (fi->flags & CEPH_F_ATEND)
  220. return 0;
  221. /* always start with . and .. */
  222. if (ctx->pos == 0) {
  223. /* note dir version at start of readdir so we can tell
  224. * if any dentries get dropped */
  225. fi->dir_release_count = atomic_read(&ci->i_release_count);
  226. dout("readdir off 0 -> '.'\n");
  227. if (!dir_emit(ctx, ".", 1,
  228. ceph_translate_ino(inode->i_sb, inode->i_ino),
  229. inode->i_mode >> 12))
  230. return 0;
  231. ctx->pos = 1;
  232. off = 1;
  233. }
  234. if (ctx->pos == 1) {
  235. ino_t ino = parent_ino(file->f_dentry);
  236. dout("readdir off 1 -> '..'\n");
  237. if (!dir_emit(ctx, "..", 2,
  238. ceph_translate_ino(inode->i_sb, ino),
  239. inode->i_mode >> 12))
  240. return 0;
  241. ctx->pos = 2;
  242. off = 2;
  243. }
  244. /* can we use the dcache? */
  245. spin_lock(&ci->i_ceph_lock);
  246. if ((ctx->pos == 2 || fi->dentry) &&
  247. !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
  248. ceph_snap(inode) != CEPH_SNAPDIR &&
  249. __ceph_dir_is_complete(ci) &&
  250. __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
  251. spin_unlock(&ci->i_ceph_lock);
  252. err = __dcache_readdir(file, ctx);
  253. if (err != -EAGAIN)
  254. return err;
  255. } else {
  256. spin_unlock(&ci->i_ceph_lock);
  257. }
  258. if (fi->dentry) {
  259. err = note_last_dentry(fi, fi->dentry->d_name.name,
  260. fi->dentry->d_name.len);
  261. if (err)
  262. return err;
  263. dput(fi->dentry);
  264. fi->dentry = NULL;
  265. }
  266. /* proceed with a normal readdir */
  267. more:
  268. /* do we have the correct frag content buffered? */
  269. if (fi->frag != frag || fi->last_readdir == NULL) {
  270. struct ceph_mds_request *req;
  271. int op = ceph_snap(inode) == CEPH_SNAPDIR ?
  272. CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
  273. /* discard old result, if any */
  274. if (fi->last_readdir) {
  275. ceph_mdsc_put_request(fi->last_readdir);
  276. fi->last_readdir = NULL;
  277. }
  278. /* requery frag tree, as the frag topology may have changed */
  279. frag = ceph_choose_frag(ceph_inode(inode), frag, NULL, NULL);
  280. dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
  281. ceph_vinop(inode), frag, fi->last_name);
  282. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  283. if (IS_ERR(req))
  284. return PTR_ERR(req);
  285. req->r_inode = inode;
  286. ihold(inode);
  287. req->r_dentry = dget(file->f_dentry);
  288. /* hints to request -> mds selection code */
  289. req->r_direct_mode = USE_AUTH_MDS;
  290. req->r_direct_hash = ceph_frag_value(frag);
  291. req->r_direct_is_hash = true;
  292. req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
  293. req->r_readdir_offset = fi->next_offset;
  294. req->r_args.readdir.frag = cpu_to_le32(frag);
  295. req->r_args.readdir.max_entries = cpu_to_le32(max_entries);
  296. req->r_args.readdir.max_bytes = cpu_to_le32(max_bytes);
  297. req->r_num_caps = max_entries + 1;
  298. err = ceph_mdsc_do_request(mdsc, NULL, req);
  299. if (err < 0) {
  300. ceph_mdsc_put_request(req);
  301. return err;
  302. }
  303. dout("readdir got and parsed readdir result=%d"
  304. " on frag %x, end=%d, complete=%d\n", err, frag,
  305. (int)req->r_reply_info.dir_end,
  306. (int)req->r_reply_info.dir_complete);
  307. if (!req->r_did_prepopulate) {
  308. dout("readdir !did_prepopulate");
  309. /* preclude from marking dir complete */
  310. fi->dir_release_count--;
  311. }
  312. /* note next offset and last dentry name */
  313. rinfo = &req->r_reply_info;
  314. if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  315. frag = le32_to_cpu(rinfo->dir_dir->frag);
  316. if (ceph_frag_is_leftmost(frag))
  317. fi->next_offset = 2;
  318. else
  319. fi->next_offset = 0;
  320. off = fi->next_offset;
  321. }
  322. fi->offset = fi->next_offset;
  323. fi->last_readdir = req;
  324. fi->frag = frag;
  325. if (req->r_reply_info.dir_end) {
  326. kfree(fi->last_name);
  327. fi->last_name = NULL;
  328. if (ceph_frag_is_rightmost(frag))
  329. fi->next_offset = 2;
  330. else
  331. fi->next_offset = 0;
  332. } else {
  333. err = note_last_dentry(fi,
  334. rinfo->dir_dname[rinfo->dir_nr-1],
  335. rinfo->dir_dname_len[rinfo->dir_nr-1]);
  336. if (err)
  337. return err;
  338. fi->next_offset += rinfo->dir_nr;
  339. }
  340. }
  341. rinfo = &fi->last_readdir->r_reply_info;
  342. dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
  343. rinfo->dir_nr, off, fi->offset);
  344. ctx->pos = ceph_make_fpos(frag, off);
  345. while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
  346. struct ceph_mds_reply_inode *in =
  347. rinfo->dir_in[off - fi->offset].in;
  348. struct ceph_vino vino;
  349. ino_t ino;
  350. dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
  351. off, off - fi->offset, rinfo->dir_nr, ctx->pos,
  352. rinfo->dir_dname_len[off - fi->offset],
  353. rinfo->dir_dname[off - fi->offset], in);
  354. BUG_ON(!in);
  355. ftype = le32_to_cpu(in->mode) >> 12;
  356. vino.ino = le64_to_cpu(in->ino);
  357. vino.snap = le64_to_cpu(in->snapid);
  358. ino = ceph_vino_to_ino(vino);
  359. if (!dir_emit(ctx,
  360. rinfo->dir_dname[off - fi->offset],
  361. rinfo->dir_dname_len[off - fi->offset],
  362. ceph_translate_ino(inode->i_sb, ino), ftype)) {
  363. dout("filldir stopping us...\n");
  364. return 0;
  365. }
  366. off++;
  367. ctx->pos++;
  368. }
  369. if (fi->last_name) {
  370. ceph_mdsc_put_request(fi->last_readdir);
  371. fi->last_readdir = NULL;
  372. goto more;
  373. }
  374. /* more frags? */
  375. if (!ceph_frag_is_rightmost(frag)) {
  376. frag = ceph_frag_next(frag);
  377. off = 0;
  378. ctx->pos = ceph_make_fpos(frag, off);
  379. dout("readdir next frag is %x\n", frag);
  380. goto more;
  381. }
  382. fi->flags |= CEPH_F_ATEND;
  383. /*
  384. * if dir_release_count still matches the dir, no dentries
  385. * were released during the whole readdir, and we should have
  386. * the complete dir contents in our cache.
  387. */
  388. spin_lock(&ci->i_ceph_lock);
  389. if (atomic_read(&ci->i_release_count) == fi->dir_release_count) {
  390. dout(" marking %p complete\n", inode);
  391. __ceph_dir_set_complete(ci, fi->dir_release_count);
  392. ci->i_max_offset = ctx->pos;
  393. }
  394. spin_unlock(&ci->i_ceph_lock);
  395. dout("readdir %p file %p done.\n", inode, file);
  396. return 0;
  397. }
  398. static void reset_readdir(struct ceph_file_info *fi)
  399. {
  400. if (fi->last_readdir) {
  401. ceph_mdsc_put_request(fi->last_readdir);
  402. fi->last_readdir = NULL;
  403. }
  404. kfree(fi->last_name);
  405. fi->last_name = NULL;
  406. fi->next_offset = 2; /* compensate for . and .. */
  407. if (fi->dentry) {
  408. dput(fi->dentry);
  409. fi->dentry = NULL;
  410. }
  411. fi->flags &= ~CEPH_F_ATEND;
  412. }
  413. static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
  414. {
  415. struct ceph_file_info *fi = file->private_data;
  416. struct inode *inode = file->f_mapping->host;
  417. loff_t old_offset = offset;
  418. loff_t retval;
  419. mutex_lock(&inode->i_mutex);
  420. retval = -EINVAL;
  421. switch (whence) {
  422. case SEEK_END:
  423. offset += inode->i_size + 2; /* FIXME */
  424. break;
  425. case SEEK_CUR:
  426. offset += file->f_pos;
  427. case SEEK_SET:
  428. break;
  429. default:
  430. goto out;
  431. }
  432. if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
  433. if (offset != file->f_pos) {
  434. file->f_pos = offset;
  435. file->f_version = 0;
  436. fi->flags &= ~CEPH_F_ATEND;
  437. }
  438. retval = offset;
  439. /*
  440. * discard buffered readdir content on seekdir(0), or
  441. * seek to new frag, or seek prior to current chunk.
  442. */
  443. if (offset == 0 ||
  444. fpos_frag(offset) != fpos_frag(old_offset) ||
  445. fpos_off(offset) < fi->offset) {
  446. dout("dir_llseek dropping %p content\n", file);
  447. reset_readdir(fi);
  448. }
  449. /* bump dir_release_count if we did a forward seek */
  450. if (offset > old_offset)
  451. fi->dir_release_count--;
  452. }
  453. out:
  454. mutex_unlock(&inode->i_mutex);
  455. return retval;
  456. }
  457. /*
  458. * Handle lookups for the hidden .snap directory.
  459. */
  460. int ceph_handle_snapdir(struct ceph_mds_request *req,
  461. struct dentry *dentry, int err)
  462. {
  463. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  464. struct inode *parent = dentry->d_parent->d_inode; /* we hold i_mutex */
  465. /* .snap dir? */
  466. if (err == -ENOENT &&
  467. ceph_snap(parent) == CEPH_NOSNAP &&
  468. strcmp(dentry->d_name.name,
  469. fsc->mount_options->snapdir_name) == 0) {
  470. struct inode *inode = ceph_get_snapdir(parent);
  471. dout("ENOENT on snapdir %p '%.*s', linking to snapdir %p\n",
  472. dentry, dentry->d_name.len, dentry->d_name.name, inode);
  473. BUG_ON(!d_unhashed(dentry));
  474. d_add(dentry, inode);
  475. err = 0;
  476. }
  477. return err;
  478. }
  479. /*
  480. * Figure out final result of a lookup/open request.
  481. *
  482. * Mainly, make sure we return the final req->r_dentry (if it already
  483. * existed) in place of the original VFS-provided dentry when they
  484. * differ.
  485. *
  486. * Gracefully handle the case where the MDS replies with -ENOENT and
  487. * no trace (which it may do, at its discretion, e.g., if it doesn't
  488. * care to issue a lease on the negative dentry).
  489. */
  490. struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
  491. struct dentry *dentry, int err)
  492. {
  493. if (err == -ENOENT) {
  494. /* no trace? */
  495. err = 0;
  496. if (!req->r_reply_info.head->is_dentry) {
  497. dout("ENOENT and no trace, dentry %p inode %p\n",
  498. dentry, dentry->d_inode);
  499. if (dentry->d_inode) {
  500. d_drop(dentry);
  501. err = -ENOENT;
  502. } else {
  503. d_add(dentry, NULL);
  504. }
  505. }
  506. }
  507. if (err)
  508. dentry = ERR_PTR(err);
  509. else if (dentry != req->r_dentry)
  510. dentry = dget(req->r_dentry); /* we got spliced */
  511. else
  512. dentry = NULL;
  513. return dentry;
  514. }
  515. static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
  516. {
  517. return ceph_ino(inode) == CEPH_INO_ROOT &&
  518. strncmp(dentry->d_name.name, ".ceph", 5) == 0;
  519. }
  520. /*
  521. * Look up a single dir entry. If there is a lookup intent, inform
  522. * the MDS so that it gets our 'caps wanted' value in a single op.
  523. */
  524. static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  525. unsigned int flags)
  526. {
  527. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  528. struct ceph_mds_client *mdsc = fsc->mdsc;
  529. struct ceph_mds_request *req;
  530. int op;
  531. int err;
  532. dout("lookup %p dentry %p '%.*s'\n",
  533. dir, dentry, dentry->d_name.len, dentry->d_name.name);
  534. if (dentry->d_name.len > NAME_MAX)
  535. return ERR_PTR(-ENAMETOOLONG);
  536. err = ceph_init_dentry(dentry);
  537. if (err < 0)
  538. return ERR_PTR(err);
  539. /* can we conclude ENOENT locally? */
  540. if (dentry->d_inode == NULL) {
  541. struct ceph_inode_info *ci = ceph_inode(dir);
  542. struct ceph_dentry_info *di = ceph_dentry(dentry);
  543. spin_lock(&ci->i_ceph_lock);
  544. dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
  545. if (strncmp(dentry->d_name.name,
  546. fsc->mount_options->snapdir_name,
  547. dentry->d_name.len) &&
  548. !is_root_ceph_dentry(dir, dentry) &&
  549. __ceph_dir_is_complete(ci) &&
  550. (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
  551. spin_unlock(&ci->i_ceph_lock);
  552. dout(" dir %p complete, -ENOENT\n", dir);
  553. d_add(dentry, NULL);
  554. di->lease_shared_gen = ci->i_shared_gen;
  555. return NULL;
  556. }
  557. spin_unlock(&ci->i_ceph_lock);
  558. }
  559. op = ceph_snap(dir) == CEPH_SNAPDIR ?
  560. CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
  561. req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
  562. if (IS_ERR(req))
  563. return ERR_CAST(req);
  564. req->r_dentry = dget(dentry);
  565. req->r_num_caps = 2;
  566. /* we only need inode linkage */
  567. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  568. req->r_locked_dir = dir;
  569. err = ceph_mdsc_do_request(mdsc, NULL, req);
  570. err = ceph_handle_snapdir(req, dentry, err);
  571. dentry = ceph_finish_lookup(req, dentry, err);
  572. ceph_mdsc_put_request(req); /* will dput(dentry) */
  573. dout("lookup result=%p\n", dentry);
  574. return dentry;
  575. }
  576. /*
  577. * If we do a create but get no trace back from the MDS, follow up with
  578. * a lookup (the VFS expects us to link up the provided dentry).
  579. */
  580. int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
  581. {
  582. struct dentry *result = ceph_lookup(dir, dentry, 0);
  583. if (result && !IS_ERR(result)) {
  584. /*
  585. * We created the item, then did a lookup, and found
  586. * it was already linked to another inode we already
  587. * had in our cache (and thus got spliced). Link our
  588. * dentry to that inode, but don't hash it, just in
  589. * case the VFS wants to dereference it.
  590. */
  591. BUG_ON(!result->d_inode);
  592. d_instantiate(dentry, result->d_inode);
  593. return 0;
  594. }
  595. return PTR_ERR(result);
  596. }
  597. static int ceph_mknod(struct inode *dir, struct dentry *dentry,
  598. umode_t mode, dev_t rdev)
  599. {
  600. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  601. struct ceph_mds_client *mdsc = fsc->mdsc;
  602. struct ceph_mds_request *req;
  603. int err;
  604. if (ceph_snap(dir) != CEPH_NOSNAP)
  605. return -EROFS;
  606. dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
  607. dir, dentry, mode, rdev);
  608. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
  609. if (IS_ERR(req)) {
  610. d_drop(dentry);
  611. return PTR_ERR(req);
  612. }
  613. req->r_dentry = dget(dentry);
  614. req->r_num_caps = 2;
  615. req->r_locked_dir = dir;
  616. req->r_args.mknod.mode = cpu_to_le32(mode);
  617. req->r_args.mknod.rdev = cpu_to_le32(rdev);
  618. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  619. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  620. err = ceph_mdsc_do_request(mdsc, dir, req);
  621. if (!err && !req->r_reply_info.head->is_dentry)
  622. err = ceph_handle_notrace_create(dir, dentry);
  623. ceph_mdsc_put_request(req);
  624. if (err)
  625. d_drop(dentry);
  626. return err;
  627. }
  628. static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  629. bool excl)
  630. {
  631. return ceph_mknod(dir, dentry, mode, 0);
  632. }
  633. static int ceph_symlink(struct inode *dir, struct dentry *dentry,
  634. const char *dest)
  635. {
  636. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  637. struct ceph_mds_client *mdsc = fsc->mdsc;
  638. struct ceph_mds_request *req;
  639. int err;
  640. if (ceph_snap(dir) != CEPH_NOSNAP)
  641. return -EROFS;
  642. dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
  643. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
  644. if (IS_ERR(req)) {
  645. d_drop(dentry);
  646. return PTR_ERR(req);
  647. }
  648. req->r_dentry = dget(dentry);
  649. req->r_num_caps = 2;
  650. req->r_path2 = kstrdup(dest, GFP_NOFS);
  651. req->r_locked_dir = dir;
  652. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  653. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  654. err = ceph_mdsc_do_request(mdsc, dir, req);
  655. if (!err && !req->r_reply_info.head->is_dentry)
  656. err = ceph_handle_notrace_create(dir, dentry);
  657. ceph_mdsc_put_request(req);
  658. if (err)
  659. d_drop(dentry);
  660. return err;
  661. }
  662. static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  663. {
  664. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  665. struct ceph_mds_client *mdsc = fsc->mdsc;
  666. struct ceph_mds_request *req;
  667. int err = -EROFS;
  668. int op;
  669. if (ceph_snap(dir) == CEPH_SNAPDIR) {
  670. /* mkdir .snap/foo is a MKSNAP */
  671. op = CEPH_MDS_OP_MKSNAP;
  672. dout("mksnap dir %p snap '%.*s' dn %p\n", dir,
  673. dentry->d_name.len, dentry->d_name.name, dentry);
  674. } else if (ceph_snap(dir) == CEPH_NOSNAP) {
  675. dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
  676. op = CEPH_MDS_OP_MKDIR;
  677. } else {
  678. goto out;
  679. }
  680. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  681. if (IS_ERR(req)) {
  682. err = PTR_ERR(req);
  683. goto out;
  684. }
  685. req->r_dentry = dget(dentry);
  686. req->r_num_caps = 2;
  687. req->r_locked_dir = dir;
  688. req->r_args.mkdir.mode = cpu_to_le32(mode);
  689. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  690. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  691. err = ceph_mdsc_do_request(mdsc, dir, req);
  692. if (!err && !req->r_reply_info.head->is_dentry)
  693. err = ceph_handle_notrace_create(dir, dentry);
  694. ceph_mdsc_put_request(req);
  695. out:
  696. if (err < 0)
  697. d_drop(dentry);
  698. return err;
  699. }
  700. static int ceph_link(struct dentry *old_dentry, struct inode *dir,
  701. struct dentry *dentry)
  702. {
  703. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  704. struct ceph_mds_client *mdsc = fsc->mdsc;
  705. struct ceph_mds_request *req;
  706. int err;
  707. if (ceph_snap(dir) != CEPH_NOSNAP)
  708. return -EROFS;
  709. dout("link in dir %p old_dentry %p dentry %p\n", dir,
  710. old_dentry, dentry);
  711. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
  712. if (IS_ERR(req)) {
  713. d_drop(dentry);
  714. return PTR_ERR(req);
  715. }
  716. req->r_dentry = dget(dentry);
  717. req->r_num_caps = 2;
  718. req->r_old_dentry = dget(old_dentry); /* or inode? hrm. */
  719. req->r_old_dentry_dir = ceph_get_dentry_parent_inode(old_dentry);
  720. req->r_locked_dir = dir;
  721. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  722. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  723. /* release LINK_SHARED on source inode (mds will lock it) */
  724. req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
  725. err = ceph_mdsc_do_request(mdsc, dir, req);
  726. if (err) {
  727. d_drop(dentry);
  728. } else if (!req->r_reply_info.head->is_dentry) {
  729. ihold(old_dentry->d_inode);
  730. d_instantiate(dentry, old_dentry->d_inode);
  731. }
  732. ceph_mdsc_put_request(req);
  733. return err;
  734. }
  735. /*
  736. * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
  737. * looks like the link count will hit 0, drop any other caps (other
  738. * than PIN) we don't specifically want (due to the file still being
  739. * open).
  740. */
  741. static int drop_caps_for_unlink(struct inode *inode)
  742. {
  743. struct ceph_inode_info *ci = ceph_inode(inode);
  744. int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  745. spin_lock(&ci->i_ceph_lock);
  746. if (inode->i_nlink == 1) {
  747. drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
  748. ci->i_ceph_flags |= CEPH_I_NODELAY;
  749. }
  750. spin_unlock(&ci->i_ceph_lock);
  751. return drop;
  752. }
  753. /*
  754. * rmdir and unlink are differ only by the metadata op code
  755. */
  756. static int ceph_unlink(struct inode *dir, struct dentry *dentry)
  757. {
  758. struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
  759. struct ceph_mds_client *mdsc = fsc->mdsc;
  760. struct inode *inode = dentry->d_inode;
  761. struct ceph_mds_request *req;
  762. int err = -EROFS;
  763. int op;
  764. if (ceph_snap(dir) == CEPH_SNAPDIR) {
  765. /* rmdir .snap/foo is RMSNAP */
  766. dout("rmsnap dir %p '%.*s' dn %p\n", dir, dentry->d_name.len,
  767. dentry->d_name.name, dentry);
  768. op = CEPH_MDS_OP_RMSNAP;
  769. } else if (ceph_snap(dir) == CEPH_NOSNAP) {
  770. dout("unlink/rmdir dir %p dn %p inode %p\n",
  771. dir, dentry, inode);
  772. op = S_ISDIR(dentry->d_inode->i_mode) ?
  773. CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
  774. } else
  775. goto out;
  776. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  777. if (IS_ERR(req)) {
  778. err = PTR_ERR(req);
  779. goto out;
  780. }
  781. req->r_dentry = dget(dentry);
  782. req->r_num_caps = 2;
  783. req->r_locked_dir = dir;
  784. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  785. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  786. req->r_inode_drop = drop_caps_for_unlink(inode);
  787. err = ceph_mdsc_do_request(mdsc, dir, req);
  788. if (!err && !req->r_reply_info.head->is_dentry)
  789. d_delete(dentry);
  790. ceph_mdsc_put_request(req);
  791. out:
  792. return err;
  793. }
  794. static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
  795. struct inode *new_dir, struct dentry *new_dentry)
  796. {
  797. struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
  798. struct ceph_mds_client *mdsc = fsc->mdsc;
  799. struct ceph_mds_request *req;
  800. int err;
  801. if (ceph_snap(old_dir) != ceph_snap(new_dir))
  802. return -EXDEV;
  803. if (ceph_snap(old_dir) != CEPH_NOSNAP ||
  804. ceph_snap(new_dir) != CEPH_NOSNAP)
  805. return -EROFS;
  806. dout("rename dir %p dentry %p to dir %p dentry %p\n",
  807. old_dir, old_dentry, new_dir, new_dentry);
  808. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS);
  809. if (IS_ERR(req))
  810. return PTR_ERR(req);
  811. req->r_dentry = dget(new_dentry);
  812. req->r_num_caps = 2;
  813. req->r_old_dentry = dget(old_dentry);
  814. req->r_old_dentry_dir = ceph_get_dentry_parent_inode(old_dentry);
  815. req->r_locked_dir = new_dir;
  816. req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
  817. req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
  818. req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
  819. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  820. /* release LINK_RDCACHE on source inode (mds will lock it) */
  821. req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
  822. if (new_dentry->d_inode)
  823. req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode);
  824. err = ceph_mdsc_do_request(mdsc, old_dir, req);
  825. if (!err && !req->r_reply_info.head->is_dentry) {
  826. /*
  827. * Normally d_move() is done by fill_trace (called by
  828. * do_request, above). If there is no trace, we need
  829. * to do it here.
  830. */
  831. /* d_move screws up d_subdirs order */
  832. ceph_dir_clear_complete(new_dir);
  833. d_move(old_dentry, new_dentry);
  834. /* ensure target dentry is invalidated, despite
  835. rehashing bug in vfs_rename_dir */
  836. ceph_invalidate_dentry_lease(new_dentry);
  837. }
  838. ceph_mdsc_put_request(req);
  839. return err;
  840. }
  841. /*
  842. * Ensure a dentry lease will no longer revalidate.
  843. */
  844. void ceph_invalidate_dentry_lease(struct dentry *dentry)
  845. {
  846. spin_lock(&dentry->d_lock);
  847. dentry->d_time = jiffies;
  848. ceph_dentry(dentry)->lease_shared_gen = 0;
  849. spin_unlock(&dentry->d_lock);
  850. }
  851. /*
  852. * Check if dentry lease is valid. If not, delete the lease. Try to
  853. * renew if the least is more than half up.
  854. */
  855. static int dentry_lease_is_valid(struct dentry *dentry)
  856. {
  857. struct ceph_dentry_info *di;
  858. struct ceph_mds_session *s;
  859. int valid = 0;
  860. u32 gen;
  861. unsigned long ttl;
  862. struct ceph_mds_session *session = NULL;
  863. struct inode *dir = NULL;
  864. u32 seq = 0;
  865. spin_lock(&dentry->d_lock);
  866. di = ceph_dentry(dentry);
  867. if (di->lease_session) {
  868. s = di->lease_session;
  869. spin_lock(&s->s_gen_ttl_lock);
  870. gen = s->s_cap_gen;
  871. ttl = s->s_cap_ttl;
  872. spin_unlock(&s->s_gen_ttl_lock);
  873. if (di->lease_gen == gen &&
  874. time_before(jiffies, dentry->d_time) &&
  875. time_before(jiffies, ttl)) {
  876. valid = 1;
  877. if (di->lease_renew_after &&
  878. time_after(jiffies, di->lease_renew_after)) {
  879. /* we should renew */
  880. dir = dentry->d_parent->d_inode;
  881. session = ceph_get_mds_session(s);
  882. seq = di->lease_seq;
  883. di->lease_renew_after = 0;
  884. di->lease_renew_from = jiffies;
  885. }
  886. }
  887. }
  888. spin_unlock(&dentry->d_lock);
  889. if (session) {
  890. ceph_mdsc_lease_send_msg(session, dir, dentry,
  891. CEPH_MDS_LEASE_RENEW, seq);
  892. ceph_put_mds_session(session);
  893. }
  894. dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
  895. return valid;
  896. }
  897. /*
  898. * Check if directory-wide content lease/cap is valid.
  899. */
  900. static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
  901. {
  902. struct ceph_inode_info *ci = ceph_inode(dir);
  903. struct ceph_dentry_info *di = ceph_dentry(dentry);
  904. int valid = 0;
  905. spin_lock(&ci->i_ceph_lock);
  906. if (ci->i_shared_gen == di->lease_shared_gen)
  907. valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
  908. spin_unlock(&ci->i_ceph_lock);
  909. dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
  910. dir, (unsigned)ci->i_shared_gen, dentry,
  911. (unsigned)di->lease_shared_gen, valid);
  912. return valid;
  913. }
  914. /*
  915. * Check if cached dentry can be trusted.
  916. */
  917. static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
  918. {
  919. int valid = 0;
  920. struct inode *dir;
  921. if (flags & LOOKUP_RCU)
  922. return -ECHILD;
  923. dout("d_revalidate %p '%.*s' inode %p offset %lld\n", dentry,
  924. dentry->d_name.len, dentry->d_name.name, dentry->d_inode,
  925. ceph_dentry(dentry)->offset);
  926. dir = ceph_get_dentry_parent_inode(dentry);
  927. /* always trust cached snapped dentries, snapdir dentry */
  928. if (ceph_snap(dir) != CEPH_NOSNAP) {
  929. dout("d_revalidate %p '%.*s' inode %p is SNAPPED\n", dentry,
  930. dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  931. valid = 1;
  932. } else if (dentry->d_inode &&
  933. ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) {
  934. valid = 1;
  935. } else if (dentry_lease_is_valid(dentry) ||
  936. dir_lease_is_valid(dir, dentry)) {
  937. valid = 1;
  938. }
  939. dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
  940. if (valid)
  941. ceph_dentry_lru_touch(dentry);
  942. else
  943. d_drop(dentry);
  944. iput(dir);
  945. return valid;
  946. }
  947. /*
  948. * Release our ceph_dentry_info.
  949. */
  950. static void ceph_d_release(struct dentry *dentry)
  951. {
  952. struct ceph_dentry_info *di = ceph_dentry(dentry);
  953. dout("d_release %p\n", dentry);
  954. ceph_dentry_lru_del(dentry);
  955. if (di->lease_session)
  956. ceph_put_mds_session(di->lease_session);
  957. kmem_cache_free(ceph_dentry_cachep, di);
  958. dentry->d_fsdata = NULL;
  959. }
  960. static int ceph_snapdir_d_revalidate(struct dentry *dentry,
  961. unsigned int flags)
  962. {
  963. /*
  964. * Eventually, we'll want to revalidate snapped metadata
  965. * too... probably...
  966. */
  967. return 1;
  968. }
  969. /*
  970. * When the VFS prunes a dentry from the cache, we need to clear the
  971. * complete flag on the parent directory.
  972. *
  973. * Called under dentry->d_lock.
  974. */
  975. static void ceph_d_prune(struct dentry *dentry)
  976. {
  977. dout("ceph_d_prune %p\n", dentry);
  978. /* do we have a valid parent? */
  979. if (IS_ROOT(dentry))
  980. return;
  981. /* if we are not hashed, we don't affect dir's completeness */
  982. if (d_unhashed(dentry))
  983. return;
  984. /*
  985. * we hold d_lock, so d_parent is stable, and d_fsdata is never
  986. * cleared until d_release
  987. */
  988. ceph_dir_clear_complete(dentry->d_parent->d_inode);
  989. }
  990. /*
  991. * read() on a dir. This weird interface hack only works if mounted
  992. * with '-o dirstat'.
  993. */
  994. static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
  995. loff_t *ppos)
  996. {
  997. struct ceph_file_info *cf = file->private_data;
  998. struct inode *inode = file_inode(file);
  999. struct ceph_inode_info *ci = ceph_inode(inode);
  1000. int left;
  1001. const int bufsize = 1024;
  1002. if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
  1003. return -EISDIR;
  1004. if (!cf->dir_info) {
  1005. cf->dir_info = kmalloc(bufsize, GFP_NOFS);
  1006. if (!cf->dir_info)
  1007. return -ENOMEM;
  1008. cf->dir_info_len =
  1009. snprintf(cf->dir_info, bufsize,
  1010. "entries: %20lld\n"
  1011. " files: %20lld\n"
  1012. " subdirs: %20lld\n"
  1013. "rentries: %20lld\n"
  1014. " rfiles: %20lld\n"
  1015. " rsubdirs: %20lld\n"
  1016. "rbytes: %20lld\n"
  1017. "rctime: %10ld.%09ld\n",
  1018. ci->i_files + ci->i_subdirs,
  1019. ci->i_files,
  1020. ci->i_subdirs,
  1021. ci->i_rfiles + ci->i_rsubdirs,
  1022. ci->i_rfiles,
  1023. ci->i_rsubdirs,
  1024. ci->i_rbytes,
  1025. (long)ci->i_rctime.tv_sec,
  1026. (long)ci->i_rctime.tv_nsec);
  1027. }
  1028. if (*ppos >= cf->dir_info_len)
  1029. return 0;
  1030. size = min_t(unsigned, size, cf->dir_info_len-*ppos);
  1031. left = copy_to_user(buf, cf->dir_info + *ppos, size);
  1032. if (left == size)
  1033. return -EFAULT;
  1034. *ppos += (size - left);
  1035. return size - left;
  1036. }
  1037. /*
  1038. * an fsync() on a dir will wait for any uncommitted directory
  1039. * operations to commit.
  1040. */
  1041. static int ceph_dir_fsync(struct file *file, loff_t start, loff_t end,
  1042. int datasync)
  1043. {
  1044. struct inode *inode = file_inode(file);
  1045. struct ceph_inode_info *ci = ceph_inode(inode);
  1046. struct list_head *head = &ci->i_unsafe_dirops;
  1047. struct ceph_mds_request *req;
  1048. u64 last_tid;
  1049. int ret = 0;
  1050. dout("dir_fsync %p\n", inode);
  1051. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1052. if (ret)
  1053. return ret;
  1054. mutex_lock(&inode->i_mutex);
  1055. spin_lock(&ci->i_unsafe_lock);
  1056. if (list_empty(head))
  1057. goto out;
  1058. req = list_entry(head->prev,
  1059. struct ceph_mds_request, r_unsafe_dir_item);
  1060. last_tid = req->r_tid;
  1061. do {
  1062. ceph_mdsc_get_request(req);
  1063. spin_unlock(&ci->i_unsafe_lock);
  1064. dout("dir_fsync %p wait on tid %llu (until %llu)\n",
  1065. inode, req->r_tid, last_tid);
  1066. if (req->r_timeout) {
  1067. ret = wait_for_completion_timeout(
  1068. &req->r_safe_completion, req->r_timeout);
  1069. if (ret > 0)
  1070. ret = 0;
  1071. else if (ret == 0)
  1072. ret = -EIO; /* timed out */
  1073. } else {
  1074. wait_for_completion(&req->r_safe_completion);
  1075. }
  1076. ceph_mdsc_put_request(req);
  1077. spin_lock(&ci->i_unsafe_lock);
  1078. if (ret || list_empty(head))
  1079. break;
  1080. req = list_entry(head->next,
  1081. struct ceph_mds_request, r_unsafe_dir_item);
  1082. } while (req->r_tid < last_tid);
  1083. out:
  1084. spin_unlock(&ci->i_unsafe_lock);
  1085. mutex_unlock(&inode->i_mutex);
  1086. return ret;
  1087. }
  1088. /*
  1089. * We maintain a private dentry LRU.
  1090. *
  1091. * FIXME: this needs to be changed to a per-mds lru to be useful.
  1092. */
  1093. void ceph_dentry_lru_add(struct dentry *dn)
  1094. {
  1095. struct ceph_dentry_info *di = ceph_dentry(dn);
  1096. struct ceph_mds_client *mdsc;
  1097. dout("dentry_lru_add %p %p '%.*s'\n", di, dn,
  1098. dn->d_name.len, dn->d_name.name);
  1099. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1100. spin_lock(&mdsc->dentry_lru_lock);
  1101. list_add_tail(&di->lru, &mdsc->dentry_lru);
  1102. mdsc->num_dentry++;
  1103. spin_unlock(&mdsc->dentry_lru_lock);
  1104. }
  1105. void ceph_dentry_lru_touch(struct dentry *dn)
  1106. {
  1107. struct ceph_dentry_info *di = ceph_dentry(dn);
  1108. struct ceph_mds_client *mdsc;
  1109. dout("dentry_lru_touch %p %p '%.*s' (offset %lld)\n", di, dn,
  1110. dn->d_name.len, dn->d_name.name, di->offset);
  1111. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1112. spin_lock(&mdsc->dentry_lru_lock);
  1113. list_move_tail(&di->lru, &mdsc->dentry_lru);
  1114. spin_unlock(&mdsc->dentry_lru_lock);
  1115. }
  1116. void ceph_dentry_lru_del(struct dentry *dn)
  1117. {
  1118. struct ceph_dentry_info *di = ceph_dentry(dn);
  1119. struct ceph_mds_client *mdsc;
  1120. dout("dentry_lru_del %p %p '%.*s'\n", di, dn,
  1121. dn->d_name.len, dn->d_name.name);
  1122. mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
  1123. spin_lock(&mdsc->dentry_lru_lock);
  1124. list_del_init(&di->lru);
  1125. mdsc->num_dentry--;
  1126. spin_unlock(&mdsc->dentry_lru_lock);
  1127. }
  1128. /*
  1129. * Return name hash for a given dentry. This is dependent on
  1130. * the parent directory's hash function.
  1131. */
  1132. unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
  1133. {
  1134. struct ceph_inode_info *dci = ceph_inode(dir);
  1135. switch (dci->i_dir_layout.dl_dir_hash) {
  1136. case 0: /* for backward compat */
  1137. case CEPH_STR_HASH_LINUX:
  1138. return dn->d_name.hash;
  1139. default:
  1140. return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
  1141. dn->d_name.name, dn->d_name.len);
  1142. }
  1143. }
  1144. const struct file_operations ceph_dir_fops = {
  1145. .read = ceph_read_dir,
  1146. .iterate = ceph_readdir,
  1147. .llseek = ceph_dir_llseek,
  1148. .open = ceph_open,
  1149. .release = ceph_release,
  1150. .unlocked_ioctl = ceph_ioctl,
  1151. .fsync = ceph_dir_fsync,
  1152. };
  1153. const struct inode_operations ceph_dir_iops = {
  1154. .lookup = ceph_lookup,
  1155. .permission = ceph_permission,
  1156. .getattr = ceph_getattr,
  1157. .setattr = ceph_setattr,
  1158. .setxattr = ceph_setxattr,
  1159. .getxattr = ceph_getxattr,
  1160. .listxattr = ceph_listxattr,
  1161. .removexattr = ceph_removexattr,
  1162. .mknod = ceph_mknod,
  1163. .symlink = ceph_symlink,
  1164. .mkdir = ceph_mkdir,
  1165. .link = ceph_link,
  1166. .unlink = ceph_unlink,
  1167. .rmdir = ceph_unlink,
  1168. .rename = ceph_rename,
  1169. .create = ceph_create,
  1170. .atomic_open = ceph_atomic_open,
  1171. };
  1172. const struct dentry_operations ceph_dentry_ops = {
  1173. .d_revalidate = ceph_d_revalidate,
  1174. .d_release = ceph_d_release,
  1175. .d_prune = ceph_d_prune,
  1176. };
  1177. const struct dentry_operations ceph_snapdir_dentry_ops = {
  1178. .d_revalidate = ceph_snapdir_d_revalidate,
  1179. .d_release = ceph_d_release,
  1180. };
  1181. const struct dentry_operations ceph_snap_dentry_ops = {
  1182. .d_release = ceph_d_release,
  1183. .d_prune = ceph_d_prune,
  1184. };