dir.c 36 KB

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