inode.c 49 KB

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