inode.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/slab.h>
  5. #include <linux/string.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/kernel.h>
  8. #include <linux/writeback.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/posix_acl.h>
  11. #include <linux/random.h>
  12. #include "super.h"
  13. #include "mds_client.h"
  14. #include "cache.h"
  15. #include <linux/ceph/decode.h>
  16. /*
  17. * Ceph inode operations
  18. *
  19. * Implement basic inode helpers (get, alloc) and inode ops (getattr,
  20. * setattr, etc.), xattr helpers, and helpers for assimilating
  21. * metadata returned by the MDS into our cache.
  22. *
  23. * Also define helpers for doing asynchronous writeback, invalidation,
  24. * and truncation for the benefit of those who can't afford to block
  25. * (typically because they are in the message handler path).
  26. */
  27. static const struct inode_operations ceph_symlink_iops;
  28. static void ceph_invalidate_work(struct work_struct *work);
  29. static void ceph_writeback_work(struct work_struct *work);
  30. static void ceph_vmtruncate_work(struct work_struct *work);
  31. /*
  32. * find or create an inode, given the ceph ino number
  33. */
  34. static int ceph_set_ino_cb(struct inode *inode, void *data)
  35. {
  36. ceph_inode(inode)->i_vino = *(struct ceph_vino *)data;
  37. inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data);
  38. return 0;
  39. }
  40. struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino)
  41. {
  42. struct inode *inode;
  43. ino_t t = ceph_vino_to_ino(vino);
  44. inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino);
  45. if (inode == NULL)
  46. return ERR_PTR(-ENOMEM);
  47. if (inode->i_state & I_NEW) {
  48. dout("get_inode created new inode %p %llx.%llx ino %llx\n",
  49. inode, ceph_vinop(inode), (u64)inode->i_ino);
  50. unlock_new_inode(inode);
  51. }
  52. dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino,
  53. vino.snap, inode);
  54. return inode;
  55. }
  56. /*
  57. * get/constuct snapdir inode for a given directory
  58. */
  59. struct inode *ceph_get_snapdir(struct inode *parent)
  60. {
  61. struct ceph_vino vino = {
  62. .ino = ceph_ino(parent),
  63. .snap = CEPH_SNAPDIR,
  64. };
  65. struct inode *inode = ceph_get_inode(parent->i_sb, vino);
  66. struct ceph_inode_info *ci = ceph_inode(inode);
  67. BUG_ON(!S_ISDIR(parent->i_mode));
  68. if (IS_ERR(inode))
  69. return inode;
  70. inode->i_mode = parent->i_mode;
  71. inode->i_uid = parent->i_uid;
  72. inode->i_gid = parent->i_gid;
  73. inode->i_op = &ceph_snapdir_iops;
  74. inode->i_fop = &ceph_snapdir_fops;
  75. ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
  76. ci->i_rbytes = 0;
  77. return inode;
  78. }
  79. const struct inode_operations ceph_file_iops = {
  80. .permission = ceph_permission,
  81. .setattr = ceph_setattr,
  82. .getattr = ceph_getattr,
  83. .setxattr = ceph_setxattr,
  84. .getxattr = ceph_getxattr,
  85. .listxattr = ceph_listxattr,
  86. .removexattr = ceph_removexattr,
  87. .get_acl = ceph_get_acl,
  88. .set_acl = ceph_set_acl,
  89. };
  90. /*
  91. * We use a 'frag tree' to keep track of the MDS's directory fragments
  92. * for a given inode (usually there is just a single fragment). We
  93. * need to know when a child frag is delegated to a new MDS, or when
  94. * it is flagged as replicated, so we can direct our requests
  95. * accordingly.
  96. */
  97. /*
  98. * find/create a frag in the tree
  99. */
  100. static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
  101. u32 f)
  102. {
  103. struct rb_node **p;
  104. struct rb_node *parent = NULL;
  105. struct ceph_inode_frag *frag;
  106. int c;
  107. p = &ci->i_fragtree.rb_node;
  108. while (*p) {
  109. parent = *p;
  110. frag = rb_entry(parent, struct ceph_inode_frag, node);
  111. c = ceph_frag_compare(f, frag->frag);
  112. if (c < 0)
  113. p = &(*p)->rb_left;
  114. else if (c > 0)
  115. p = &(*p)->rb_right;
  116. else
  117. return frag;
  118. }
  119. frag = kmalloc(sizeof(*frag), GFP_NOFS);
  120. if (!frag) {
  121. pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx "
  122. "frag %x\n", &ci->vfs_inode,
  123. ceph_vinop(&ci->vfs_inode), f);
  124. return ERR_PTR(-ENOMEM);
  125. }
  126. frag->frag = f;
  127. frag->split_by = 0;
  128. frag->mds = -1;
  129. frag->ndist = 0;
  130. rb_link_node(&frag->node, parent, p);
  131. rb_insert_color(&frag->node, &ci->i_fragtree);
  132. dout("get_or_create_frag added %llx.%llx frag %x\n",
  133. ceph_vinop(&ci->vfs_inode), f);
  134. return frag;
  135. }
  136. /*
  137. * find a specific frag @f
  138. */
  139. struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
  140. {
  141. struct rb_node *n = ci->i_fragtree.rb_node;
  142. while (n) {
  143. struct ceph_inode_frag *frag =
  144. rb_entry(n, struct ceph_inode_frag, node);
  145. int c = ceph_frag_compare(f, frag->frag);
  146. if (c < 0)
  147. n = n->rb_left;
  148. else if (c > 0)
  149. n = n->rb_right;
  150. else
  151. return frag;
  152. }
  153. return NULL;
  154. }
  155. /*
  156. * Choose frag containing the given value @v. If @pfrag is
  157. * specified, copy the frag delegation info to the caller if
  158. * it is present.
  159. */
  160. static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  161. struct ceph_inode_frag *pfrag, int *found)
  162. {
  163. u32 t = ceph_frag_make(0, 0);
  164. struct ceph_inode_frag *frag;
  165. unsigned nway, i;
  166. u32 n;
  167. if (found)
  168. *found = 0;
  169. while (1) {
  170. WARN_ON(!ceph_frag_contains_value(t, v));
  171. frag = __ceph_find_frag(ci, t);
  172. if (!frag)
  173. break; /* t is a leaf */
  174. if (frag->split_by == 0) {
  175. if (pfrag)
  176. memcpy(pfrag, frag, sizeof(*pfrag));
  177. if (found)
  178. *found = 1;
  179. break;
  180. }
  181. /* choose child */
  182. nway = 1 << frag->split_by;
  183. dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
  184. frag->split_by, nway);
  185. for (i = 0; i < nway; i++) {
  186. n = ceph_frag_make_child(t, frag->split_by, i);
  187. if (ceph_frag_contains_value(n, v)) {
  188. t = n;
  189. break;
  190. }
  191. }
  192. BUG_ON(i == nway);
  193. }
  194. dout("choose_frag(%x) = %x\n", v, t);
  195. return t;
  196. }
  197. u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  198. struct ceph_inode_frag *pfrag, int *found)
  199. {
  200. u32 ret;
  201. mutex_lock(&ci->i_fragtree_mutex);
  202. ret = __ceph_choose_frag(ci, v, pfrag, found);
  203. mutex_unlock(&ci->i_fragtree_mutex);
  204. return ret;
  205. }
  206. /*
  207. * Process dirfrag (delegation) info from the mds. Include leaf
  208. * fragment in tree ONLY if ndist > 0. Otherwise, only
  209. * branches/splits are included in i_fragtree)
  210. */
  211. static int ceph_fill_dirfrag(struct inode *inode,
  212. struct ceph_mds_reply_dirfrag *dirinfo)
  213. {
  214. struct ceph_inode_info *ci = ceph_inode(inode);
  215. struct ceph_inode_frag *frag;
  216. u32 id = le32_to_cpu(dirinfo->frag);
  217. int mds = le32_to_cpu(dirinfo->auth);
  218. int ndist = le32_to_cpu(dirinfo->ndist);
  219. int diri_auth = -1;
  220. int i;
  221. int err = 0;
  222. spin_lock(&ci->i_ceph_lock);
  223. if (ci->i_auth_cap)
  224. diri_auth = ci->i_auth_cap->mds;
  225. spin_unlock(&ci->i_ceph_lock);
  226. mutex_lock(&ci->i_fragtree_mutex);
  227. if (ndist == 0 && mds == diri_auth) {
  228. /* no delegation info needed. */
  229. frag = __ceph_find_frag(ci, id);
  230. if (!frag)
  231. goto out;
  232. if (frag->split_by == 0) {
  233. /* tree leaf, remove */
  234. dout("fill_dirfrag removed %llx.%llx frag %x"
  235. " (no ref)\n", ceph_vinop(inode), id);
  236. rb_erase(&frag->node, &ci->i_fragtree);
  237. kfree(frag);
  238. } else {
  239. /* tree branch, keep and clear */
  240. dout("fill_dirfrag cleared %llx.%llx frag %x"
  241. " referral\n", ceph_vinop(inode), id);
  242. frag->mds = -1;
  243. frag->ndist = 0;
  244. }
  245. goto out;
  246. }
  247. /* find/add this frag to store mds delegation info */
  248. frag = __get_or_create_frag(ci, id);
  249. if (IS_ERR(frag)) {
  250. /* this is not the end of the world; we can continue
  251. with bad/inaccurate delegation info */
  252. pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
  253. ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
  254. err = -ENOMEM;
  255. goto out;
  256. }
  257. frag->mds = mds;
  258. frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
  259. for (i = 0; i < frag->ndist; i++)
  260. frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
  261. dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
  262. ceph_vinop(inode), frag->frag, frag->ndist);
  263. out:
  264. mutex_unlock(&ci->i_fragtree_mutex);
  265. return err;
  266. }
  267. static int ceph_fill_fragtree(struct inode *inode,
  268. struct ceph_frag_tree_head *fragtree,
  269. struct ceph_mds_reply_dirfrag *dirinfo)
  270. {
  271. struct ceph_inode_info *ci = ceph_inode(inode);
  272. struct ceph_inode_frag *frag;
  273. struct rb_node *rb_node;
  274. int i;
  275. u32 id, nsplits;
  276. bool update = false;
  277. mutex_lock(&ci->i_fragtree_mutex);
  278. nsplits = le32_to_cpu(fragtree->nsplits);
  279. if (nsplits) {
  280. i = prandom_u32() % nsplits;
  281. id = le32_to_cpu(fragtree->splits[i].frag);
  282. if (!__ceph_find_frag(ci, id))
  283. update = true;
  284. } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
  285. rb_node = rb_first(&ci->i_fragtree);
  286. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  287. if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
  288. update = true;
  289. }
  290. if (!update && dirinfo) {
  291. id = le32_to_cpu(dirinfo->frag);
  292. if (id != __ceph_choose_frag(ci, id, NULL, NULL))
  293. update = true;
  294. }
  295. if (!update)
  296. goto out_unlock;
  297. dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
  298. rb_node = rb_first(&ci->i_fragtree);
  299. for (i = 0; i < nsplits; i++) {
  300. id = le32_to_cpu(fragtree->splits[i].frag);
  301. frag = NULL;
  302. while (rb_node) {
  303. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  304. if (ceph_frag_compare(frag->frag, id) >= 0) {
  305. if (frag->frag != id)
  306. frag = NULL;
  307. else
  308. rb_node = rb_next(rb_node);
  309. break;
  310. }
  311. rb_node = rb_next(rb_node);
  312. rb_erase(&frag->node, &ci->i_fragtree);
  313. kfree(frag);
  314. frag = NULL;
  315. }
  316. if (!frag) {
  317. frag = __get_or_create_frag(ci, id);
  318. if (IS_ERR(frag))
  319. continue;
  320. }
  321. frag->split_by = le32_to_cpu(fragtree->splits[i].by);
  322. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  323. }
  324. while (rb_node) {
  325. frag = rb_entry(rb_node, struct ceph_inode_frag, node);
  326. rb_node = rb_next(rb_node);
  327. rb_erase(&frag->node, &ci->i_fragtree);
  328. kfree(frag);
  329. }
  330. out_unlock:
  331. mutex_unlock(&ci->i_fragtree_mutex);
  332. return 0;
  333. }
  334. /*
  335. * initialize a newly allocated inode.
  336. */
  337. struct inode *ceph_alloc_inode(struct super_block *sb)
  338. {
  339. struct ceph_inode_info *ci;
  340. int i;
  341. ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS);
  342. if (!ci)
  343. return NULL;
  344. dout("alloc_inode %p\n", &ci->vfs_inode);
  345. spin_lock_init(&ci->i_ceph_lock);
  346. ci->i_version = 0;
  347. ci->i_inline_version = 0;
  348. ci->i_time_warp_seq = 0;
  349. ci->i_ceph_flags = 0;
  350. atomic64_set(&ci->i_ordered_count, 1);
  351. atomic64_set(&ci->i_release_count, 1);
  352. atomic64_set(&ci->i_complete_seq[0], 0);
  353. atomic64_set(&ci->i_complete_seq[1], 0);
  354. ci->i_symlink = NULL;
  355. memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
  356. ci->i_fragtree = RB_ROOT;
  357. mutex_init(&ci->i_fragtree_mutex);
  358. ci->i_xattrs.blob = NULL;
  359. ci->i_xattrs.prealloc_blob = NULL;
  360. ci->i_xattrs.dirty = false;
  361. ci->i_xattrs.index = RB_ROOT;
  362. ci->i_xattrs.count = 0;
  363. ci->i_xattrs.names_size = 0;
  364. ci->i_xattrs.vals_size = 0;
  365. ci->i_xattrs.version = 0;
  366. ci->i_xattrs.index_version = 0;
  367. ci->i_caps = RB_ROOT;
  368. ci->i_auth_cap = NULL;
  369. ci->i_dirty_caps = 0;
  370. ci->i_flushing_caps = 0;
  371. INIT_LIST_HEAD(&ci->i_dirty_item);
  372. INIT_LIST_HEAD(&ci->i_flushing_item);
  373. ci->i_prealloc_cap_flush = NULL;
  374. ci->i_cap_flush_tree = RB_ROOT;
  375. init_waitqueue_head(&ci->i_cap_wq);
  376. ci->i_hold_caps_min = 0;
  377. ci->i_hold_caps_max = 0;
  378. INIT_LIST_HEAD(&ci->i_cap_delay_list);
  379. INIT_LIST_HEAD(&ci->i_cap_snaps);
  380. ci->i_head_snapc = NULL;
  381. ci->i_snap_caps = 0;
  382. for (i = 0; i < CEPH_FILE_MODE_NUM; i++)
  383. ci->i_nr_by_mode[i] = 0;
  384. mutex_init(&ci->i_truncate_mutex);
  385. ci->i_truncate_seq = 0;
  386. ci->i_truncate_size = 0;
  387. ci->i_truncate_pending = 0;
  388. ci->i_max_size = 0;
  389. ci->i_reported_size = 0;
  390. ci->i_wanted_max_size = 0;
  391. ci->i_requested_max_size = 0;
  392. ci->i_pin_ref = 0;
  393. ci->i_rd_ref = 0;
  394. ci->i_rdcache_ref = 0;
  395. ci->i_wr_ref = 0;
  396. ci->i_wb_ref = 0;
  397. ci->i_wrbuffer_ref = 0;
  398. ci->i_wrbuffer_ref_head = 0;
  399. ci->i_shared_gen = 0;
  400. ci->i_rdcache_gen = 0;
  401. ci->i_rdcache_revoking = 0;
  402. INIT_LIST_HEAD(&ci->i_unsafe_writes);
  403. INIT_LIST_HEAD(&ci->i_unsafe_dirops);
  404. INIT_LIST_HEAD(&ci->i_unsafe_iops);
  405. spin_lock_init(&ci->i_unsafe_lock);
  406. ci->i_snap_realm = NULL;
  407. INIT_LIST_HEAD(&ci->i_snap_realm_item);
  408. INIT_LIST_HEAD(&ci->i_snap_flush_item);
  409. INIT_WORK(&ci->i_wb_work, ceph_writeback_work);
  410. INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work);
  411. INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work);
  412. ceph_fscache_inode_init(ci);
  413. return &ci->vfs_inode;
  414. }
  415. static void ceph_i_callback(struct rcu_head *head)
  416. {
  417. struct inode *inode = container_of(head, struct inode, i_rcu);
  418. struct ceph_inode_info *ci = ceph_inode(inode);
  419. kmem_cache_free(ceph_inode_cachep, ci);
  420. }
  421. void ceph_destroy_inode(struct inode *inode)
  422. {
  423. struct ceph_inode_info *ci = ceph_inode(inode);
  424. struct ceph_inode_frag *frag;
  425. struct rb_node *n;
  426. dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
  427. ceph_fscache_unregister_inode_cookie(ci);
  428. ceph_queue_caps_release(inode);
  429. /*
  430. * we may still have a snap_realm reference if there are stray
  431. * caps in i_snap_caps.
  432. */
  433. if (ci->i_snap_realm) {
  434. struct ceph_mds_client *mdsc =
  435. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  436. struct ceph_snap_realm *realm = ci->i_snap_realm;
  437. dout(" dropping residual ref to snap realm %p\n", realm);
  438. spin_lock(&realm->inodes_with_caps_lock);
  439. list_del_init(&ci->i_snap_realm_item);
  440. spin_unlock(&realm->inodes_with_caps_lock);
  441. ceph_put_snap_realm(mdsc, realm);
  442. }
  443. kfree(ci->i_symlink);
  444. while ((n = rb_first(&ci->i_fragtree)) != NULL) {
  445. frag = rb_entry(n, struct ceph_inode_frag, node);
  446. rb_erase(n, &ci->i_fragtree);
  447. kfree(frag);
  448. }
  449. __ceph_destroy_xattrs(ci);
  450. if (ci->i_xattrs.blob)
  451. ceph_buffer_put(ci->i_xattrs.blob);
  452. if (ci->i_xattrs.prealloc_blob)
  453. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  454. call_rcu(&inode->i_rcu, ceph_i_callback);
  455. }
  456. int ceph_drop_inode(struct inode *inode)
  457. {
  458. /*
  459. * Positve dentry and corresponding inode are always accompanied
  460. * in MDS reply. So no need to keep inode in the cache after
  461. * dropping all its aliases.
  462. */
  463. return 1;
  464. }
  465. /*
  466. * Helpers to fill in size, ctime, mtime, and atime. We have to be
  467. * careful because either the client or MDS may have more up to date
  468. * info, depending on which capabilities are held, and whether
  469. * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
  470. * and size are monotonically increasing, except when utimes() or
  471. * truncate() increments the corresponding _seq values.)
  472. */
  473. int ceph_fill_file_size(struct inode *inode, int issued,
  474. u32 truncate_seq, u64 truncate_size, u64 size)
  475. {
  476. struct ceph_inode_info *ci = ceph_inode(inode);
  477. int queue_trunc = 0;
  478. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
  479. (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) {
  480. dout("size %lld -> %llu\n", inode->i_size, size);
  481. inode->i_size = size;
  482. inode->i_blocks = (size + (1<<9) - 1) >> 9;
  483. ci->i_reported_size = size;
  484. if (truncate_seq != ci->i_truncate_seq) {
  485. dout("truncate_seq %u -> %u\n",
  486. ci->i_truncate_seq, truncate_seq);
  487. ci->i_truncate_seq = truncate_seq;
  488. /* the MDS should have revoked these caps */
  489. WARN_ON_ONCE(issued & (CEPH_CAP_FILE_EXCL |
  490. CEPH_CAP_FILE_RD |
  491. CEPH_CAP_FILE_WR |
  492. CEPH_CAP_FILE_LAZYIO));
  493. /*
  494. * If we hold relevant caps, or in the case where we're
  495. * not the only client referencing this file and we
  496. * don't hold those caps, then we need to check whether
  497. * the file is either opened or mmaped
  498. */
  499. if ((issued & (CEPH_CAP_FILE_CACHE|
  500. CEPH_CAP_FILE_BUFFER)) ||
  501. mapping_mapped(inode->i_mapping) ||
  502. __ceph_caps_file_wanted(ci)) {
  503. ci->i_truncate_pending++;
  504. queue_trunc = 1;
  505. }
  506. }
  507. }
  508. if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 &&
  509. ci->i_truncate_size != truncate_size) {
  510. dout("truncate_size %lld -> %llu\n", ci->i_truncate_size,
  511. truncate_size);
  512. ci->i_truncate_size = truncate_size;
  513. }
  514. if (queue_trunc)
  515. ceph_fscache_invalidate(inode);
  516. return queue_trunc;
  517. }
  518. void ceph_fill_file_time(struct inode *inode, int issued,
  519. u64 time_warp_seq, struct timespec *ctime,
  520. struct timespec *mtime, struct timespec *atime)
  521. {
  522. struct ceph_inode_info *ci = ceph_inode(inode);
  523. int warn = 0;
  524. if (issued & (CEPH_CAP_FILE_EXCL|
  525. CEPH_CAP_FILE_WR|
  526. CEPH_CAP_FILE_BUFFER|
  527. CEPH_CAP_AUTH_EXCL|
  528. CEPH_CAP_XATTR_EXCL)) {
  529. if (timespec_compare(ctime, &inode->i_ctime) > 0) {
  530. dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n",
  531. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  532. ctime->tv_sec, ctime->tv_nsec);
  533. inode->i_ctime = *ctime;
  534. }
  535. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
  536. /* the MDS did a utimes() */
  537. dout("mtime %ld.%09ld -> %ld.%09ld "
  538. "tw %d -> %d\n",
  539. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  540. mtime->tv_sec, mtime->tv_nsec,
  541. ci->i_time_warp_seq, (int)time_warp_seq);
  542. inode->i_mtime = *mtime;
  543. inode->i_atime = *atime;
  544. ci->i_time_warp_seq = time_warp_seq;
  545. } else if (time_warp_seq == ci->i_time_warp_seq) {
  546. /* nobody did utimes(); take the max */
  547. if (timespec_compare(mtime, &inode->i_mtime) > 0) {
  548. dout("mtime %ld.%09ld -> %ld.%09ld inc\n",
  549. inode->i_mtime.tv_sec,
  550. inode->i_mtime.tv_nsec,
  551. mtime->tv_sec, mtime->tv_nsec);
  552. inode->i_mtime = *mtime;
  553. }
  554. if (timespec_compare(atime, &inode->i_atime) > 0) {
  555. dout("atime %ld.%09ld -> %ld.%09ld inc\n",
  556. inode->i_atime.tv_sec,
  557. inode->i_atime.tv_nsec,
  558. atime->tv_sec, atime->tv_nsec);
  559. inode->i_atime = *atime;
  560. }
  561. } else if (issued & CEPH_CAP_FILE_EXCL) {
  562. /* we did a utimes(); ignore mds values */
  563. } else {
  564. warn = 1;
  565. }
  566. } else {
  567. /* we have no write|excl caps; whatever the MDS says is true */
  568. if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
  569. inode->i_ctime = *ctime;
  570. inode->i_mtime = *mtime;
  571. inode->i_atime = *atime;
  572. ci->i_time_warp_seq = time_warp_seq;
  573. } else {
  574. warn = 1;
  575. }
  576. }
  577. if (warn) /* time_warp_seq shouldn't go backwards */
  578. dout("%p mds time_warp_seq %llu < %u\n",
  579. inode, time_warp_seq, ci->i_time_warp_seq);
  580. }
  581. /*
  582. * Populate an inode based on info from mds. May be called on new or
  583. * existing inodes.
  584. */
  585. static int fill_inode(struct inode *inode, struct page *locked_page,
  586. struct ceph_mds_reply_info_in *iinfo,
  587. struct ceph_mds_reply_dirfrag *dirinfo,
  588. struct ceph_mds_session *session,
  589. unsigned long ttl_from, int cap_fmode,
  590. struct ceph_cap_reservation *caps_reservation)
  591. {
  592. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  593. struct ceph_mds_reply_inode *info = iinfo->in;
  594. struct ceph_inode_info *ci = ceph_inode(inode);
  595. int issued = 0, implemented, new_issued;
  596. struct timespec mtime, atime, ctime;
  597. struct ceph_buffer *xattr_blob = NULL;
  598. struct ceph_cap *new_cap = NULL;
  599. int err = 0;
  600. bool wake = false;
  601. bool queue_trunc = false;
  602. bool new_version = false;
  603. bool fill_inline = false;
  604. dout("fill_inode %p ino %llx.%llx v %llu had %llu\n",
  605. inode, ceph_vinop(inode), le64_to_cpu(info->version),
  606. ci->i_version);
  607. /* prealloc new cap struct */
  608. if (info->cap.caps && ceph_snap(inode) == CEPH_NOSNAP)
  609. new_cap = ceph_get_cap(mdsc, caps_reservation);
  610. /*
  611. * prealloc xattr data, if it looks like we'll need it. only
  612. * if len > 4 (meaning there are actually xattrs; the first 4
  613. * bytes are the xattr count).
  614. */
  615. if (iinfo->xattr_len > 4) {
  616. xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
  617. if (!xattr_blob)
  618. pr_err("fill_inode ENOMEM xattr blob %d bytes\n",
  619. iinfo->xattr_len);
  620. }
  621. spin_lock(&ci->i_ceph_lock);
  622. /*
  623. * provided version will be odd if inode value is projected,
  624. * even if stable. skip the update if we have newer stable
  625. * info (ours>=theirs, e.g. due to racing mds replies), unless
  626. * we are getting projected (unstable) info (in which case the
  627. * version is odd, and we want ours>theirs).
  628. * us them
  629. * 2 2 skip
  630. * 3 2 skip
  631. * 3 3 update
  632. */
  633. if (ci->i_version == 0 ||
  634. ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  635. le64_to_cpu(info->version) > (ci->i_version & ~1)))
  636. new_version = true;
  637. issued = __ceph_caps_issued(ci, &implemented);
  638. issued |= implemented | __ceph_caps_dirty(ci);
  639. new_issued = ~issued & le32_to_cpu(info->cap.caps);
  640. /* update inode */
  641. ci->i_version = le64_to_cpu(info->version);
  642. inode->i_version++;
  643. inode->i_rdev = le32_to_cpu(info->rdev);
  644. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  645. if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
  646. (issued & CEPH_CAP_AUTH_EXCL) == 0) {
  647. inode->i_mode = le32_to_cpu(info->mode);
  648. inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
  649. inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
  650. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  651. from_kuid(&init_user_ns, inode->i_uid),
  652. from_kgid(&init_user_ns, inode->i_gid));
  653. }
  654. if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
  655. (issued & CEPH_CAP_LINK_EXCL) == 0)
  656. set_nlink(inode, le32_to_cpu(info->nlink));
  657. if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
  658. /* be careful with mtime, atime, size */
  659. ceph_decode_timespec(&atime, &info->atime);
  660. ceph_decode_timespec(&mtime, &info->mtime);
  661. ceph_decode_timespec(&ctime, &info->ctime);
  662. ceph_fill_file_time(inode, issued,
  663. le32_to_cpu(info->time_warp_seq),
  664. &ctime, &mtime, &atime);
  665. }
  666. if (new_version ||
  667. (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  668. if (ci->i_layout.fl_pg_pool != info->layout.fl_pg_pool)
  669. ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
  670. ci->i_layout = info->layout;
  671. queue_trunc = ceph_fill_file_size(inode, issued,
  672. le32_to_cpu(info->truncate_seq),
  673. le64_to_cpu(info->truncate_size),
  674. le64_to_cpu(info->size));
  675. /* only update max_size on auth cap */
  676. if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
  677. ci->i_max_size != le64_to_cpu(info->max_size)) {
  678. dout("max_size %lld -> %llu\n", ci->i_max_size,
  679. le64_to_cpu(info->max_size));
  680. ci->i_max_size = le64_to_cpu(info->max_size);
  681. }
  682. }
  683. /* xattrs */
  684. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  685. if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
  686. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  687. if (ci->i_xattrs.blob)
  688. ceph_buffer_put(ci->i_xattrs.blob);
  689. ci->i_xattrs.blob = xattr_blob;
  690. if (xattr_blob)
  691. memcpy(ci->i_xattrs.blob->vec.iov_base,
  692. iinfo->xattr_data, iinfo->xattr_len);
  693. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  694. ceph_forget_all_cached_acls(inode);
  695. xattr_blob = NULL;
  696. }
  697. inode->i_mapping->a_ops = &ceph_aops;
  698. switch (inode->i_mode & S_IFMT) {
  699. case S_IFIFO:
  700. case S_IFBLK:
  701. case S_IFCHR:
  702. case S_IFSOCK:
  703. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  704. inode->i_op = &ceph_file_iops;
  705. break;
  706. case S_IFREG:
  707. inode->i_op = &ceph_file_iops;
  708. inode->i_fop = &ceph_file_fops;
  709. break;
  710. case S_IFLNK:
  711. inode->i_op = &ceph_symlink_iops;
  712. if (!ci->i_symlink) {
  713. u32 symlen = iinfo->symlink_len;
  714. char *sym;
  715. spin_unlock(&ci->i_ceph_lock);
  716. err = -EINVAL;
  717. if (WARN_ON(symlen != inode->i_size))
  718. goto out;
  719. err = -ENOMEM;
  720. sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
  721. if (!sym)
  722. goto out;
  723. spin_lock(&ci->i_ceph_lock);
  724. if (!ci->i_symlink)
  725. ci->i_symlink = sym;
  726. else
  727. kfree(sym); /* lost a race */
  728. }
  729. inode->i_link = ci->i_symlink;
  730. break;
  731. case S_IFDIR:
  732. inode->i_op = &ceph_dir_iops;
  733. inode->i_fop = &ceph_dir_fops;
  734. ci->i_dir_layout = iinfo->dir_layout;
  735. ci->i_files = le64_to_cpu(info->files);
  736. ci->i_subdirs = le64_to_cpu(info->subdirs);
  737. ci->i_rbytes = le64_to_cpu(info->rbytes);
  738. ci->i_rfiles = le64_to_cpu(info->rfiles);
  739. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  740. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  741. break;
  742. default:
  743. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  744. ceph_vinop(inode), inode->i_mode);
  745. }
  746. /* were we issued a capability? */
  747. if (info->cap.caps) {
  748. if (ceph_snap(inode) == CEPH_NOSNAP) {
  749. unsigned caps = le32_to_cpu(info->cap.caps);
  750. ceph_add_cap(inode, session,
  751. le64_to_cpu(info->cap.cap_id),
  752. cap_fmode, caps,
  753. le32_to_cpu(info->cap.wanted),
  754. le32_to_cpu(info->cap.seq),
  755. le32_to_cpu(info->cap.mseq),
  756. le64_to_cpu(info->cap.realm),
  757. info->cap.flags, &new_cap);
  758. /* set dir completion flag? */
  759. if (S_ISDIR(inode->i_mode) &&
  760. ci->i_files == 0 && ci->i_subdirs == 0 &&
  761. (caps & CEPH_CAP_FILE_SHARED) &&
  762. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  763. !__ceph_dir_is_complete(ci)) {
  764. dout(" marking %p complete (empty)\n", inode);
  765. i_size_write(inode, 0);
  766. __ceph_dir_set_complete(ci,
  767. atomic64_read(&ci->i_release_count),
  768. atomic64_read(&ci->i_ordered_count));
  769. }
  770. wake = true;
  771. } else {
  772. dout(" %p got snap_caps %s\n", inode,
  773. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  774. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  775. if (cap_fmode >= 0)
  776. __ceph_get_fmode(ci, cap_fmode);
  777. }
  778. } else if (cap_fmode >= 0) {
  779. pr_warn("mds issued no caps on %llx.%llx\n",
  780. ceph_vinop(inode));
  781. __ceph_get_fmode(ci, cap_fmode);
  782. }
  783. if (iinfo->inline_version > 0 &&
  784. iinfo->inline_version >= ci->i_inline_version) {
  785. int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  786. ci->i_inline_version = iinfo->inline_version;
  787. if (ci->i_inline_version != CEPH_INLINE_NONE &&
  788. (locked_page ||
  789. (le32_to_cpu(info->cap.caps) & cache_caps)))
  790. fill_inline = true;
  791. }
  792. spin_unlock(&ci->i_ceph_lock);
  793. if (fill_inline)
  794. ceph_fill_inline_data(inode, locked_page,
  795. iinfo->inline_data, iinfo->inline_len);
  796. if (wake)
  797. wake_up_all(&ci->i_cap_wq);
  798. /* queue truncate if we saw i_size decrease */
  799. if (queue_trunc)
  800. ceph_queue_vmtruncate(inode);
  801. /* populate frag tree */
  802. if (S_ISDIR(inode->i_mode))
  803. ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
  804. /* update delegation info? */
  805. if (dirinfo)
  806. ceph_fill_dirfrag(inode, dirinfo);
  807. err = 0;
  808. out:
  809. if (new_cap)
  810. ceph_put_cap(mdsc, new_cap);
  811. if (xattr_blob)
  812. ceph_buffer_put(xattr_blob);
  813. return err;
  814. }
  815. /*
  816. * caller should hold session s_mutex.
  817. */
  818. static void update_dentry_lease(struct dentry *dentry,
  819. struct ceph_mds_reply_lease *lease,
  820. struct ceph_mds_session *session,
  821. unsigned long from_time)
  822. {
  823. struct ceph_dentry_info *di = ceph_dentry(dentry);
  824. long unsigned duration = le32_to_cpu(lease->duration_ms);
  825. long unsigned ttl = from_time + (duration * HZ) / 1000;
  826. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  827. struct inode *dir;
  828. /* only track leases on regular dentries */
  829. if (dentry->d_op != &ceph_dentry_ops)
  830. return;
  831. spin_lock(&dentry->d_lock);
  832. dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
  833. dentry, duration, ttl);
  834. /* make lease_rdcache_gen match directory */
  835. dir = d_inode(dentry->d_parent);
  836. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  837. if (duration == 0)
  838. goto out_unlock;
  839. if (di->lease_gen == session->s_cap_gen &&
  840. time_before(ttl, dentry->d_time))
  841. goto out_unlock; /* we already have a newer lease. */
  842. if (di->lease_session && di->lease_session != session)
  843. goto out_unlock;
  844. ceph_dentry_lru_touch(dentry);
  845. if (!di->lease_session)
  846. di->lease_session = ceph_get_mds_session(session);
  847. di->lease_gen = session->s_cap_gen;
  848. di->lease_seq = le32_to_cpu(lease->seq);
  849. di->lease_renew_after = half_ttl;
  850. di->lease_renew_from = 0;
  851. dentry->d_time = ttl;
  852. out_unlock:
  853. spin_unlock(&dentry->d_lock);
  854. return;
  855. }
  856. /*
  857. * splice a dentry to an inode.
  858. * caller must hold directory i_mutex for this to be safe.
  859. *
  860. * we will only rehash the resulting dentry if @prehash is
  861. * true; @prehash will be set to false (for the benefit of
  862. * the caller) if we fail.
  863. */
  864. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
  865. bool *prehash)
  866. {
  867. struct dentry *realdn;
  868. BUG_ON(d_inode(dn));
  869. /* dn must be unhashed */
  870. if (!d_unhashed(dn))
  871. d_drop(dn);
  872. realdn = d_splice_alias(in, dn);
  873. if (IS_ERR(realdn)) {
  874. pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
  875. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  876. if (prehash)
  877. *prehash = false; /* don't rehash on error */
  878. dn = realdn; /* note realdn contains the error */
  879. goto out;
  880. } else if (realdn) {
  881. dout("dn %p (%d) spliced with %p (%d) "
  882. "inode %p ino %llx.%llx\n",
  883. dn, d_count(dn),
  884. realdn, d_count(realdn),
  885. d_inode(realdn), ceph_vinop(d_inode(realdn)));
  886. dput(dn);
  887. dn = realdn;
  888. } else {
  889. BUG_ON(!ceph_dentry(dn));
  890. dout("dn %p attached to %p ino %llx.%llx\n",
  891. dn, d_inode(dn), ceph_vinop(d_inode(dn)));
  892. }
  893. if ((!prehash || *prehash) && d_unhashed(dn))
  894. d_rehash(dn);
  895. out:
  896. return dn;
  897. }
  898. /*
  899. * Incorporate results into the local cache. This is either just
  900. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  901. * after a lookup).
  902. *
  903. * A reply may contain
  904. * a directory inode along with a dentry.
  905. * and/or a target inode
  906. *
  907. * Called with snap_rwsem (read).
  908. */
  909. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
  910. struct ceph_mds_session *session)
  911. {
  912. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  913. struct inode *in = NULL;
  914. struct ceph_vino vino;
  915. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  916. int err = 0;
  917. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  918. rinfo->head->is_dentry, rinfo->head->is_target);
  919. #if 0
  920. /*
  921. * Debugging hook:
  922. *
  923. * If we resend completed ops to a recovering mds, we get no
  924. * trace. Since that is very rare, pretend this is the case
  925. * to ensure the 'no trace' handlers in the callers behave.
  926. *
  927. * Fill in inodes unconditionally to avoid breaking cap
  928. * invariants.
  929. */
  930. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  931. pr_info("fill_trace faking empty trace on %lld %s\n",
  932. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  933. if (rinfo->head->is_dentry) {
  934. rinfo->head->is_dentry = 0;
  935. err = fill_inode(req->r_locked_dir,
  936. &rinfo->diri, rinfo->dirfrag,
  937. session, req->r_request_started, -1);
  938. }
  939. if (rinfo->head->is_target) {
  940. rinfo->head->is_target = 0;
  941. ininfo = rinfo->targeti.in;
  942. vino.ino = le64_to_cpu(ininfo->ino);
  943. vino.snap = le64_to_cpu(ininfo->snapid);
  944. in = ceph_get_inode(sb, vino);
  945. err = fill_inode(in, &rinfo->targeti, NULL,
  946. session, req->r_request_started,
  947. req->r_fmode);
  948. iput(in);
  949. }
  950. }
  951. #endif
  952. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  953. dout("fill_trace reply is empty!\n");
  954. if (rinfo->head->result == 0 && req->r_locked_dir)
  955. ceph_invalidate_dir_request(req);
  956. return 0;
  957. }
  958. if (rinfo->head->is_dentry) {
  959. struct inode *dir = req->r_locked_dir;
  960. if (dir) {
  961. err = fill_inode(dir, NULL,
  962. &rinfo->diri, rinfo->dirfrag,
  963. session, req->r_request_started, -1,
  964. &req->r_caps_reservation);
  965. if (err < 0)
  966. goto done;
  967. } else {
  968. WARN_ON_ONCE(1);
  969. }
  970. if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME) {
  971. struct qstr dname;
  972. struct dentry *dn, *parent;
  973. BUG_ON(!rinfo->head->is_target);
  974. BUG_ON(req->r_dentry);
  975. parent = d_find_any_alias(dir);
  976. BUG_ON(!parent);
  977. dname.name = rinfo->dname;
  978. dname.len = rinfo->dname_len;
  979. dname.hash = full_name_hash(dname.name, dname.len);
  980. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  981. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  982. retry_lookup:
  983. dn = d_lookup(parent, &dname);
  984. dout("d_lookup on parent=%p name=%.*s got %p\n",
  985. parent, dname.len, dname.name, dn);
  986. if (!dn) {
  987. dn = d_alloc(parent, &dname);
  988. dout("d_alloc %p '%.*s' = %p\n", parent,
  989. dname.len, dname.name, dn);
  990. if (dn == NULL) {
  991. dput(parent);
  992. err = -ENOMEM;
  993. goto done;
  994. }
  995. err = ceph_init_dentry(dn);
  996. if (err < 0) {
  997. dput(dn);
  998. dput(parent);
  999. goto done;
  1000. }
  1001. } else if (d_really_is_positive(dn) &&
  1002. (ceph_ino(d_inode(dn)) != vino.ino ||
  1003. ceph_snap(d_inode(dn)) != vino.snap)) {
  1004. dout(" dn %p points to wrong inode %p\n",
  1005. dn, d_inode(dn));
  1006. d_delete(dn);
  1007. dput(dn);
  1008. goto retry_lookup;
  1009. }
  1010. req->r_dentry = dn;
  1011. dput(parent);
  1012. }
  1013. }
  1014. if (rinfo->head->is_target) {
  1015. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  1016. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  1017. in = ceph_get_inode(sb, vino);
  1018. if (IS_ERR(in)) {
  1019. err = PTR_ERR(in);
  1020. goto done;
  1021. }
  1022. req->r_target_inode = in;
  1023. err = fill_inode(in, req->r_locked_page, &rinfo->targeti, NULL,
  1024. session, req->r_request_started,
  1025. (!req->r_aborted && rinfo->head->result == 0) ?
  1026. req->r_fmode : -1,
  1027. &req->r_caps_reservation);
  1028. if (err < 0) {
  1029. pr_err("fill_inode badness %p %llx.%llx\n",
  1030. in, ceph_vinop(in));
  1031. goto done;
  1032. }
  1033. }
  1034. /*
  1035. * ignore null lease/binding on snapdir ENOENT, or else we
  1036. * will have trouble splicing in the virtual snapdir later
  1037. */
  1038. if (rinfo->head->is_dentry && !req->r_aborted &&
  1039. req->r_locked_dir &&
  1040. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  1041. fsc->mount_options->snapdir_name,
  1042. req->r_dentry->d_name.len))) {
  1043. /*
  1044. * lookup link rename : null -> possibly existing inode
  1045. * mknod symlink mkdir : null -> new inode
  1046. * unlink : linked -> null
  1047. */
  1048. struct inode *dir = req->r_locked_dir;
  1049. struct dentry *dn = req->r_dentry;
  1050. bool have_dir_cap, have_lease;
  1051. BUG_ON(!dn);
  1052. BUG_ON(!dir);
  1053. BUG_ON(d_inode(dn->d_parent) != dir);
  1054. BUG_ON(ceph_ino(dir) !=
  1055. le64_to_cpu(rinfo->diri.in->ino));
  1056. BUG_ON(ceph_snap(dir) !=
  1057. le64_to_cpu(rinfo->diri.in->snapid));
  1058. /* do we have a lease on the whole dir? */
  1059. have_dir_cap =
  1060. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  1061. CEPH_CAP_FILE_SHARED);
  1062. /* do we have a dn lease? */
  1063. have_lease = have_dir_cap ||
  1064. le32_to_cpu(rinfo->dlease->duration_ms);
  1065. if (!have_lease)
  1066. dout("fill_trace no dentry lease or dir cap\n");
  1067. /* rename? */
  1068. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  1069. struct inode *olddir = req->r_old_dentry_dir;
  1070. BUG_ON(!olddir);
  1071. dout(" src %p '%pd' dst %p '%pd'\n",
  1072. req->r_old_dentry,
  1073. req->r_old_dentry,
  1074. dn, dn);
  1075. dout("fill_trace doing d_move %p -> %p\n",
  1076. req->r_old_dentry, dn);
  1077. /* d_move screws up sibling dentries' offsets */
  1078. ceph_dir_clear_ordered(dir);
  1079. ceph_dir_clear_ordered(olddir);
  1080. d_move(req->r_old_dentry, dn);
  1081. dout(" src %p '%pd' dst %p '%pd'\n",
  1082. req->r_old_dentry,
  1083. req->r_old_dentry,
  1084. dn, dn);
  1085. /* ensure target dentry is invalidated, despite
  1086. rehashing bug in vfs_rename_dir */
  1087. ceph_invalidate_dentry_lease(dn);
  1088. dout("dn %p gets new offset %lld\n", req->r_old_dentry,
  1089. ceph_dentry(req->r_old_dentry)->offset);
  1090. dn = req->r_old_dentry; /* use old_dentry */
  1091. }
  1092. /* null dentry? */
  1093. if (!rinfo->head->is_target) {
  1094. dout("fill_trace null dentry\n");
  1095. if (d_really_is_positive(dn)) {
  1096. ceph_dir_clear_ordered(dir);
  1097. dout("d_delete %p\n", dn);
  1098. d_delete(dn);
  1099. } else {
  1100. dout("d_instantiate %p NULL\n", dn);
  1101. d_instantiate(dn, NULL);
  1102. if (have_lease && d_unhashed(dn))
  1103. d_rehash(dn);
  1104. update_dentry_lease(dn, rinfo->dlease,
  1105. session,
  1106. req->r_request_started);
  1107. }
  1108. goto done;
  1109. }
  1110. /* attach proper inode */
  1111. if (d_really_is_negative(dn)) {
  1112. ceph_dir_clear_ordered(dir);
  1113. ihold(in);
  1114. dn = splice_dentry(dn, in, &have_lease);
  1115. if (IS_ERR(dn)) {
  1116. err = PTR_ERR(dn);
  1117. goto done;
  1118. }
  1119. req->r_dentry = dn; /* may have spliced */
  1120. } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
  1121. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  1122. dn, d_inode(dn), ceph_vinop(d_inode(dn)),
  1123. ceph_vinop(in));
  1124. have_lease = false;
  1125. }
  1126. if (have_lease)
  1127. update_dentry_lease(dn, rinfo->dlease, session,
  1128. req->r_request_started);
  1129. dout(" final dn %p\n", dn);
  1130. } else if (!req->r_aborted &&
  1131. (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  1132. req->r_op == CEPH_MDS_OP_MKSNAP)) {
  1133. struct dentry *dn = req->r_dentry;
  1134. struct inode *dir = req->r_locked_dir;
  1135. /* fill out a snapdir LOOKUPSNAP dentry */
  1136. BUG_ON(!dn);
  1137. BUG_ON(!dir);
  1138. BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
  1139. dout(" linking snapped dir %p to dn %p\n", in, dn);
  1140. ceph_dir_clear_ordered(dir);
  1141. ihold(in);
  1142. dn = splice_dentry(dn, in, NULL);
  1143. if (IS_ERR(dn)) {
  1144. err = PTR_ERR(dn);
  1145. goto done;
  1146. }
  1147. req->r_dentry = dn; /* may have spliced */
  1148. }
  1149. done:
  1150. dout("fill_trace done err=%d\n", err);
  1151. return err;
  1152. }
  1153. /*
  1154. * Prepopulate our cache with readdir results, leases, etc.
  1155. */
  1156. static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
  1157. struct ceph_mds_session *session)
  1158. {
  1159. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1160. int i, err = 0;
  1161. for (i = 0; i < rinfo->dir_nr; i++) {
  1162. struct ceph_vino vino;
  1163. struct inode *in;
  1164. int rc;
  1165. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1166. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1167. in = ceph_get_inode(req->r_dentry->d_sb, vino);
  1168. if (IS_ERR(in)) {
  1169. err = PTR_ERR(in);
  1170. dout("new_inode badness got %d\n", err);
  1171. continue;
  1172. }
  1173. rc = fill_inode(in, NULL, &rinfo->dir_in[i], NULL, session,
  1174. req->r_request_started, -1,
  1175. &req->r_caps_reservation);
  1176. if (rc < 0) {
  1177. pr_err("fill_inode badness on %p got %d\n", in, rc);
  1178. err = rc;
  1179. continue;
  1180. }
  1181. }
  1182. return err;
  1183. }
  1184. void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
  1185. {
  1186. if (ctl->page) {
  1187. kunmap(ctl->page);
  1188. page_cache_release(ctl->page);
  1189. ctl->page = NULL;
  1190. }
  1191. }
  1192. static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
  1193. struct ceph_readdir_cache_control *ctl,
  1194. struct ceph_mds_request *req)
  1195. {
  1196. struct ceph_inode_info *ci = ceph_inode(dir);
  1197. unsigned nsize = PAGE_CACHE_SIZE / sizeof(struct dentry*);
  1198. unsigned idx = ctl->index % nsize;
  1199. pgoff_t pgoff = ctl->index / nsize;
  1200. if (!ctl->page || pgoff != page_index(ctl->page)) {
  1201. ceph_readdir_cache_release(ctl);
  1202. if (idx == 0)
  1203. ctl->page = grab_cache_page(&dir->i_data, pgoff);
  1204. else
  1205. ctl->page = find_lock_page(&dir->i_data, pgoff);
  1206. if (!ctl->page) {
  1207. ctl->index = -1;
  1208. return idx == 0 ? -ENOMEM : 0;
  1209. }
  1210. /* reading/filling the cache are serialized by
  1211. * i_mutex, no need to use page lock */
  1212. unlock_page(ctl->page);
  1213. ctl->dentries = kmap(ctl->page);
  1214. if (idx == 0)
  1215. memset(ctl->dentries, 0, PAGE_CACHE_SIZE);
  1216. }
  1217. if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
  1218. req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
  1219. dout("readdir cache dn %p idx %d\n", dn, ctl->index);
  1220. ctl->dentries[idx] = dn;
  1221. ctl->index++;
  1222. } else {
  1223. dout("disable readdir cache\n");
  1224. ctl->index = -1;
  1225. }
  1226. return 0;
  1227. }
  1228. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1229. struct ceph_mds_session *session)
  1230. {
  1231. struct dentry *parent = req->r_dentry;
  1232. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1233. struct qstr dname;
  1234. struct dentry *dn;
  1235. struct inode *in;
  1236. int err = 0, ret, i;
  1237. struct inode *snapdir = NULL;
  1238. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1239. struct ceph_dentry_info *di;
  1240. u32 frag = le32_to_cpu(rhead->args.readdir.frag);
  1241. struct ceph_readdir_cache_control cache_ctl = {};
  1242. if (req->r_aborted)
  1243. return readdir_prepopulate_inodes_only(req, session);
  1244. if (rinfo->dir_dir &&
  1245. le32_to_cpu(rinfo->dir_dir->frag) != frag) {
  1246. dout("readdir_prepopulate got new frag %x -> %x\n",
  1247. frag, le32_to_cpu(rinfo->dir_dir->frag));
  1248. frag = le32_to_cpu(rinfo->dir_dir->frag);
  1249. if (ceph_frag_is_leftmost(frag))
  1250. req->r_readdir_offset = 2;
  1251. else
  1252. req->r_readdir_offset = 0;
  1253. }
  1254. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1255. snapdir = ceph_get_snapdir(d_inode(parent));
  1256. parent = d_find_alias(snapdir);
  1257. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1258. rinfo->dir_nr, parent);
  1259. } else {
  1260. dout("readdir_prepopulate %d items under dn %p\n",
  1261. rinfo->dir_nr, parent);
  1262. if (rinfo->dir_dir)
  1263. ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
  1264. }
  1265. if (ceph_frag_is_leftmost(frag) && req->r_readdir_offset == 2) {
  1266. /* note dir version at start of readdir so we can tell
  1267. * if any dentries get dropped */
  1268. struct ceph_inode_info *ci = ceph_inode(d_inode(parent));
  1269. req->r_dir_release_cnt = atomic64_read(&ci->i_release_count);
  1270. req->r_dir_ordered_cnt = atomic64_read(&ci->i_ordered_count);
  1271. req->r_readdir_cache_idx = 0;
  1272. }
  1273. cache_ctl.index = req->r_readdir_cache_idx;
  1274. /* FIXME: release caps/leases if error occurs */
  1275. for (i = 0; i < rinfo->dir_nr; i++) {
  1276. struct ceph_vino vino;
  1277. dname.name = rinfo->dir_dname[i];
  1278. dname.len = rinfo->dir_dname_len[i];
  1279. dname.hash = full_name_hash(dname.name, dname.len);
  1280. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1281. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1282. retry_lookup:
  1283. dn = d_lookup(parent, &dname);
  1284. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1285. parent, dname.len, dname.name, dn);
  1286. if (!dn) {
  1287. dn = d_alloc(parent, &dname);
  1288. dout("d_alloc %p '%.*s' = %p\n", parent,
  1289. dname.len, dname.name, dn);
  1290. if (dn == NULL) {
  1291. dout("d_alloc badness\n");
  1292. err = -ENOMEM;
  1293. goto out;
  1294. }
  1295. ret = ceph_init_dentry(dn);
  1296. if (ret < 0) {
  1297. dput(dn);
  1298. err = ret;
  1299. goto out;
  1300. }
  1301. } else if (d_really_is_positive(dn) &&
  1302. (ceph_ino(d_inode(dn)) != vino.ino ||
  1303. ceph_snap(d_inode(dn)) != vino.snap)) {
  1304. dout(" dn %p points to wrong inode %p\n",
  1305. dn, d_inode(dn));
  1306. d_delete(dn);
  1307. dput(dn);
  1308. goto retry_lookup;
  1309. }
  1310. /* inode */
  1311. if (d_really_is_positive(dn)) {
  1312. in = d_inode(dn);
  1313. } else {
  1314. in = ceph_get_inode(parent->d_sb, vino);
  1315. if (IS_ERR(in)) {
  1316. dout("new_inode badness\n");
  1317. d_drop(dn);
  1318. dput(dn);
  1319. err = PTR_ERR(in);
  1320. goto out;
  1321. }
  1322. }
  1323. ret = fill_inode(in, NULL, &rinfo->dir_in[i], NULL, session,
  1324. req->r_request_started, -1,
  1325. &req->r_caps_reservation);
  1326. if (ret < 0) {
  1327. pr_err("fill_inode badness on %p\n", in);
  1328. if (d_really_is_negative(dn))
  1329. iput(in);
  1330. d_drop(dn);
  1331. err = ret;
  1332. goto next_item;
  1333. }
  1334. if (d_really_is_negative(dn)) {
  1335. struct dentry *realdn = splice_dentry(dn, in, NULL);
  1336. if (IS_ERR(realdn)) {
  1337. err = PTR_ERR(realdn);
  1338. d_drop(dn);
  1339. dn = NULL;
  1340. goto next_item;
  1341. }
  1342. dn = realdn;
  1343. }
  1344. di = dn->d_fsdata;
  1345. di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
  1346. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1347. req->r_session,
  1348. req->r_request_started);
  1349. if (err == 0 && cache_ctl.index >= 0) {
  1350. ret = fill_readdir_cache(d_inode(parent), dn,
  1351. &cache_ctl, req);
  1352. if (ret < 0)
  1353. err = ret;
  1354. }
  1355. next_item:
  1356. if (dn)
  1357. dput(dn);
  1358. }
  1359. out:
  1360. if (err == 0) {
  1361. req->r_did_prepopulate = true;
  1362. req->r_readdir_cache_idx = cache_ctl.index;
  1363. }
  1364. ceph_readdir_cache_release(&cache_ctl);
  1365. if (snapdir) {
  1366. iput(snapdir);
  1367. dput(parent);
  1368. }
  1369. dout("readdir_prepopulate done\n");
  1370. return err;
  1371. }
  1372. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1373. {
  1374. struct ceph_inode_info *ci = ceph_inode(inode);
  1375. int ret = 0;
  1376. spin_lock(&ci->i_ceph_lock);
  1377. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1378. inode->i_size = size;
  1379. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1380. /* tell the MDS if we are approaching max_size */
  1381. if ((size << 1) >= ci->i_max_size &&
  1382. (ci->i_reported_size << 1) < ci->i_max_size)
  1383. ret = 1;
  1384. spin_unlock(&ci->i_ceph_lock);
  1385. return ret;
  1386. }
  1387. /*
  1388. * Write back inode data in a worker thread. (This can't be done
  1389. * in the message handler context.)
  1390. */
  1391. void ceph_queue_writeback(struct inode *inode)
  1392. {
  1393. ihold(inode);
  1394. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1395. &ceph_inode(inode)->i_wb_work)) {
  1396. dout("ceph_queue_writeback %p\n", inode);
  1397. } else {
  1398. dout("ceph_queue_writeback %p failed\n", inode);
  1399. iput(inode);
  1400. }
  1401. }
  1402. static void ceph_writeback_work(struct work_struct *work)
  1403. {
  1404. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1405. i_wb_work);
  1406. struct inode *inode = &ci->vfs_inode;
  1407. dout("writeback %p\n", inode);
  1408. filemap_fdatawrite(&inode->i_data);
  1409. iput(inode);
  1410. }
  1411. /*
  1412. * queue an async invalidation
  1413. */
  1414. void ceph_queue_invalidate(struct inode *inode)
  1415. {
  1416. ihold(inode);
  1417. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1418. &ceph_inode(inode)->i_pg_inv_work)) {
  1419. dout("ceph_queue_invalidate %p\n", inode);
  1420. } else {
  1421. dout("ceph_queue_invalidate %p failed\n", inode);
  1422. iput(inode);
  1423. }
  1424. }
  1425. /*
  1426. * Invalidate inode pages in a worker thread. (This can't be done
  1427. * in the message handler context.)
  1428. */
  1429. static void ceph_invalidate_work(struct work_struct *work)
  1430. {
  1431. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1432. i_pg_inv_work);
  1433. struct inode *inode = &ci->vfs_inode;
  1434. u32 orig_gen;
  1435. int check = 0;
  1436. mutex_lock(&ci->i_truncate_mutex);
  1437. spin_lock(&ci->i_ceph_lock);
  1438. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1439. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1440. if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1441. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1442. check = 1;
  1443. spin_unlock(&ci->i_ceph_lock);
  1444. mutex_unlock(&ci->i_truncate_mutex);
  1445. goto out;
  1446. }
  1447. orig_gen = ci->i_rdcache_gen;
  1448. spin_unlock(&ci->i_ceph_lock);
  1449. truncate_pagecache(inode, 0);
  1450. spin_lock(&ci->i_ceph_lock);
  1451. if (orig_gen == ci->i_rdcache_gen &&
  1452. orig_gen == ci->i_rdcache_revoking) {
  1453. dout("invalidate_pages %p gen %d successful\n", inode,
  1454. ci->i_rdcache_gen);
  1455. ci->i_rdcache_revoking--;
  1456. check = 1;
  1457. } else {
  1458. dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
  1459. inode, orig_gen, ci->i_rdcache_gen,
  1460. ci->i_rdcache_revoking);
  1461. if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
  1462. check = 1;
  1463. }
  1464. spin_unlock(&ci->i_ceph_lock);
  1465. mutex_unlock(&ci->i_truncate_mutex);
  1466. out:
  1467. if (check)
  1468. ceph_check_caps(ci, 0, NULL);
  1469. iput(inode);
  1470. }
  1471. /*
  1472. * called by trunc_wq;
  1473. *
  1474. * We also truncate in a separate thread as well.
  1475. */
  1476. static void ceph_vmtruncate_work(struct work_struct *work)
  1477. {
  1478. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1479. i_vmtruncate_work);
  1480. struct inode *inode = &ci->vfs_inode;
  1481. dout("vmtruncate_work %p\n", inode);
  1482. __ceph_do_pending_vmtruncate(inode);
  1483. iput(inode);
  1484. }
  1485. /*
  1486. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1487. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1488. */
  1489. void ceph_queue_vmtruncate(struct inode *inode)
  1490. {
  1491. struct ceph_inode_info *ci = ceph_inode(inode);
  1492. ihold(inode);
  1493. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1494. &ci->i_vmtruncate_work)) {
  1495. dout("ceph_queue_vmtruncate %p\n", inode);
  1496. } else {
  1497. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1498. inode, ci->i_truncate_pending);
  1499. iput(inode);
  1500. }
  1501. }
  1502. /*
  1503. * Make sure any pending truncation is applied before doing anything
  1504. * that may depend on it.
  1505. */
  1506. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1507. {
  1508. struct ceph_inode_info *ci = ceph_inode(inode);
  1509. u64 to;
  1510. int wrbuffer_refs, finish = 0;
  1511. mutex_lock(&ci->i_truncate_mutex);
  1512. retry:
  1513. spin_lock(&ci->i_ceph_lock);
  1514. if (ci->i_truncate_pending == 0) {
  1515. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1516. spin_unlock(&ci->i_ceph_lock);
  1517. mutex_unlock(&ci->i_truncate_mutex);
  1518. return;
  1519. }
  1520. /*
  1521. * make sure any dirty snapped pages are flushed before we
  1522. * possibly truncate them.. so write AND block!
  1523. */
  1524. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1525. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1526. inode);
  1527. spin_unlock(&ci->i_ceph_lock);
  1528. filemap_write_and_wait_range(&inode->i_data, 0,
  1529. inode->i_sb->s_maxbytes);
  1530. goto retry;
  1531. }
  1532. /* there should be no reader or writer */
  1533. WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
  1534. to = ci->i_truncate_size;
  1535. wrbuffer_refs = ci->i_wrbuffer_ref;
  1536. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1537. ci->i_truncate_pending, to);
  1538. spin_unlock(&ci->i_ceph_lock);
  1539. truncate_pagecache(inode, to);
  1540. spin_lock(&ci->i_ceph_lock);
  1541. if (to == ci->i_truncate_size) {
  1542. ci->i_truncate_pending = 0;
  1543. finish = 1;
  1544. }
  1545. spin_unlock(&ci->i_ceph_lock);
  1546. if (!finish)
  1547. goto retry;
  1548. mutex_unlock(&ci->i_truncate_mutex);
  1549. if (wrbuffer_refs == 0)
  1550. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1551. wake_up_all(&ci->i_cap_wq);
  1552. }
  1553. /*
  1554. * symlinks
  1555. */
  1556. static const struct inode_operations ceph_symlink_iops = {
  1557. .readlink = generic_readlink,
  1558. .follow_link = simple_follow_link,
  1559. .setattr = ceph_setattr,
  1560. .getattr = ceph_getattr,
  1561. .setxattr = ceph_setxattr,
  1562. .getxattr = ceph_getxattr,
  1563. .listxattr = ceph_listxattr,
  1564. .removexattr = ceph_removexattr,
  1565. };
  1566. /*
  1567. * setattr
  1568. */
  1569. int __ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1570. {
  1571. struct inode *inode = d_inode(dentry);
  1572. struct ceph_inode_info *ci = ceph_inode(inode);
  1573. const unsigned int ia_valid = attr->ia_valid;
  1574. struct ceph_mds_request *req;
  1575. struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
  1576. struct ceph_cap_flush *prealloc_cf;
  1577. int issued;
  1578. int release = 0, dirtied = 0;
  1579. int mask = 0;
  1580. int err = 0;
  1581. int inode_dirty_flags = 0;
  1582. bool lock_snap_rwsem = false;
  1583. if (ceph_snap(inode) != CEPH_NOSNAP)
  1584. return -EROFS;
  1585. err = inode_change_ok(inode, attr);
  1586. if (err != 0)
  1587. return err;
  1588. prealloc_cf = ceph_alloc_cap_flush();
  1589. if (!prealloc_cf)
  1590. return -ENOMEM;
  1591. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1592. USE_AUTH_MDS);
  1593. if (IS_ERR(req)) {
  1594. ceph_free_cap_flush(prealloc_cf);
  1595. return PTR_ERR(req);
  1596. }
  1597. spin_lock(&ci->i_ceph_lock);
  1598. issued = __ceph_caps_issued(ci, NULL);
  1599. if (!ci->i_head_snapc &&
  1600. (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
  1601. lock_snap_rwsem = true;
  1602. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  1603. spin_unlock(&ci->i_ceph_lock);
  1604. down_read(&mdsc->snap_rwsem);
  1605. spin_lock(&ci->i_ceph_lock);
  1606. issued = __ceph_caps_issued(ci, NULL);
  1607. }
  1608. }
  1609. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1610. if (ia_valid & ATTR_UID) {
  1611. dout("setattr %p uid %d -> %d\n", inode,
  1612. from_kuid(&init_user_ns, inode->i_uid),
  1613. from_kuid(&init_user_ns, attr->ia_uid));
  1614. if (issued & CEPH_CAP_AUTH_EXCL) {
  1615. inode->i_uid = attr->ia_uid;
  1616. dirtied |= CEPH_CAP_AUTH_EXCL;
  1617. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1618. !uid_eq(attr->ia_uid, inode->i_uid)) {
  1619. req->r_args.setattr.uid = cpu_to_le32(
  1620. from_kuid(&init_user_ns, attr->ia_uid));
  1621. mask |= CEPH_SETATTR_UID;
  1622. release |= CEPH_CAP_AUTH_SHARED;
  1623. }
  1624. }
  1625. if (ia_valid & ATTR_GID) {
  1626. dout("setattr %p gid %d -> %d\n", inode,
  1627. from_kgid(&init_user_ns, inode->i_gid),
  1628. from_kgid(&init_user_ns, attr->ia_gid));
  1629. if (issued & CEPH_CAP_AUTH_EXCL) {
  1630. inode->i_gid = attr->ia_gid;
  1631. dirtied |= CEPH_CAP_AUTH_EXCL;
  1632. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1633. !gid_eq(attr->ia_gid, inode->i_gid)) {
  1634. req->r_args.setattr.gid = cpu_to_le32(
  1635. from_kgid(&init_user_ns, attr->ia_gid));
  1636. mask |= CEPH_SETATTR_GID;
  1637. release |= CEPH_CAP_AUTH_SHARED;
  1638. }
  1639. }
  1640. if (ia_valid & ATTR_MODE) {
  1641. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1642. attr->ia_mode);
  1643. if (issued & CEPH_CAP_AUTH_EXCL) {
  1644. inode->i_mode = attr->ia_mode;
  1645. dirtied |= CEPH_CAP_AUTH_EXCL;
  1646. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1647. attr->ia_mode != inode->i_mode) {
  1648. inode->i_mode = attr->ia_mode;
  1649. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1650. mask |= CEPH_SETATTR_MODE;
  1651. release |= CEPH_CAP_AUTH_SHARED;
  1652. }
  1653. }
  1654. if (ia_valid & ATTR_ATIME) {
  1655. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1656. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1657. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1658. if (issued & CEPH_CAP_FILE_EXCL) {
  1659. ci->i_time_warp_seq++;
  1660. inode->i_atime = attr->ia_atime;
  1661. dirtied |= CEPH_CAP_FILE_EXCL;
  1662. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1663. timespec_compare(&inode->i_atime,
  1664. &attr->ia_atime) < 0) {
  1665. inode->i_atime = attr->ia_atime;
  1666. dirtied |= CEPH_CAP_FILE_WR;
  1667. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1668. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1669. ceph_encode_timespec(&req->r_args.setattr.atime,
  1670. &attr->ia_atime);
  1671. mask |= CEPH_SETATTR_ATIME;
  1672. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1673. CEPH_CAP_FILE_WR;
  1674. }
  1675. }
  1676. if (ia_valid & ATTR_MTIME) {
  1677. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1678. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1679. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1680. if (issued & CEPH_CAP_FILE_EXCL) {
  1681. ci->i_time_warp_seq++;
  1682. inode->i_mtime = attr->ia_mtime;
  1683. dirtied |= CEPH_CAP_FILE_EXCL;
  1684. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1685. timespec_compare(&inode->i_mtime,
  1686. &attr->ia_mtime) < 0) {
  1687. inode->i_mtime = attr->ia_mtime;
  1688. dirtied |= CEPH_CAP_FILE_WR;
  1689. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1690. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1691. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1692. &attr->ia_mtime);
  1693. mask |= CEPH_SETATTR_MTIME;
  1694. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1695. CEPH_CAP_FILE_WR;
  1696. }
  1697. }
  1698. if (ia_valid & ATTR_SIZE) {
  1699. dout("setattr %p size %lld -> %lld\n", inode,
  1700. inode->i_size, attr->ia_size);
  1701. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1702. attr->ia_size > inode->i_size) {
  1703. inode->i_size = attr->ia_size;
  1704. inode->i_blocks =
  1705. (attr->ia_size + (1 << 9) - 1) >> 9;
  1706. inode->i_ctime = attr->ia_ctime;
  1707. ci->i_reported_size = attr->ia_size;
  1708. dirtied |= CEPH_CAP_FILE_EXCL;
  1709. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1710. attr->ia_size != inode->i_size) {
  1711. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1712. req->r_args.setattr.old_size =
  1713. cpu_to_le64(inode->i_size);
  1714. mask |= CEPH_SETATTR_SIZE;
  1715. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1716. CEPH_CAP_FILE_WR;
  1717. }
  1718. }
  1719. /* these do nothing */
  1720. if (ia_valid & ATTR_CTIME) {
  1721. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1722. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1723. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1724. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1725. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1726. only ? "ctime only" : "ignored");
  1727. inode->i_ctime = attr->ia_ctime;
  1728. if (only) {
  1729. /*
  1730. * if kernel wants to dirty ctime but nothing else,
  1731. * we need to choose a cap to dirty under, or do
  1732. * a almost-no-op setattr
  1733. */
  1734. if (issued & CEPH_CAP_AUTH_EXCL)
  1735. dirtied |= CEPH_CAP_AUTH_EXCL;
  1736. else if (issued & CEPH_CAP_FILE_EXCL)
  1737. dirtied |= CEPH_CAP_FILE_EXCL;
  1738. else if (issued & CEPH_CAP_XATTR_EXCL)
  1739. dirtied |= CEPH_CAP_XATTR_EXCL;
  1740. else
  1741. mask |= CEPH_SETATTR_CTIME;
  1742. }
  1743. }
  1744. if (ia_valid & ATTR_FILE)
  1745. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1746. if (dirtied) {
  1747. inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
  1748. &prealloc_cf);
  1749. inode->i_ctime = CURRENT_TIME;
  1750. }
  1751. release &= issued;
  1752. spin_unlock(&ci->i_ceph_lock);
  1753. if (lock_snap_rwsem)
  1754. up_read(&mdsc->snap_rwsem);
  1755. if (inode_dirty_flags)
  1756. __mark_inode_dirty(inode, inode_dirty_flags);
  1757. if (mask) {
  1758. req->r_inode = inode;
  1759. ihold(inode);
  1760. req->r_inode_drop = release;
  1761. req->r_args.setattr.mask = cpu_to_le32(mask);
  1762. req->r_num_caps = 1;
  1763. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1764. }
  1765. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1766. ceph_cap_string(dirtied), mask);
  1767. ceph_mdsc_put_request(req);
  1768. ceph_free_cap_flush(prealloc_cf);
  1769. if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
  1770. __ceph_do_pending_vmtruncate(inode);
  1771. return err;
  1772. }
  1773. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1774. {
  1775. int err;
  1776. err = __ceph_setattr(dentry, attr);
  1777. if (err >= 0 && (attr->ia_valid & ATTR_MODE))
  1778. err = posix_acl_chmod(d_inode(dentry), attr->ia_mode);
  1779. return err;
  1780. }
  1781. /*
  1782. * Verify that we have a lease on the given mask. If not,
  1783. * do a getattr against an mds.
  1784. */
  1785. int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
  1786. int mask, bool force)
  1787. {
  1788. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1789. struct ceph_mds_client *mdsc = fsc->mdsc;
  1790. struct ceph_mds_request *req;
  1791. int err;
  1792. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1793. dout("do_getattr inode %p SNAPDIR\n", inode);
  1794. return 0;
  1795. }
  1796. dout("do_getattr inode %p mask %s mode 0%o\n",
  1797. inode, ceph_cap_string(mask), inode->i_mode);
  1798. if (!force && ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1799. return 0;
  1800. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1801. if (IS_ERR(req))
  1802. return PTR_ERR(req);
  1803. req->r_inode = inode;
  1804. ihold(inode);
  1805. req->r_num_caps = 1;
  1806. req->r_args.getattr.mask = cpu_to_le32(mask);
  1807. req->r_locked_page = locked_page;
  1808. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1809. if (locked_page && err == 0) {
  1810. u64 inline_version = req->r_reply_info.targeti.inline_version;
  1811. if (inline_version == 0) {
  1812. /* the reply is supposed to contain inline data */
  1813. err = -EINVAL;
  1814. } else if (inline_version == CEPH_INLINE_NONE) {
  1815. err = -ENODATA;
  1816. } else {
  1817. err = req->r_reply_info.targeti.inline_len;
  1818. }
  1819. }
  1820. ceph_mdsc_put_request(req);
  1821. dout("do_getattr result=%d\n", err);
  1822. return err;
  1823. }
  1824. /*
  1825. * Check inode permissions. We verify we have a valid value for
  1826. * the AUTH cap, then call the generic handler.
  1827. */
  1828. int ceph_permission(struct inode *inode, int mask)
  1829. {
  1830. int err;
  1831. if (mask & MAY_NOT_BLOCK)
  1832. return -ECHILD;
  1833. err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
  1834. if (!err)
  1835. err = generic_permission(inode, mask);
  1836. return err;
  1837. }
  1838. /*
  1839. * Get all attributes. Hopefully somedata we'll have a statlite()
  1840. * and can limit the fields we require to be accurate.
  1841. */
  1842. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1843. struct kstat *stat)
  1844. {
  1845. struct inode *inode = d_inode(dentry);
  1846. struct ceph_inode_info *ci = ceph_inode(inode);
  1847. int err;
  1848. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL, false);
  1849. if (!err) {
  1850. generic_fillattr(inode, stat);
  1851. stat->ino = ceph_translate_ino(inode->i_sb, inode->i_ino);
  1852. if (ceph_snap(inode) != CEPH_NOSNAP)
  1853. stat->dev = ceph_snap(inode);
  1854. else
  1855. stat->dev = 0;
  1856. if (S_ISDIR(inode->i_mode)) {
  1857. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb),
  1858. RBYTES))
  1859. stat->size = ci->i_rbytes;
  1860. else
  1861. stat->size = ci->i_files + ci->i_subdirs;
  1862. stat->blocks = 0;
  1863. stat->blksize = 65536;
  1864. }
  1865. }
  1866. return err;
  1867. }