dir.c 34 KB

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