inode.c 62 KB

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