inode.c 51 KB

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