dir.c 34 KB

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