inode.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. #include "ceph_debug.h"
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/smp_lock.h>
  5. #include <linux/slab.h>
  6. #include <linux/string.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/kernel.h>
  9. #include <linux/namei.h>
  10. #include <linux/writeback.h>
  11. #include <linux/vmalloc.h>
  12. #include "super.h"
  13. #include "decode.h"
  14. /*
  15. * Ceph inode operations
  16. *
  17. * Implement basic inode helpers (get, alloc) and inode ops (getattr,
  18. * setattr, etc.), xattr helpers, and helpers for assimilating
  19. * metadata returned by the MDS into our cache.
  20. *
  21. * Also define helpers for doing asynchronous writeback, invalidation,
  22. * and truncation for the benefit of those who can't afford to block
  23. * (typically because they are in the message handler path).
  24. */
  25. static const struct inode_operations ceph_symlink_iops;
  26. static void ceph_inode_invalidate_pages(struct work_struct *work);
  27. /*
  28. * find or create an inode, given the ceph ino number
  29. */
  30. struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
  31. {
  32. struct inode *inode;
  33. ino_t t = ceph_vino_to_ino(vino);
  34. inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
  35. if (inode == NULL)
  36. return ERR_PTR(-ENOMEM);
  37. if (inode->i_state & I_NEW) {
  38. dout("get_inode created new inode %p %llx.%llx ino %llx\n",
  39. inode, ceph_vinop(inode), (u64)inode->i_ino);
  40. unlock_new_inode(inode);
  41. }
  42. dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
  43. vino.snap, inode);
  44. return inode;
  45. }
  46. /*
  47. * get/constuct snapdir inode for a given directory
  48. */
  49. struct inode *ceph_get_snapdir(struct inode *parent)
  50. {
  51. struct ceph_vino vino = {
  52. .ino = ceph_ino(parent),
  53. .snap = CEPH_SNAPDIR,
  54. };
  55. struct inode *inode = ceph_get_inode(parent->i_sb, vino);
  56. BUG_ON(!S_ISDIR(parent->i_mode));
  57. if (IS_ERR(inode))
  58. return ERR_PTR(PTR_ERR(inode));
  59. inode->i_mode = parent->i_mode;
  60. inode->i_uid = parent->i_uid;
  61. inode->i_gid = parent->i_gid;
  62. inode->i_op = &ceph_dir_iops;
  63. inode->i_fop = &ceph_dir_fops;
  64. ceph_inode(inode)->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
  65. return inode;
  66. }
  67. const struct inode_operations ceph_file_iops = {
  68. .permission = ceph_permission,
  69. .setattr = ceph_setattr,
  70. .getattr = ceph_getattr,
  71. .setxattr = ceph_setxattr,
  72. .getxattr = ceph_getxattr,
  73. .listxattr = ceph_listxattr,
  74. .removexattr = ceph_removexattr,
  75. };
  76. /*
  77. * We use a 'frag tree' to keep track of the MDS's directory fragments
  78. * for a given inode (usually there is just a single fragment). We
  79. * need to know when a child frag is delegated to a new MDS, or when
  80. * it is flagged as replicated, so we can direct our requests
  81. * accordingly.
  82. */
  83. /*
  84. * find/create a frag in the tree
  85. */
  86. static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
  87. u32 f)
  88. {
  89. struct rb_node **p;
  90. struct rb_node *parent = NULL;
  91. struct ceph_inode_frag *frag;
  92. int c;
  93. p = &ci->i_fragtree.rb_node;
  94. while (*p) {
  95. parent = *p;
  96. frag = rb_entry(parent, struct ceph_inode_frag, node);
  97. c = ceph_frag_compare(f, frag->frag);
  98. if (c < 0)
  99. p = &(*p)->rb_left;
  100. else if (c > 0)
  101. p = &(*p)->rb_right;
  102. else
  103. return frag;
  104. }
  105. frag = kmalloc(sizeof(*frag), GFP_NOFS);
  106. if (!frag) {
  107. pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
  108. "frag %x\n", &ci->vfs_inode,
  109. ceph_vinop(&ci->vfs_inode), f);
  110. return ERR_PTR(-ENOMEM);
  111. }
  112. frag->frag = f;
  113. frag->split_by = 0;
  114. frag->mds = -1;
  115. frag->ndist = 0;
  116. rb_link_node(&frag->node, parent, p);
  117. rb_insert_color(&frag->node, &ci->i_fragtree);
  118. dout("get_or_create_frag added %llx.%llx frag %x\n",
  119. ceph_vinop(&ci->vfs_inode), f);
  120. return frag;
  121. }
  122. /*
  123. * find a specific frag @f
  124. */
  125. struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
  126. {
  127. struct rb_node *n = ci->i_fragtree.rb_node;
  128. while (n) {
  129. struct ceph_inode_frag *frag =
  130. rb_entry(n, struct ceph_inode_frag, node);
  131. int c = ceph_frag_compare(f, frag->frag);
  132. if (c < 0)
  133. n = n->rb_left;
  134. else if (c > 0)
  135. n = n->rb_right;
  136. else
  137. return frag;
  138. }
  139. return NULL;
  140. }
  141. /*
  142. * Choose frag containing the given value @v. If @pfrag is
  143. * specified, copy the frag delegation info to the caller if
  144. * it is present.
  145. */
  146. u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  147. struct ceph_inode_frag *pfrag,
  148. int *found)
  149. {
  150. u32 t = ceph_frag_make(0, 0);
  151. struct ceph_inode_frag *frag;
  152. unsigned nway, i;
  153. u32 n;
  154. if (found)
  155. *found = 0;
  156. mutex_lock(&ci->i_fragtree_mutex);
  157. while (1) {
  158. WARN_ON(!ceph_frag_contains_value(t, v));
  159. frag = __ceph_find_frag(ci, t);
  160. if (!frag)
  161. break; /* t is a leaf */
  162. if (frag->split_by == 0) {
  163. if (pfrag)
  164. memcpy(pfrag, frag, sizeof(*pfrag));
  165. if (found)
  166. *found = 1;
  167. break;
  168. }
  169. /* choose child */
  170. nway = 1 << frag->split_by;
  171. dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
  172. frag->split_by, nway);
  173. for (i = 0; i < nway; i++) {
  174. n = ceph_frag_make_child(t, frag->split_by, i);
  175. if (ceph_frag_contains_value(n, v)) {
  176. t = n;
  177. break;
  178. }
  179. }
  180. BUG_ON(i == nway);
  181. }
  182. dout("choose_frag(%x) = %x\n", v, t);
  183. mutex_unlock(&ci->i_fragtree_mutex);
  184. return t;
  185. }
  186. /*
  187. * Process dirfrag (delegation) info from the mds. Include leaf
  188. * fragment in tree ONLY if ndist > 0. Otherwise, only
  189. * branches/splits are included in i_fragtree)
  190. */
  191. static int ceph_fill_dirfrag(struct inode *inode,
  192. struct ceph_mds_reply_dirfrag *dirinfo)
  193. {
  194. struct ceph_inode_info *ci = ceph_inode(inode);
  195. struct ceph_inode_frag *frag;
  196. u32 id = le32_to_cpu(dirinfo->frag);
  197. int mds = le32_to_cpu(dirinfo->auth);
  198. int ndist = le32_to_cpu(dirinfo->ndist);
  199. int i;
  200. int err = 0;
  201. mutex_lock(&ci->i_fragtree_mutex);
  202. if (ndist == 0) {
  203. /* no delegation info needed. */
  204. frag = __ceph_find_frag(ci, id);
  205. if (!frag)
  206. goto out;
  207. if (frag->split_by == 0) {
  208. /* tree leaf, remove */
  209. dout("fill_dirfrag removed %llx.%llx frag %x"
  210. " (no ref)\n", ceph_vinop(inode), id);
  211. rb_erase(&frag->node, &ci->i_fragtree);
  212. kfree(frag);
  213. } else {
  214. /* tree branch, keep and clear */
  215. dout("fill_dirfrag cleared %llx.%llx frag %x"
  216. " referral\n", ceph_vinop(inode), id);
  217. frag->mds = -1;
  218. frag->ndist = 0;
  219. }
  220. goto out;
  221. }
  222. /* find/add this frag to store mds delegation info */
  223. frag = __get_or_create_frag(ci, id);
  224. if (IS_ERR(frag)) {
  225. /* this is not the end of the world; we can continue
  226. with bad/inaccurate delegation info */
  227. pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
  228. ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
  229. err = -ENOMEM;
  230. goto out;
  231. }
  232. frag->mds = mds;
  233. frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
  234. for (i = 0; i < frag->ndist; i++)
  235. frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
  236. dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
  237. ceph_vinop(inode), frag->frag, frag->ndist);
  238. out:
  239. mutex_unlock(&ci->i_fragtree_mutex);
  240. return err;
  241. }
  242. /*
  243. * initialize a newly allocated inode.
  244. */
  245. struct inode *ceph_alloc_inode(struct super_block *sb)
  246. {
  247. struct ceph_inode_info *ci;
  248. int i;
  249. ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
  250. if (!ci)
  251. return NULL;
  252. dout("alloc_inode %p\n", &ci->vfs_inode);
  253. ci->i_version = 0;
  254. ci->i_time_warp_seq = 0;
  255. ci->i_ceph_flags = 0;
  256. ci->i_release_count = 0;
  257. ci->i_symlink = NULL;
  258. ci->i_fragtree = RB_ROOT;
  259. mutex_init(&ci->i_fragtree_mutex);
  260. ci->i_xattrs.blob = NULL;
  261. ci->i_xattrs.prealloc_blob = NULL;
  262. ci->i_xattrs.dirty = false;
  263. ci->i_xattrs.index = RB_ROOT;
  264. ci->i_xattrs.count = 0;
  265. ci->i_xattrs.names_size = 0;
  266. ci->i_xattrs.vals_size = 0;
  267. ci->i_xattrs.version = 0;
  268. ci->i_xattrs.index_version = 0;
  269. ci->i_caps = RB_ROOT;
  270. ci->i_auth_cap = NULL;
  271. ci->i_dirty_caps = 0;
  272. ci->i_flushing_caps = 0;
  273. INIT_LIST_HEAD(&ci->i_dirty_item);
  274. INIT_LIST_HEAD(&ci->i_flushing_item);
  275. ci->i_cap_flush_seq = 0;
  276. ci->i_cap_flush_last_tid = 0;
  277. memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid));
  278. init_waitqueue_head(&ci->i_cap_wq);
  279. ci->i_hold_caps_min = 0;
  280. ci->i_hold_caps_max = 0;
  281. INIT_LIST_HEAD(&ci->i_cap_delay_list);
  282. ci->i_cap_exporting_mds = 0;
  283. ci->i_cap_exporting_mseq = 0;
  284. ci->i_cap_exporting_issued = 0;
  285. INIT_LIST_HEAD(&ci->i_cap_snaps);
  286. ci->i_head_snapc = NULL;
  287. ci->i_snap_caps = 0;
  288. for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
  289. ci->i_nr_by_mode[i] = 0;
  290. ci->i_truncate_seq = 0;
  291. ci->i_truncate_size = 0;
  292. ci->i_truncate_pending = 0;
  293. ci->i_max_size = 0;
  294. ci->i_reported_size = 0;
  295. ci->i_wanted_max_size = 0;
  296. ci->i_requested_max_size = 0;
  297. ci->i_pin_ref = 0;
  298. ci->i_rd_ref = 0;
  299. ci->i_rdcache_ref = 0;
  300. ci->i_wr_ref = 0;
  301. ci->i_wrbuffer_ref = 0;
  302. ci->i_wrbuffer_ref_head = 0;
  303. ci->i_shared_gen = 0;
  304. ci->i_rdcache_gen = 0;
  305. ci->i_rdcache_revoking = 0;
  306. INIT_LIST_HEAD(&ci->i_unsafe_writes);
  307. INIT_LIST_HEAD(&ci->i_unsafe_dirops);
  308. spin_lock_init(&ci->i_unsafe_lock);
  309. ci->i_snap_realm = NULL;
  310. INIT_LIST_HEAD(&ci->i_snap_realm_item);
  311. INIT_LIST_HEAD(&ci->i_snap_flush_item);
  312. INIT_WORK(&ci->i_wb_work, ceph_inode_writeback);
  313. INIT_WORK(&ci->i_pg_inv_work, ceph_inode_invalidate_pages);
  314. INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
  315. return &ci->vfs_inode;
  316. }
  317. void ceph_destroy_inode(struct inode *inode)
  318. {
  319. struct ceph_inode_info *ci = ceph_inode(inode);
  320. struct ceph_inode_frag *frag;
  321. struct rb_node *n;
  322. dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
  323. ceph_queue_caps_release(inode);
  324. kfree(ci->i_symlink);
  325. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  326. frag = rb_entry(n, struct ceph_inode_frag, node);
  327. rb_erase(n, &ci->i_fragtree);
  328. kfree(frag);
  329. }
  330. __ceph_destroy_xattrs(ci);
  331. ceph_buffer_put(ci->i_xattrs.blob);
  332. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  333. kmem_cache_free(ceph_inode_cachep, ci);
  334. }
  335. /*
  336. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  337. * careful because either the client or MDS may have more up to date
  338. * info, depending on which capabilities are held, and whether
  339. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  340. * and size are monotonically increasing, except when utimes() or
  341. * truncate() increments the corresponding _seq values.)
  342. */
  343. int ceph_fill_file_size(struct inode *inode, int issued,
  344. u32 truncate_seq, u64 truncate_size, u64 size)
  345. {
  346. struct ceph_inode_info *ci = ceph_inode(inode);
  347. int queue_trunc = 0;
  348. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  349. (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
  350. dout("size %lld -> %llu\n", inode->i_size, size);
  351. inode->i_size = size;
  352. inode->i_blocks = (size + (1<<9) - 1) >> 9;
  353. ci->i_reported_size = size;
  354. if (truncate_seq != ci->i_truncate_seq) {
  355. dout("truncate_seq %u -> %u\n",
  356. ci->i_truncate_seq, truncate_seq);
  357. ci->i_truncate_seq = truncate_seq;
  358. if (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD|
  359. CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER|
  360. CEPH_CAP_FILE_EXCL)) {
  361. ci->i_truncate_pending++;
  362. queue_trunc = 1;
  363. }
  364. }
  365. }
  366. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
  367. ci->i_truncate_size != truncate_size) {
  368. dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
  369. truncate_size);
  370. ci->i_truncate_size = truncate_size;
  371. }
  372. return queue_trunc;
  373. }
  374. void ceph_fill_file_time(struct inode *inode, int issued,
  375. u64 time_warp_seq, struct timespec *ctime,
  376. struct timespec *mtime, struct timespec *atime)
  377. {
  378. struct ceph_inode_info *ci = ceph_inode(inode);
  379. int warn = 0;
  380. if (issued & (CEPH_CAP_FILE_EXCL|
  381. CEPH_CAP_FILE_WR|
  382. CEPH_CAP_FILE_BUFFER)) {
  383. if (timespec_compare(ctime, &inode->i_ctime) > 0) {
  384. dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
  385. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  386. ctime->tv_sec, ctime->tv_nsec);
  387. inode->i_ctime = *ctime;
  388. }
  389. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  390. /* the MDS did a utimes() */
  391. dout("mtime %ld.%09ld -> %ld.%09ld "
  392. "tw %d -> %d\n",
  393. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  394. mtime->tv_sec, mtime->tv_nsec,
  395. ci->i_time_warp_seq, (int)time_warp_seq);
  396. inode->i_mtime = *mtime;
  397. inode->i_atime = *atime;
  398. ci->i_time_warp_seq = time_warp_seq;
  399. } else if (time_warp_seq == ci->i_time_warp_seq) {
  400. /* nobody did utimes(); take the max */
  401. if (timespec_compare(mtime, &inode->i_mtime) > 0) {
  402. dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
  403. inode->i_mtime.tv_sec,
  404. inode->i_mtime.tv_nsec,
  405. mtime->tv_sec, mtime->tv_nsec);
  406. inode->i_mtime = *mtime;
  407. }
  408. if (timespec_compare(atime, &inode->i_atime) > 0) {
  409. dout("atime %ld.%09ld -> %ld.%09ld inc\n",
  410. inode->i_atime.tv_sec,
  411. inode->i_atime.tv_nsec,
  412. atime->tv_sec, atime->tv_nsec);
  413. inode->i_atime = *atime;
  414. }
  415. } else if (issued & CEPH_CAP_FILE_EXCL) {
  416. /* we did a utimes(); ignore mds values */
  417. } else {
  418. warn = 1;
  419. }
  420. } else {
  421. /* we have no write caps; whatever the MDS says is true */
  422. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  423. inode->i_ctime = *ctime;
  424. inode->i_mtime = *mtime;
  425. inode->i_atime = *atime;
  426. ci->i_time_warp_seq = time_warp_seq;
  427. } else {
  428. warn = 1;
  429. }
  430. }
  431. if (warn) /* time_warp_seq shouldn't go backwards */
  432. dout("%p mds time_warp_seq %llu < %u\n",
  433. inode, time_warp_seq, ci->i_time_warp_seq);
  434. }
  435. /*
  436. * Populate an inode based on info from mds. May be called on new or
  437. * existing inodes.
  438. */
  439. static int fill_inode(struct inode *inode,
  440. struct ceph_mds_reply_info_in *iinfo,
  441. struct ceph_mds_reply_dirfrag *dirinfo,
  442. struct ceph_mds_session *session,
  443. unsigned long ttl_from, int cap_fmode,
  444. struct ceph_cap_reservation *caps_reservation)
  445. {
  446. struct ceph_mds_reply_inode *info = iinfo->in;
  447. struct ceph_inode_info *ci = ceph_inode(inode);
  448. int i;
  449. int issued, implemented;
  450. struct timespec mtime, atime, ctime;
  451. u32 nsplits;
  452. struct ceph_buffer *xattr_blob = NULL;
  453. int err = 0;
  454. int queue_trunc = 0;
  455. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  456. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  457. ci->i_version);
  458. /*
  459. * prealloc xattr data, if it looks like we'll need it. only
  460. * if len > 4 (meaning there are actually xattrs; the first 4
  461. * bytes are the xattr count).
  462. */
  463. if (iinfo->xattr_len > 4) {
  464. xattr_blob = ceph_buffer_new_alloc(iinfo->xattr_len, GFP_NOFS);
  465. if (!xattr_blob)
  466. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  467. iinfo->xattr_len);
  468. }
  469. spin_lock(&inode->i_lock);
  470. /*
  471. * provided version will be odd if inode value is projected,
  472. * even if stable. skip the update if we have a newer info
  473. * (e.g., due to inode info racing form multiple MDSs), or if
  474. * we are getting projected (unstable) inode info.
  475. */
  476. if (le64_to_cpu(info->version) > 0 &&
  477. (ci->i_version & ~1) > le64_to_cpu(info->version))
  478. goto no_change;
  479. issued = __ceph_caps_issued(ci, &implemented);
  480. issued |= implemented | __ceph_caps_dirty(ci);
  481. /* update inode */
  482. ci->i_version = le64_to_cpu(info->version);
  483. inode->i_version++;
  484. inode->i_rdev = le32_to_cpu(info->rdev);
  485. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  486. inode->i_mode = le32_to_cpu(info->mode);
  487. inode->i_uid = le32_to_cpu(info->uid);
  488. inode->i_gid = le32_to_cpu(info->gid);
  489. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  490. inode->i_uid, inode->i_gid);
  491. }
  492. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  493. inode->i_nlink = le32_to_cpu(info->nlink);
  494. /* be careful with mtime, atime, size */
  495. ceph_decode_timespec(&atime, &info->atime);
  496. ceph_decode_timespec(&mtime, &info->mtime);
  497. ceph_decode_timespec(&ctime, &info->ctime);
  498. queue_trunc = ceph_fill_file_size(inode, issued,
  499. le32_to_cpu(info->truncate_seq),
  500. le64_to_cpu(info->truncate_size),
  501. le64_to_cpu(info->size));
  502. ceph_fill_file_time(inode, issued,
  503. le32_to_cpu(info->time_warp_seq),
  504. &ctime, &mtime, &atime);
  505. ci->i_max_size = le64_to_cpu(info->max_size);
  506. ci->i_layout = info->layout;
  507. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  508. /* xattrs */
  509. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  510. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  511. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  512. if (ci->i_xattrs.blob)
  513. ceph_buffer_put(ci->i_xattrs.blob);
  514. ci->i_xattrs.blob = xattr_blob;
  515. if (xattr_blob)
  516. memcpy(ci->i_xattrs.blob->vec.iov_base,
  517. iinfo->xattr_data, iinfo->xattr_len);
  518. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  519. }
  520. inode->i_mapping->a_ops = &ceph_aops;
  521. inode->i_mapping->backing_dev_info =
  522. &ceph_client(inode->i_sb)->backing_dev_info;
  523. switch (inode->i_mode & S_IFMT) {
  524. case S_IFIFO:
  525. case S_IFBLK:
  526. case S_IFCHR:
  527. case S_IFSOCK:
  528. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  529. inode->i_op = &ceph_file_iops;
  530. break;
  531. case S_IFREG:
  532. inode->i_op = &ceph_file_iops;
  533. inode->i_fop = &ceph_file_fops;
  534. break;
  535. case S_IFLNK:
  536. inode->i_op = &ceph_symlink_iops;
  537. if (!ci->i_symlink) {
  538. int symlen = iinfo->symlink_len;
  539. char *sym;
  540. BUG_ON(symlen != inode->i_size);
  541. spin_unlock(&inode->i_lock);
  542. err = -ENOMEM;
  543. sym = kmalloc(symlen+1, GFP_NOFS);
  544. if (!sym)
  545. goto out;
  546. memcpy(sym, iinfo->symlink, symlen);
  547. sym[symlen] = 0;
  548. spin_lock(&inode->i_lock);
  549. if (!ci->i_symlink)
  550. ci->i_symlink = sym;
  551. else
  552. kfree(sym); /* lost a race */
  553. }
  554. break;
  555. case S_IFDIR:
  556. inode->i_op = &ceph_dir_iops;
  557. inode->i_fop = &ceph_dir_fops;
  558. ci->i_files = le64_to_cpu(info->files);
  559. ci->i_subdirs = le64_to_cpu(info->subdirs);
  560. ci->i_rbytes = le64_to_cpu(info->rbytes);
  561. ci->i_rfiles = le64_to_cpu(info->rfiles);
  562. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  563. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  564. /* set dir completion flag? */
  565. if (ci->i_files == 0 && ci->i_subdirs == 0 &&
  566. ceph_snap(inode) == CEPH_NOSNAP &&
  567. (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) {
  568. dout(" marking %p complete (empty)\n", inode);
  569. ci->i_ceph_flags |= CEPH_I_COMPLETE;
  570. ci->i_max_offset = 2;
  571. }
  572. /* it may be better to set st_size in getattr instead? */
  573. if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES))
  574. inode->i_size = ci->i_rbytes;
  575. break;
  576. default:
  577. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  578. ceph_vinop(inode), inode->i_mode);
  579. }
  580. no_change:
  581. spin_unlock(&inode->i_lock);
  582. /* queue truncate if we saw i_size decrease */
  583. if (queue_trunc)
  584. if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
  585. &ci->i_vmtruncate_work))
  586. igrab(inode);
  587. /* populate frag tree */
  588. /* FIXME: move me up, if/when version reflects fragtree changes */
  589. nsplits = le32_to_cpu(info->fragtree.nsplits);
  590. mutex_lock(&ci->i_fragtree_mutex);
  591. for (i = 0; i < nsplits; i++) {
  592. u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
  593. struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
  594. if (IS_ERR(frag))
  595. continue;
  596. frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
  597. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  598. }
  599. mutex_unlock(&ci->i_fragtree_mutex);
  600. /* were we issued a capability? */
  601. if (info->cap.caps) {
  602. if (ceph_snap(inode) == CEPH_NOSNAP) {
  603. ceph_add_cap(inode, session,
  604. le64_to_cpu(info->cap.cap_id),
  605. cap_fmode,
  606. le32_to_cpu(info->cap.caps),
  607. le32_to_cpu(info->cap.wanted),
  608. le32_to_cpu(info->cap.seq),
  609. le32_to_cpu(info->cap.mseq),
  610. le64_to_cpu(info->cap.realm),
  611. info->cap.flags,
  612. caps_reservation);
  613. } else {
  614. spin_lock(&inode->i_lock);
  615. dout(" %p got snap_caps %s\n", inode,
  616. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  617. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  618. if (cap_fmode >= 0)
  619. __ceph_get_fmode(ci, cap_fmode);
  620. spin_unlock(&inode->i_lock);
  621. }
  622. }
  623. /* update delegation info? */
  624. if (dirinfo)
  625. ceph_fill_dirfrag(inode, dirinfo);
  626. err = 0;
  627. out:
  628. ceph_buffer_put(xattr_blob);
  629. return err;
  630. }
  631. /*
  632. * caller should hold session s_mutex.
  633. */
  634. static void update_dentry_lease(struct dentry *dentry,
  635. struct ceph_mds_reply_lease *lease,
  636. struct ceph_mds_session *session,
  637. unsigned long from_time)
  638. {
  639. struct ceph_dentry_info *di = ceph_dentry(dentry);
  640. long unsigned duration = le32_to_cpu(lease->duration_ms);
  641. long unsigned ttl = from_time + (duration * HZ) / 1000;
  642. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  643. struct inode *dir;
  644. /* only track leases on regular dentries */
  645. if (dentry->d_op != &ceph_dentry_ops)
  646. return;
  647. spin_lock(&dentry->d_lock);
  648. dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
  649. dentry, le16_to_cpu(lease->mask), duration, ttl);
  650. /* make lease_rdcache_gen match directory */
  651. dir = dentry->d_parent->d_inode;
  652. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  653. if (lease->mask == 0)
  654. goto out_unlock;
  655. if (di->lease_gen == session->s_cap_gen &&
  656. time_before(ttl, dentry->d_time))
  657. goto out_unlock; /* we already have a newer lease. */
  658. if (di->lease_session && di->lease_session != session)
  659. goto out_unlock;
  660. ceph_dentry_lru_touch(dentry);
  661. if (!di->lease_session)
  662. di->lease_session = ceph_get_mds_session(session);
  663. di->lease_gen = session->s_cap_gen;
  664. di->lease_seq = le32_to_cpu(lease->seq);
  665. di->lease_renew_after = half_ttl;
  666. di->lease_renew_from = 0;
  667. dentry->d_time = ttl;
  668. out_unlock:
  669. spin_unlock(&dentry->d_lock);
  670. return;
  671. }
  672. /*
  673. * splice a dentry to an inode.
  674. * caller must hold directory i_mutex for this to be safe.
  675. *
  676. * we will only rehash the resulting dentry if @prehash is
  677. * true; @prehash will be set to false (for the benefit of
  678. * the caller) if we fail.
  679. */
  680. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
  681. bool *prehash)
  682. {
  683. struct dentry *realdn;
  684. /* dn must be unhashed */
  685. if (!d_unhashed(dn))
  686. d_drop(dn);
  687. realdn = d_materialise_unique(dn, in);
  688. if (IS_ERR(realdn)) {
  689. pr_err("splice_dentry error %p inode %p ino %llx.%llx\n",
  690. dn, in, ceph_vinop(in));
  691. if (prehash)
  692. *prehash = false; /* don't rehash on error */
  693. dn = realdn; /* note realdn contains the error */
  694. goto out;
  695. } else if (realdn) {
  696. dout("dn %p (%d) spliced with %p (%d) "
  697. "inode %p ino %llx.%llx\n",
  698. dn, atomic_read(&dn->d_count),
  699. realdn, atomic_read(&realdn->d_count),
  700. realdn->d_inode, ceph_vinop(realdn->d_inode));
  701. dput(dn);
  702. dn = realdn;
  703. } else {
  704. BUG_ON(!ceph_dentry(dn));
  705. dout("dn %p attached to %p ino %llx.%llx\n",
  706. dn, dn->d_inode, ceph_vinop(dn->d_inode));
  707. }
  708. if ((!prehash || *prehash) && d_unhashed(dn))
  709. d_rehash(dn);
  710. out:
  711. return dn;
  712. }
  713. /*
  714. * Incorporate results into the local cache. This is either just
  715. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  716. * after a lookup).
  717. *
  718. * A reply may contain
  719. * a directory inode along with a dentry.
  720. * and/or a target inode
  721. *
  722. * Called with snap_rwsem (read).
  723. */
  724. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
  725. struct ceph_mds_session *session)
  726. {
  727. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  728. struct inode *in = NULL;
  729. struct ceph_mds_reply_inode *ininfo;
  730. struct ceph_vino vino;
  731. int i = 0;
  732. int err = 0;
  733. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  734. rinfo->head->is_dentry, rinfo->head->is_target);
  735. #if 0
  736. /*
  737. * Debugging hook:
  738. *
  739. * If we resend completed ops to a recovering mds, we get no
  740. * trace. Since that is very rare, pretend this is the case
  741. * to ensure the 'no trace' handlers in the callers behave.
  742. *
  743. * Fill in inodes unconditionally to avoid breaking cap
  744. * invariants.
  745. */
  746. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  747. pr_info("fill_trace faking empty trace on %lld %s\n",
  748. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  749. if (rinfo->head->is_dentry) {
  750. rinfo->head->is_dentry = 0;
  751. err = fill_inode(req->r_locked_dir,
  752. &rinfo->diri, rinfo->dirfrag,
  753. session, req->r_request_started, -1);
  754. }
  755. if (rinfo->head->is_target) {
  756. rinfo->head->is_target = 0;
  757. ininfo = rinfo->targeti.in;
  758. vino.ino = le64_to_cpu(ininfo->ino);
  759. vino.snap = le64_to_cpu(ininfo->snapid);
  760. in = ceph_get_inode(sb, vino);
  761. err = fill_inode(in, &rinfo->targeti, NULL,
  762. session, req->r_request_started,
  763. req->r_fmode);
  764. iput(in);
  765. }
  766. }
  767. #endif
  768. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  769. dout("fill_trace reply is empty!\n");
  770. if (rinfo->head->result == 0 && req->r_locked_dir) {
  771. struct ceph_inode_info *ci =
  772. ceph_inode(req->r_locked_dir);
  773. dout(" clearing %p complete (empty trace)\n",
  774. req->r_locked_dir);
  775. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  776. ci->i_release_count++;
  777. }
  778. return 0;
  779. }
  780. if (rinfo->head->is_dentry) {
  781. /*
  782. * lookup link rename : null -> possibly existing inode
  783. * mknod symlink mkdir : null -> new inode
  784. * unlink : linked -> null
  785. */
  786. struct inode *dir = req->r_locked_dir;
  787. struct dentry *dn = req->r_dentry;
  788. bool have_dir_cap, have_lease;
  789. BUG_ON(!dn);
  790. BUG_ON(!dir);
  791. BUG_ON(dn->d_parent->d_inode != dir);
  792. BUG_ON(ceph_ino(dir) !=
  793. le64_to_cpu(rinfo->diri.in->ino));
  794. BUG_ON(ceph_snap(dir) !=
  795. le64_to_cpu(rinfo->diri.in->snapid));
  796. err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
  797. session, req->r_request_started, -1,
  798. &req->r_caps_reservation);
  799. if (err < 0)
  800. return err;
  801. /* do we have a lease on the whole dir? */
  802. have_dir_cap =
  803. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  804. CEPH_CAP_FILE_SHARED);
  805. /* do we have a dn lease? */
  806. have_lease = have_dir_cap ||
  807. (le16_to_cpu(rinfo->dlease->mask) &
  808. CEPH_LOCK_DN);
  809. if (!have_lease)
  810. dout("fill_trace no dentry lease or dir cap\n");
  811. /* rename? */
  812. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  813. dout(" src %p '%.*s' dst %p '%.*s'\n",
  814. req->r_old_dentry,
  815. req->r_old_dentry->d_name.len,
  816. req->r_old_dentry->d_name.name,
  817. dn, dn->d_name.len, dn->d_name.name);
  818. dout("fill_trace doing d_move %p -> %p\n",
  819. req->r_old_dentry, dn);
  820. d_move(req->r_old_dentry, dn);
  821. dout(" src %p '%.*s' dst %p '%.*s'\n",
  822. req->r_old_dentry,
  823. req->r_old_dentry->d_name.len,
  824. req->r_old_dentry->d_name.name,
  825. dn, dn->d_name.len, dn->d_name.name);
  826. /* take overwritten dentry's readdir offset */
  827. ceph_dentry(req->r_old_dentry)->offset =
  828. ceph_dentry(dn)->offset;
  829. dn = req->r_old_dentry; /* use old_dentry */
  830. in = dn->d_inode;
  831. }
  832. /* null dentry? */
  833. if (!rinfo->head->is_target) {
  834. dout("fill_trace null dentry\n");
  835. if (dn->d_inode) {
  836. dout("d_delete %p\n", dn);
  837. d_delete(dn);
  838. } else {
  839. dout("d_instantiate %p NULL\n", dn);
  840. d_instantiate(dn, NULL);
  841. if (have_lease && d_unhashed(dn))
  842. d_rehash(dn);
  843. update_dentry_lease(dn, rinfo->dlease,
  844. session,
  845. req->r_request_started);
  846. }
  847. goto done;
  848. }
  849. /* attach proper inode */
  850. ininfo = rinfo->targeti.in;
  851. vino.ino = le64_to_cpu(ininfo->ino);
  852. vino.snap = le64_to_cpu(ininfo->snapid);
  853. if (!dn->d_inode) {
  854. in = ceph_get_inode(sb, vino);
  855. if (IS_ERR(in)) {
  856. pr_err("fill_trace bad get_inode "
  857. "%llx.%llx\n", vino.ino, vino.snap);
  858. err = PTR_ERR(in);
  859. d_delete(dn);
  860. goto done;
  861. }
  862. dn = splice_dentry(dn, in, &have_lease);
  863. if (IS_ERR(dn)) {
  864. err = PTR_ERR(dn);
  865. goto done;
  866. }
  867. req->r_dentry = dn; /* may have spliced */
  868. igrab(in);
  869. } else if (ceph_ino(in) == vino.ino &&
  870. ceph_snap(in) == vino.snap) {
  871. igrab(in);
  872. } else {
  873. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  874. dn, in, ceph_ino(in), ceph_snap(in),
  875. vino.ino, vino.snap);
  876. have_lease = false;
  877. in = NULL;
  878. }
  879. if (have_lease)
  880. update_dentry_lease(dn, rinfo->dlease, session,
  881. req->r_request_started);
  882. dout(" final dn %p\n", dn);
  883. i++;
  884. } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  885. req->r_op == CEPH_MDS_OP_MKSNAP) {
  886. struct dentry *dn = req->r_dentry;
  887. /* fill out a snapdir LOOKUPSNAP dentry */
  888. BUG_ON(!dn);
  889. BUG_ON(!req->r_locked_dir);
  890. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  891. ininfo = rinfo->targeti.in;
  892. vino.ino = le64_to_cpu(ininfo->ino);
  893. vino.snap = le64_to_cpu(ininfo->snapid);
  894. in = ceph_get_inode(sb, vino);
  895. if (IS_ERR(in)) {
  896. pr_err("fill_inode get_inode badness %llx.%llx\n",
  897. vino.ino, vino.snap);
  898. err = PTR_ERR(in);
  899. d_delete(dn);
  900. goto done;
  901. }
  902. dout(" linking snapped dir %p to dn %p\n", in, dn);
  903. dn = splice_dentry(dn, in, NULL);
  904. if (IS_ERR(dn)) {
  905. err = PTR_ERR(dn);
  906. goto done;
  907. }
  908. req->r_dentry = dn; /* may have spliced */
  909. igrab(in);
  910. rinfo->head->is_dentry = 1; /* fool notrace handlers */
  911. }
  912. if (rinfo->head->is_target) {
  913. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  914. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  915. if (in == NULL || ceph_ino(in) != vino.ino ||
  916. ceph_snap(in) != vino.snap) {
  917. in = ceph_get_inode(sb, vino);
  918. if (IS_ERR(in)) {
  919. err = PTR_ERR(in);
  920. goto done;
  921. }
  922. }
  923. req->r_target_inode = in;
  924. err = fill_inode(in,
  925. &rinfo->targeti, NULL,
  926. session, req->r_request_started,
  927. (le32_to_cpu(rinfo->head->result) == 0) ?
  928. req->r_fmode : -1,
  929. &req->r_caps_reservation);
  930. if (err < 0) {
  931. pr_err("fill_inode badness %p %llx.%llx\n",
  932. in, ceph_vinop(in));
  933. goto done;
  934. }
  935. }
  936. done:
  937. dout("fill_trace done err=%d\n", err);
  938. return err;
  939. }
  940. /*
  941. * Prepopulate our cache with readdir results, leases, etc.
  942. */
  943. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  944. struct ceph_mds_session *session)
  945. {
  946. struct dentry *parent = req->r_dentry;
  947. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  948. struct qstr dname;
  949. struct dentry *dn;
  950. struct inode *in;
  951. int err = 0, i;
  952. struct inode *snapdir = NULL;
  953. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  954. u64 frag = le32_to_cpu(rhead->args.readdir.frag);
  955. struct ceph_dentry_info *di;
  956. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  957. snapdir = ceph_get_snapdir(parent->d_inode);
  958. parent = d_find_alias(snapdir);
  959. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  960. rinfo->dir_nr, parent);
  961. } else {
  962. dout("readdir_prepopulate %d items under dn %p\n",
  963. rinfo->dir_nr, parent);
  964. if (rinfo->dir_dir)
  965. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  966. }
  967. for (i = 0; i < rinfo->dir_nr; i++) {
  968. struct ceph_vino vino;
  969. dname.name = rinfo->dir_dname[i];
  970. dname.len = rinfo->dir_dname_len[i];
  971. dname.hash = full_name_hash(dname.name, dname.len);
  972. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  973. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  974. retry_lookup:
  975. dn = d_lookup(parent, &dname);
  976. dout("d_lookup on parent=%p name=%.*s got %p\n",
  977. parent, dname.len, dname.name, dn);
  978. if (!dn) {
  979. dn = d_alloc(parent, &dname);
  980. dout("d_alloc %p '%.*s' = %p\n", parent,
  981. dname.len, dname.name, dn);
  982. if (dn == NULL) {
  983. dout("d_alloc badness\n");
  984. err = -ENOMEM;
  985. goto out;
  986. }
  987. err = ceph_init_dentry(dn);
  988. if (err < 0)
  989. goto out;
  990. } else if (dn->d_inode &&
  991. (ceph_ino(dn->d_inode) != vino.ino ||
  992. ceph_snap(dn->d_inode) != vino.snap)) {
  993. dout(" dn %p points to wrong inode %p\n",
  994. dn, dn->d_inode);
  995. d_delete(dn);
  996. dput(dn);
  997. goto retry_lookup;
  998. } else {
  999. /* reorder parent's d_subdirs */
  1000. spin_lock(&dcache_lock);
  1001. spin_lock(&dn->d_lock);
  1002. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1003. spin_unlock(&dn->d_lock);
  1004. spin_unlock(&dcache_lock);
  1005. }
  1006. di = dn->d_fsdata;
  1007. di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
  1008. /* inode */
  1009. if (dn->d_inode) {
  1010. in = dn->d_inode;
  1011. } else {
  1012. in = ceph_get_inode(parent->d_sb, vino);
  1013. if (in == NULL) {
  1014. dout("new_inode badness\n");
  1015. d_delete(dn);
  1016. dput(dn);
  1017. err = -ENOMEM;
  1018. goto out;
  1019. }
  1020. dn = splice_dentry(dn, in, NULL);
  1021. }
  1022. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1023. req->r_request_started, -1,
  1024. &req->r_caps_reservation) < 0) {
  1025. pr_err("fill_inode badness on %p\n", in);
  1026. dput(dn);
  1027. continue;
  1028. }
  1029. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1030. req->r_session, req->r_request_started);
  1031. dput(dn);
  1032. }
  1033. req->r_did_prepopulate = true;
  1034. out:
  1035. if (snapdir) {
  1036. iput(snapdir);
  1037. dput(parent);
  1038. }
  1039. dout("readdir_prepopulate done\n");
  1040. return err;
  1041. }
  1042. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1043. {
  1044. struct ceph_inode_info *ci = ceph_inode(inode);
  1045. int ret = 0;
  1046. spin_lock(&inode->i_lock);
  1047. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1048. inode->i_size = size;
  1049. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1050. /* tell the MDS if we are approaching max_size */
  1051. if ((size << 1) >= ci->i_max_size &&
  1052. (ci->i_reported_size << 1) < ci->i_max_size)
  1053. ret = 1;
  1054. spin_unlock(&inode->i_lock);
  1055. return ret;
  1056. }
  1057. /*
  1058. * Write back inode data in a worker thread. (This can't be done
  1059. * in the message handler context.)
  1060. */
  1061. void ceph_inode_writeback(struct work_struct *work)
  1062. {
  1063. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1064. i_wb_work);
  1065. struct inode *inode = &ci->vfs_inode;
  1066. dout("writeback %p\n", inode);
  1067. filemap_fdatawrite(&inode->i_data);
  1068. iput(inode);
  1069. }
  1070. /*
  1071. * Invalidate inode pages in a worker thread. (This can't be done
  1072. * in the message handler context.)
  1073. */
  1074. static void ceph_inode_invalidate_pages(struct work_struct *work)
  1075. {
  1076. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1077. i_pg_inv_work);
  1078. struct inode *inode = &ci->vfs_inode;
  1079. u32 orig_gen;
  1080. int check = 0;
  1081. spin_lock(&inode->i_lock);
  1082. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1083. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1084. if (ci->i_rdcache_gen == 0 ||
  1085. ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1086. BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
  1087. /* nevermind! */
  1088. ci->i_rdcache_revoking = 0;
  1089. spin_unlock(&inode->i_lock);
  1090. goto out;
  1091. }
  1092. orig_gen = ci->i_rdcache_gen;
  1093. spin_unlock(&inode->i_lock);
  1094. truncate_inode_pages(&inode->i_data, 0);
  1095. spin_lock(&inode->i_lock);
  1096. if (orig_gen == ci->i_rdcache_gen) {
  1097. dout("invalidate_pages %p gen %d successful\n", inode,
  1098. ci->i_rdcache_gen);
  1099. ci->i_rdcache_gen = 0;
  1100. ci->i_rdcache_revoking = 0;
  1101. check = 1;
  1102. } else {
  1103. dout("invalidate_pages %p gen %d raced, gen now %d\n",
  1104. inode, orig_gen, ci->i_rdcache_gen);
  1105. }
  1106. spin_unlock(&inode->i_lock);
  1107. if (check)
  1108. ceph_check_caps(ci, 0, NULL);
  1109. out:
  1110. iput(inode);
  1111. }
  1112. /*
  1113. * called by trunc_wq; take i_mutex ourselves
  1114. *
  1115. * We also truncate in a separate thread as well.
  1116. */
  1117. void ceph_vmtruncate_work(struct work_struct *work)
  1118. {
  1119. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1120. i_vmtruncate_work);
  1121. struct inode *inode = &ci->vfs_inode;
  1122. dout("vmtruncate_work %p\n", inode);
  1123. mutex_lock(&inode->i_mutex);
  1124. __ceph_do_pending_vmtruncate(inode);
  1125. mutex_unlock(&inode->i_mutex);
  1126. iput(inode);
  1127. }
  1128. /*
  1129. * called with i_mutex held.
  1130. *
  1131. * Make sure any pending truncation is applied before doing anything
  1132. * that may depend on it.
  1133. */
  1134. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1135. {
  1136. struct ceph_inode_info *ci = ceph_inode(inode);
  1137. u64 to;
  1138. int wrbuffer_refs, wake = 0;
  1139. retry:
  1140. spin_lock(&inode->i_lock);
  1141. if (ci->i_truncate_pending == 0) {
  1142. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1143. spin_unlock(&inode->i_lock);
  1144. return;
  1145. }
  1146. /*
  1147. * make sure any dirty snapped pages are flushed before we
  1148. * possibly truncate them.. so write AND block!
  1149. */
  1150. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1151. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1152. inode);
  1153. spin_unlock(&inode->i_lock);
  1154. filemap_write_and_wait_range(&inode->i_data, 0,
  1155. inode->i_sb->s_maxbytes);
  1156. goto retry;
  1157. }
  1158. to = ci->i_truncate_size;
  1159. wrbuffer_refs = ci->i_wrbuffer_ref;
  1160. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1161. ci->i_truncate_pending, to);
  1162. spin_unlock(&inode->i_lock);
  1163. truncate_inode_pages(inode->i_mapping, to);
  1164. spin_lock(&inode->i_lock);
  1165. ci->i_truncate_pending--;
  1166. if (ci->i_truncate_pending == 0)
  1167. wake = 1;
  1168. spin_unlock(&inode->i_lock);
  1169. if (wrbuffer_refs == 0)
  1170. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1171. if (wake)
  1172. wake_up(&ci->i_cap_wq);
  1173. }
  1174. /*
  1175. * symlinks
  1176. */
  1177. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1178. {
  1179. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1180. nd_set_link(nd, ci->i_symlink);
  1181. return NULL;
  1182. }
  1183. static const struct inode_operations ceph_symlink_iops = {
  1184. .readlink = generic_readlink,
  1185. .follow_link = ceph_sym_follow_link,
  1186. };
  1187. /*
  1188. * setattr
  1189. */
  1190. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1191. {
  1192. struct inode *inode = dentry->d_inode;
  1193. struct ceph_inode_info *ci = ceph_inode(inode);
  1194. struct inode *parent_inode = dentry->d_parent->d_inode;
  1195. const unsigned int ia_valid = attr->ia_valid;
  1196. struct ceph_mds_request *req;
  1197. struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
  1198. int issued;
  1199. int release = 0, dirtied = 0;
  1200. int mask = 0;
  1201. int err = 0;
  1202. int queue_trunc = 0;
  1203. if (ceph_snap(inode) != CEPH_NOSNAP)
  1204. return -EROFS;
  1205. __ceph_do_pending_vmtruncate(inode);
  1206. err = inode_change_ok(inode, attr);
  1207. if (err != 0)
  1208. return err;
  1209. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1210. USE_AUTH_MDS);
  1211. if (IS_ERR(req))
  1212. return PTR_ERR(req);
  1213. spin_lock(&inode->i_lock);
  1214. issued = __ceph_caps_issued(ci, NULL);
  1215. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1216. if (ia_valid & ATTR_UID) {
  1217. dout("setattr %p uid %d -> %d\n", inode,
  1218. inode->i_uid, attr->ia_uid);
  1219. if (issued & CEPH_CAP_AUTH_EXCL) {
  1220. inode->i_uid = attr->ia_uid;
  1221. dirtied |= CEPH_CAP_AUTH_EXCL;
  1222. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1223. attr->ia_uid != inode->i_uid) {
  1224. req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
  1225. mask |= CEPH_SETATTR_UID;
  1226. release |= CEPH_CAP_AUTH_SHARED;
  1227. }
  1228. }
  1229. if (ia_valid & ATTR_GID) {
  1230. dout("setattr %p gid %d -> %d\n", inode,
  1231. inode->i_gid, attr->ia_gid);
  1232. if (issued & CEPH_CAP_AUTH_EXCL) {
  1233. inode->i_gid = attr->ia_gid;
  1234. dirtied |= CEPH_CAP_AUTH_EXCL;
  1235. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1236. attr->ia_gid != inode->i_gid) {
  1237. req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
  1238. mask |= CEPH_SETATTR_GID;
  1239. release |= CEPH_CAP_AUTH_SHARED;
  1240. }
  1241. }
  1242. if (ia_valid & ATTR_MODE) {
  1243. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1244. attr->ia_mode);
  1245. if (issued & CEPH_CAP_AUTH_EXCL) {
  1246. inode->i_mode = attr->ia_mode;
  1247. dirtied |= CEPH_CAP_AUTH_EXCL;
  1248. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1249. attr->ia_mode != inode->i_mode) {
  1250. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1251. mask |= CEPH_SETATTR_MODE;
  1252. release |= CEPH_CAP_AUTH_SHARED;
  1253. }
  1254. }
  1255. if (ia_valid & ATTR_ATIME) {
  1256. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1257. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1258. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1259. if (issued & CEPH_CAP_FILE_EXCL) {
  1260. ci->i_time_warp_seq++;
  1261. inode->i_atime = attr->ia_atime;
  1262. dirtied |= CEPH_CAP_FILE_EXCL;
  1263. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1264. timespec_compare(&inode->i_atime,
  1265. &attr->ia_atime) < 0) {
  1266. inode->i_atime = attr->ia_atime;
  1267. dirtied |= CEPH_CAP_FILE_WR;
  1268. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1269. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1270. ceph_encode_timespec(&req->r_args.setattr.atime,
  1271. &attr->ia_atime);
  1272. mask |= CEPH_SETATTR_ATIME;
  1273. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1274. CEPH_CAP_FILE_WR;
  1275. }
  1276. }
  1277. if (ia_valid & ATTR_MTIME) {
  1278. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1279. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1280. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1281. if (issued & CEPH_CAP_FILE_EXCL) {
  1282. ci->i_time_warp_seq++;
  1283. inode->i_mtime = attr->ia_mtime;
  1284. dirtied |= CEPH_CAP_FILE_EXCL;
  1285. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1286. timespec_compare(&inode->i_mtime,
  1287. &attr->ia_mtime) < 0) {
  1288. inode->i_mtime = attr->ia_mtime;
  1289. dirtied |= CEPH_CAP_FILE_WR;
  1290. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1291. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1292. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1293. &attr->ia_mtime);
  1294. mask |= CEPH_SETATTR_MTIME;
  1295. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1296. CEPH_CAP_FILE_WR;
  1297. }
  1298. }
  1299. if (ia_valid & ATTR_SIZE) {
  1300. dout("setattr %p size %lld -> %lld\n", inode,
  1301. inode->i_size, attr->ia_size);
  1302. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1303. err = -EINVAL;
  1304. goto out;
  1305. }
  1306. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1307. attr->ia_size > inode->i_size) {
  1308. inode->i_size = attr->ia_size;
  1309. if (attr->ia_size < inode->i_size) {
  1310. ci->i_truncate_size = attr->ia_size;
  1311. ci->i_truncate_pending++;
  1312. queue_trunc = 1;
  1313. }
  1314. inode->i_blocks =
  1315. (attr->ia_size + (1 << 9) - 1) >> 9;
  1316. inode->i_ctime = attr->ia_ctime;
  1317. ci->i_reported_size = attr->ia_size;
  1318. dirtied |= CEPH_CAP_FILE_EXCL;
  1319. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1320. attr->ia_size != inode->i_size) {
  1321. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1322. req->r_args.setattr.old_size =
  1323. cpu_to_le64(inode->i_size);
  1324. mask |= CEPH_SETATTR_SIZE;
  1325. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1326. CEPH_CAP_FILE_WR;
  1327. }
  1328. }
  1329. /* these do nothing */
  1330. if (ia_valid & ATTR_CTIME) {
  1331. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1332. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1333. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1334. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1335. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1336. only ? "ctime only" : "ignored");
  1337. inode->i_ctime = attr->ia_ctime;
  1338. if (only) {
  1339. /*
  1340. * if kernel wants to dirty ctime but nothing else,
  1341. * we need to choose a cap to dirty under, or do
  1342. * a almost-no-op setattr
  1343. */
  1344. if (issued & CEPH_CAP_AUTH_EXCL)
  1345. dirtied |= CEPH_CAP_AUTH_EXCL;
  1346. else if (issued & CEPH_CAP_FILE_EXCL)
  1347. dirtied |= CEPH_CAP_FILE_EXCL;
  1348. else if (issued & CEPH_CAP_XATTR_EXCL)
  1349. dirtied |= CEPH_CAP_XATTR_EXCL;
  1350. else
  1351. mask |= CEPH_SETATTR_CTIME;
  1352. }
  1353. }
  1354. if (ia_valid & ATTR_FILE)
  1355. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1356. if (dirtied) {
  1357. __ceph_mark_dirty_caps(ci, dirtied);
  1358. inode->i_ctime = CURRENT_TIME;
  1359. }
  1360. release &= issued;
  1361. spin_unlock(&inode->i_lock);
  1362. if (queue_trunc)
  1363. __ceph_do_pending_vmtruncate(inode);
  1364. if (mask) {
  1365. req->r_inode = igrab(inode);
  1366. req->r_inode_drop = release;
  1367. req->r_args.setattr.mask = cpu_to_le32(mask);
  1368. req->r_num_caps = 1;
  1369. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1370. }
  1371. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1372. ceph_cap_string(dirtied), mask);
  1373. ceph_mdsc_put_request(req);
  1374. __ceph_do_pending_vmtruncate(inode);
  1375. return err;
  1376. out:
  1377. spin_unlock(&inode->i_lock);
  1378. ceph_mdsc_put_request(req);
  1379. return err;
  1380. }
  1381. /*
  1382. * Verify that we have a lease on the given mask. If not,
  1383. * do a getattr against an mds.
  1384. */
  1385. int ceph_do_getattr(struct inode *inode, int mask)
  1386. {
  1387. struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
  1388. struct ceph_mds_client *mdsc = &client->mdsc;
  1389. struct ceph_mds_request *req;
  1390. int err;
  1391. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1392. dout("do_getattr inode %p SNAPDIR\n", inode);
  1393. return 0;
  1394. }
  1395. dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
  1396. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1397. return 0;
  1398. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1399. if (IS_ERR(req))
  1400. return PTR_ERR(req);
  1401. req->r_inode = igrab(inode);
  1402. req->r_num_caps = 1;
  1403. req->r_args.getattr.mask = cpu_to_le32(mask);
  1404. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1405. ceph_mdsc_put_request(req);
  1406. dout("do_getattr result=%d\n", err);
  1407. return err;
  1408. }
  1409. /*
  1410. * Check inode permissions. We verify we have a valid value for
  1411. * the AUTH cap, then call the generic handler.
  1412. */
  1413. int ceph_permission(struct inode *inode, int mask)
  1414. {
  1415. int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1416. if (!err)
  1417. err = generic_permission(inode, mask, NULL);
  1418. return err;
  1419. }
  1420. /*
  1421. * Get all attributes. Hopefully somedata we'll have a statlite()
  1422. * and can limit the fields we require to be accurate.
  1423. */
  1424. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1425. struct kstat *stat)
  1426. {
  1427. struct inode *inode = dentry->d_inode;
  1428. struct ceph_inode_info *ci = ceph_inode(inode);
  1429. int err;
  1430. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1431. if (!err) {
  1432. generic_fillattr(inode, stat);
  1433. stat->ino = inode->i_ino;
  1434. if (ceph_snap(inode) != CEPH_NOSNAP)
  1435. stat->dev = ceph_snap(inode);
  1436. else
  1437. stat->dev = 0;
  1438. if (S_ISDIR(inode->i_mode)) {
  1439. stat->size = ci->i_rbytes;
  1440. stat->blocks = 0;
  1441. stat->blksize = 65536;
  1442. }
  1443. }
  1444. return err;
  1445. }