inode.c 56 KB

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