dir.c 34 KB

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