inode.c 44 KB

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