dir.c 36 KB

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