inode.c 60 KB

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