inode.c 44 KB

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