inode.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  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 a newer info
  504. * (e.g., due to inode info racing form multiple MDSs), or if
  505. * we are getting projected (unstable) inode info.
  506. */
  507. if (le64_to_cpu(info->version) > 0 &&
  508. (ci->i_version & ~1) > le64_to_cpu(info->version))
  509. goto no_change;
  510. issued = __ceph_caps_issued(ci, &implemented);
  511. issued |= implemented | __ceph_caps_dirty(ci);
  512. /* update inode */
  513. ci->i_version = le64_to_cpu(info->version);
  514. inode->i_version++;
  515. inode->i_rdev = le32_to_cpu(info->rdev);
  516. if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
  517. inode->i_mode = le32_to_cpu(info->mode);
  518. inode->i_uid = le32_to_cpu(info->uid);
  519. inode->i_gid = le32_to_cpu(info->gid);
  520. dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
  521. inode->i_uid, inode->i_gid);
  522. }
  523. if ((issued & CEPH_CAP_LINK_EXCL) == 0)
  524. inode->i_nlink = le32_to_cpu(info->nlink);
  525. /* be careful with mtime, atime, size */
  526. ceph_decode_timespec(&atime, &info->atime);
  527. ceph_decode_timespec(&mtime, &info->mtime);
  528. ceph_decode_timespec(&ctime, &info->ctime);
  529. queue_trunc = ceph_fill_file_size(inode, issued,
  530. le32_to_cpu(info->truncate_seq),
  531. le64_to_cpu(info->truncate_size),
  532. le64_to_cpu(info->size));
  533. ceph_fill_file_time(inode, issued,
  534. le32_to_cpu(info->time_warp_seq),
  535. &ctime, &mtime, &atime);
  536. ci->i_max_size = le64_to_cpu(info->max_size);
  537. ci->i_layout = info->layout;
  538. inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
  539. /* xattrs */
  540. /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
  541. if ((issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  542. le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
  543. if (ci->i_xattrs.blob)
  544. ceph_buffer_put(ci->i_xattrs.blob);
  545. ci->i_xattrs.blob = xattr_blob;
  546. if (xattr_blob)
  547. memcpy(ci->i_xattrs.blob->vec.iov_base,
  548. iinfo->xattr_data, iinfo->xattr_len);
  549. ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
  550. xattr_blob = NULL;
  551. }
  552. inode->i_mapping->a_ops = &ceph_aops;
  553. inode->i_mapping->backing_dev_info =
  554. &ceph_sb_to_client(inode->i_sb)->backing_dev_info;
  555. switch (inode->i_mode & S_IFMT) {
  556. case S_IFIFO:
  557. case S_IFBLK:
  558. case S_IFCHR:
  559. case S_IFSOCK:
  560. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  561. inode->i_op = &ceph_file_iops;
  562. break;
  563. case S_IFREG:
  564. inode->i_op = &ceph_file_iops;
  565. inode->i_fop = &ceph_file_fops;
  566. break;
  567. case S_IFLNK:
  568. inode->i_op = &ceph_symlink_iops;
  569. if (!ci->i_symlink) {
  570. int symlen = iinfo->symlink_len;
  571. char *sym;
  572. BUG_ON(symlen != inode->i_size);
  573. spin_unlock(&inode->i_lock);
  574. err = -ENOMEM;
  575. sym = kmalloc(symlen+1, GFP_NOFS);
  576. if (!sym)
  577. goto out;
  578. memcpy(sym, iinfo->symlink, symlen);
  579. sym[symlen] = 0;
  580. spin_lock(&inode->i_lock);
  581. if (!ci->i_symlink)
  582. ci->i_symlink = sym;
  583. else
  584. kfree(sym); /* lost a race */
  585. }
  586. break;
  587. case S_IFDIR:
  588. inode->i_op = &ceph_dir_iops;
  589. inode->i_fop = &ceph_dir_fops;
  590. ci->i_files = le64_to_cpu(info->files);
  591. ci->i_subdirs = le64_to_cpu(info->subdirs);
  592. ci->i_rbytes = le64_to_cpu(info->rbytes);
  593. ci->i_rfiles = le64_to_cpu(info->rfiles);
  594. ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
  595. ceph_decode_timespec(&ci->i_rctime, &info->rctime);
  596. /* set dir completion flag? */
  597. if (ci->i_files == 0 && ci->i_subdirs == 0 &&
  598. ceph_snap(inode) == CEPH_NOSNAP &&
  599. (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED) &&
  600. (issued & CEPH_CAP_FILE_EXCL) == 0 &&
  601. (ci->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
  602. dout(" marking %p complete (empty)\n", inode);
  603. ci->i_ceph_flags |= CEPH_I_COMPLETE;
  604. ci->i_max_offset = 2;
  605. }
  606. /* it may be better to set st_size in getattr instead? */
  607. if (ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), RBYTES))
  608. inode->i_size = ci->i_rbytes;
  609. break;
  610. default:
  611. pr_err("fill_inode %llx.%llx BAD mode 0%o\n",
  612. ceph_vinop(inode), inode->i_mode);
  613. }
  614. no_change:
  615. spin_unlock(&inode->i_lock);
  616. /* queue truncate if we saw i_size decrease */
  617. if (queue_trunc)
  618. ceph_queue_vmtruncate(inode);
  619. /* populate frag tree */
  620. /* FIXME: move me up, if/when version reflects fragtree changes */
  621. nsplits = le32_to_cpu(info->fragtree.nsplits);
  622. mutex_lock(&ci->i_fragtree_mutex);
  623. for (i = 0; i < nsplits; i++) {
  624. u32 id = le32_to_cpu(info->fragtree.splits[i].frag);
  625. struct ceph_inode_frag *frag = __get_or_create_frag(ci, id);
  626. if (IS_ERR(frag))
  627. continue;
  628. frag->split_by = le32_to_cpu(info->fragtree.splits[i].by);
  629. dout(" frag %x split by %d\n", frag->frag, frag->split_by);
  630. }
  631. mutex_unlock(&ci->i_fragtree_mutex);
  632. /* were we issued a capability? */
  633. if (info->cap.caps) {
  634. if (ceph_snap(inode) == CEPH_NOSNAP) {
  635. ceph_add_cap(inode, session,
  636. le64_to_cpu(info->cap.cap_id),
  637. cap_fmode,
  638. le32_to_cpu(info->cap.caps),
  639. le32_to_cpu(info->cap.wanted),
  640. le32_to_cpu(info->cap.seq),
  641. le32_to_cpu(info->cap.mseq),
  642. le64_to_cpu(info->cap.realm),
  643. info->cap.flags,
  644. caps_reservation);
  645. } else {
  646. spin_lock(&inode->i_lock);
  647. dout(" %p got snap_caps %s\n", inode,
  648. ceph_cap_string(le32_to_cpu(info->cap.caps)));
  649. ci->i_snap_caps |= le32_to_cpu(info->cap.caps);
  650. if (cap_fmode >= 0)
  651. __ceph_get_fmode(ci, cap_fmode);
  652. spin_unlock(&inode->i_lock);
  653. }
  654. } else if (cap_fmode >= 0) {
  655. pr_warning("mds issued no caps on %llx.%llx\n",
  656. ceph_vinop(inode));
  657. __ceph_get_fmode(ci, cap_fmode);
  658. }
  659. /* update delegation info? */
  660. if (dirinfo)
  661. ceph_fill_dirfrag(inode, dirinfo);
  662. err = 0;
  663. out:
  664. if (xattr_blob)
  665. ceph_buffer_put(xattr_blob);
  666. return err;
  667. }
  668. /*
  669. * caller should hold session s_mutex.
  670. */
  671. static void update_dentry_lease(struct dentry *dentry,
  672. struct ceph_mds_reply_lease *lease,
  673. struct ceph_mds_session *session,
  674. unsigned long from_time)
  675. {
  676. struct ceph_dentry_info *di = ceph_dentry(dentry);
  677. long unsigned duration = le32_to_cpu(lease->duration_ms);
  678. long unsigned ttl = from_time + (duration * HZ) / 1000;
  679. long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
  680. struct inode *dir;
  681. /* only track leases on regular dentries */
  682. if (dentry->d_op != &ceph_dentry_ops)
  683. return;
  684. spin_lock(&dentry->d_lock);
  685. dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n",
  686. dentry, le16_to_cpu(lease->mask), duration, ttl);
  687. /* make lease_rdcache_gen match directory */
  688. dir = dentry->d_parent->d_inode;
  689. di->lease_shared_gen = ceph_inode(dir)->i_shared_gen;
  690. if (lease->mask == 0)
  691. goto out_unlock;
  692. if (di->lease_gen == session->s_cap_gen &&
  693. time_before(ttl, dentry->d_time))
  694. goto out_unlock; /* we already have a newer lease. */
  695. if (di->lease_session && di->lease_session != session)
  696. goto out_unlock;
  697. ceph_dentry_lru_touch(dentry);
  698. if (!di->lease_session)
  699. di->lease_session = ceph_get_mds_session(session);
  700. di->lease_gen = session->s_cap_gen;
  701. di->lease_seq = le32_to_cpu(lease->seq);
  702. di->lease_renew_after = half_ttl;
  703. di->lease_renew_from = 0;
  704. dentry->d_time = ttl;
  705. out_unlock:
  706. spin_unlock(&dentry->d_lock);
  707. return;
  708. }
  709. /*
  710. * Set dentry's directory position based on the current dir's max, and
  711. * order it in d_subdirs, so that dcache_readdir behaves.
  712. */
  713. static void ceph_set_dentry_offset(struct dentry *dn)
  714. {
  715. struct dentry *dir = dn->d_parent;
  716. struct inode *inode = dn->d_parent->d_inode;
  717. struct ceph_dentry_info *di;
  718. BUG_ON(!inode);
  719. di = ceph_dentry(dn);
  720. spin_lock(&inode->i_lock);
  721. if ((ceph_inode(inode)->i_ceph_flags & CEPH_I_COMPLETE) == 0) {
  722. spin_unlock(&inode->i_lock);
  723. return;
  724. }
  725. di->offset = ceph_inode(inode)->i_max_offset++;
  726. spin_unlock(&inode->i_lock);
  727. spin_lock(&dcache_lock);
  728. spin_lock(&dn->d_lock);
  729. list_move(&dn->d_u.d_child, &dir->d_subdirs);
  730. dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset,
  731. dn->d_u.d_child.prev, dn->d_u.d_child.next);
  732. spin_unlock(&dn->d_lock);
  733. spin_unlock(&dcache_lock);
  734. }
  735. /*
  736. * splice a dentry to an inode.
  737. * caller must hold directory i_mutex for this to be safe.
  738. *
  739. * we will only rehash the resulting dentry if @prehash is
  740. * true; @prehash will be set to false (for the benefit of
  741. * the caller) if we fail.
  742. */
  743. static struct dentry *splice_dentry(struct dentry *dn, struct inode *in,
  744. bool *prehash, bool set_offset)
  745. {
  746. struct dentry *realdn;
  747. BUG_ON(dn->d_inode);
  748. /* dn must be unhashed */
  749. if (!d_unhashed(dn))
  750. d_drop(dn);
  751. realdn = d_materialise_unique(dn, in);
  752. if (IS_ERR(realdn)) {
  753. pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
  754. PTR_ERR(realdn), dn, in, ceph_vinop(in));
  755. if (prehash)
  756. *prehash = false; /* don't rehash on error */
  757. dn = realdn; /* note realdn contains the error */
  758. goto out;
  759. } else if (realdn) {
  760. dout("dn %p (%d) spliced with %p (%d) "
  761. "inode %p ino %llx.%llx\n",
  762. dn, atomic_read(&dn->d_count),
  763. realdn, atomic_read(&realdn->d_count),
  764. realdn->d_inode, ceph_vinop(realdn->d_inode));
  765. dput(dn);
  766. dn = realdn;
  767. } else {
  768. BUG_ON(!ceph_dentry(dn));
  769. dout("dn %p attached to %p ino %llx.%llx\n",
  770. dn, dn->d_inode, ceph_vinop(dn->d_inode));
  771. }
  772. if ((!prehash || *prehash) && d_unhashed(dn))
  773. d_rehash(dn);
  774. if (set_offset)
  775. ceph_set_dentry_offset(dn);
  776. out:
  777. return dn;
  778. }
  779. /*
  780. * Incorporate results into the local cache. This is either just
  781. * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
  782. * after a lookup).
  783. *
  784. * A reply may contain
  785. * a directory inode along with a dentry.
  786. * and/or a target inode
  787. *
  788. * Called with snap_rwsem (read).
  789. */
  790. int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,
  791. struct ceph_mds_session *session)
  792. {
  793. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  794. struct inode *in = NULL;
  795. struct ceph_mds_reply_inode *ininfo;
  796. struct ceph_vino vino;
  797. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  798. int i = 0;
  799. int err = 0;
  800. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  801. rinfo->head->is_dentry, rinfo->head->is_target);
  802. #if 0
  803. /*
  804. * Debugging hook:
  805. *
  806. * If we resend completed ops to a recovering mds, we get no
  807. * trace. Since that is very rare, pretend this is the case
  808. * to ensure the 'no trace' handlers in the callers behave.
  809. *
  810. * Fill in inodes unconditionally to avoid breaking cap
  811. * invariants.
  812. */
  813. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  814. pr_info("fill_trace faking empty trace on %lld %s\n",
  815. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  816. if (rinfo->head->is_dentry) {
  817. rinfo->head->is_dentry = 0;
  818. err = fill_inode(req->r_locked_dir,
  819. &rinfo->diri, rinfo->dirfrag,
  820. session, req->r_request_started, -1);
  821. }
  822. if (rinfo->head->is_target) {
  823. rinfo->head->is_target = 0;
  824. ininfo = rinfo->targeti.in;
  825. vino.ino = le64_to_cpu(ininfo->ino);
  826. vino.snap = le64_to_cpu(ininfo->snapid);
  827. in = ceph_get_inode(sb, vino);
  828. err = fill_inode(in, &rinfo->targeti, NULL,
  829. session, req->r_request_started,
  830. req->r_fmode);
  831. iput(in);
  832. }
  833. }
  834. #endif
  835. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  836. dout("fill_trace reply is empty!\n");
  837. if (rinfo->head->result == 0 && req->r_locked_dir)
  838. ceph_invalidate_dir_request(req);
  839. return 0;
  840. }
  841. if (rinfo->head->is_dentry) {
  842. struct inode *dir = req->r_locked_dir;
  843. err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
  844. session, req->r_request_started, -1,
  845. &req->r_caps_reservation);
  846. if (err < 0)
  847. return err;
  848. }
  849. /*
  850. * ignore null lease/binding on snapdir ENOENT, or else we
  851. * will have trouble splicing in the virtual snapdir later
  852. */
  853. if (rinfo->head->is_dentry && !req->r_aborted &&
  854. (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
  855. fsc->mount_options->snapdir_name,
  856. req->r_dentry->d_name.len))) {
  857. /*
  858. * lookup link rename : null -> possibly existing inode
  859. * mknod symlink mkdir : null -> new inode
  860. * unlink : linked -> null
  861. */
  862. struct inode *dir = req->r_locked_dir;
  863. struct dentry *dn = req->r_dentry;
  864. bool have_dir_cap, have_lease;
  865. BUG_ON(!dn);
  866. BUG_ON(!dir);
  867. BUG_ON(dn->d_parent->d_inode != dir);
  868. BUG_ON(ceph_ino(dir) !=
  869. le64_to_cpu(rinfo->diri.in->ino));
  870. BUG_ON(ceph_snap(dir) !=
  871. le64_to_cpu(rinfo->diri.in->snapid));
  872. /* do we have a lease on the whole dir? */
  873. have_dir_cap =
  874. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  875. CEPH_CAP_FILE_SHARED);
  876. /* do we have a dn lease? */
  877. have_lease = have_dir_cap ||
  878. (le16_to_cpu(rinfo->dlease->mask) &
  879. CEPH_LOCK_DN);
  880. if (!have_lease)
  881. dout("fill_trace no dentry lease or dir cap\n");
  882. /* rename? */
  883. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  884. dout(" src %p '%.*s' dst %p '%.*s'\n",
  885. req->r_old_dentry,
  886. req->r_old_dentry->d_name.len,
  887. req->r_old_dentry->d_name.name,
  888. dn, dn->d_name.len, dn->d_name.name);
  889. dout("fill_trace doing d_move %p -> %p\n",
  890. req->r_old_dentry, dn);
  891. /* d_move screws up d_subdirs order */
  892. ceph_i_clear(dir, CEPH_I_COMPLETE);
  893. d_move(req->r_old_dentry, dn);
  894. dout(" src %p '%.*s' dst %p '%.*s'\n",
  895. req->r_old_dentry,
  896. req->r_old_dentry->d_name.len,
  897. req->r_old_dentry->d_name.name,
  898. dn, dn->d_name.len, dn->d_name.name);
  899. /* ensure target dentry is invalidated, despite
  900. rehashing bug in vfs_rename_dir */
  901. ceph_invalidate_dentry_lease(dn);
  902. /* take overwritten dentry's readdir offset */
  903. dout("dn %p gets %p offset %lld (old offset %lld)\n",
  904. req->r_old_dentry, dn, ceph_dentry(dn)->offset,
  905. ceph_dentry(req->r_old_dentry)->offset);
  906. ceph_dentry(req->r_old_dentry)->offset =
  907. ceph_dentry(dn)->offset;
  908. dn = req->r_old_dentry; /* use old_dentry */
  909. in = dn->d_inode;
  910. }
  911. /* null dentry? */
  912. if (!rinfo->head->is_target) {
  913. dout("fill_trace null dentry\n");
  914. if (dn->d_inode) {
  915. dout("d_delete %p\n", dn);
  916. d_delete(dn);
  917. } else {
  918. dout("d_instantiate %p NULL\n", dn);
  919. d_instantiate(dn, NULL);
  920. if (have_lease && d_unhashed(dn))
  921. d_rehash(dn);
  922. update_dentry_lease(dn, rinfo->dlease,
  923. session,
  924. req->r_request_started);
  925. }
  926. goto done;
  927. }
  928. /* attach proper inode */
  929. ininfo = rinfo->targeti.in;
  930. vino.ino = le64_to_cpu(ininfo->ino);
  931. vino.snap = le64_to_cpu(ininfo->snapid);
  932. if (!dn->d_inode) {
  933. in = ceph_get_inode(sb, vino);
  934. if (IS_ERR(in)) {
  935. pr_err("fill_trace bad get_inode "
  936. "%llx.%llx\n", vino.ino, vino.snap);
  937. err = PTR_ERR(in);
  938. d_delete(dn);
  939. goto done;
  940. }
  941. dn = splice_dentry(dn, in, &have_lease, true);
  942. if (IS_ERR(dn)) {
  943. err = PTR_ERR(dn);
  944. goto done;
  945. }
  946. req->r_dentry = dn; /* may have spliced */
  947. igrab(in);
  948. } else if (ceph_ino(in) == vino.ino &&
  949. ceph_snap(in) == vino.snap) {
  950. igrab(in);
  951. } else {
  952. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  953. dn, in, ceph_ino(in), ceph_snap(in),
  954. vino.ino, vino.snap);
  955. have_lease = false;
  956. in = NULL;
  957. }
  958. if (have_lease)
  959. update_dentry_lease(dn, rinfo->dlease, session,
  960. req->r_request_started);
  961. dout(" final dn %p\n", dn);
  962. i++;
  963. } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  964. req->r_op == CEPH_MDS_OP_MKSNAP) {
  965. struct dentry *dn = req->r_dentry;
  966. /* fill out a snapdir LOOKUPSNAP dentry */
  967. BUG_ON(!dn);
  968. BUG_ON(!req->r_locked_dir);
  969. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  970. ininfo = rinfo->targeti.in;
  971. vino.ino = le64_to_cpu(ininfo->ino);
  972. vino.snap = le64_to_cpu(ininfo->snapid);
  973. in = ceph_get_inode(sb, vino);
  974. if (IS_ERR(in)) {
  975. pr_err("fill_inode get_inode badness %llx.%llx\n",
  976. vino.ino, vino.snap);
  977. err = PTR_ERR(in);
  978. d_delete(dn);
  979. goto done;
  980. }
  981. dout(" linking snapped dir %p to dn %p\n", in, dn);
  982. dn = splice_dentry(dn, in, NULL, true);
  983. if (IS_ERR(dn)) {
  984. err = PTR_ERR(dn);
  985. goto done;
  986. }
  987. req->r_dentry = dn; /* may have spliced */
  988. igrab(in);
  989. rinfo->head->is_dentry = 1; /* fool notrace handlers */
  990. }
  991. if (rinfo->head->is_target) {
  992. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  993. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  994. if (in == NULL || ceph_ino(in) != vino.ino ||
  995. ceph_snap(in) != vino.snap) {
  996. in = ceph_get_inode(sb, vino);
  997. if (IS_ERR(in)) {
  998. err = PTR_ERR(in);
  999. goto done;
  1000. }
  1001. }
  1002. req->r_target_inode = in;
  1003. err = fill_inode(in,
  1004. &rinfo->targeti, NULL,
  1005. session, req->r_request_started,
  1006. (le32_to_cpu(rinfo->head->result) == 0) ?
  1007. req->r_fmode : -1,
  1008. &req->r_caps_reservation);
  1009. if (err < 0) {
  1010. pr_err("fill_inode badness %p %llx.%llx\n",
  1011. in, ceph_vinop(in));
  1012. goto done;
  1013. }
  1014. }
  1015. done:
  1016. dout("fill_trace done err=%d\n", err);
  1017. return err;
  1018. }
  1019. /*
  1020. * Prepopulate our cache with readdir results, leases, etc.
  1021. */
  1022. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1023. struct ceph_mds_session *session)
  1024. {
  1025. struct dentry *parent = req->r_dentry;
  1026. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1027. struct qstr dname;
  1028. struct dentry *dn;
  1029. struct inode *in;
  1030. int err = 0, i;
  1031. struct inode *snapdir = NULL;
  1032. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1033. u64 frag = le32_to_cpu(rhead->args.readdir.frag);
  1034. struct ceph_dentry_info *di;
  1035. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1036. snapdir = ceph_get_snapdir(parent->d_inode);
  1037. parent = d_find_alias(snapdir);
  1038. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1039. rinfo->dir_nr, parent);
  1040. } else {
  1041. dout("readdir_prepopulate %d items under dn %p\n",
  1042. rinfo->dir_nr, parent);
  1043. if (rinfo->dir_dir)
  1044. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  1045. }
  1046. for (i = 0; i < rinfo->dir_nr; i++) {
  1047. struct ceph_vino vino;
  1048. dname.name = rinfo->dir_dname[i];
  1049. dname.len = rinfo->dir_dname_len[i];
  1050. dname.hash = full_name_hash(dname.name, dname.len);
  1051. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1052. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1053. retry_lookup:
  1054. dn = d_lookup(parent, &dname);
  1055. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1056. parent, dname.len, dname.name, dn);
  1057. if (!dn) {
  1058. dn = d_alloc(parent, &dname);
  1059. dout("d_alloc %p '%.*s' = %p\n", parent,
  1060. dname.len, dname.name, dn);
  1061. if (dn == NULL) {
  1062. dout("d_alloc badness\n");
  1063. err = -ENOMEM;
  1064. goto out;
  1065. }
  1066. err = ceph_init_dentry(dn);
  1067. if (err < 0) {
  1068. dput(dn);
  1069. goto out;
  1070. }
  1071. } else if (dn->d_inode &&
  1072. (ceph_ino(dn->d_inode) != vino.ino ||
  1073. ceph_snap(dn->d_inode) != vino.snap)) {
  1074. dout(" dn %p points to wrong inode %p\n",
  1075. dn, dn->d_inode);
  1076. d_delete(dn);
  1077. dput(dn);
  1078. goto retry_lookup;
  1079. } else {
  1080. /* reorder parent's d_subdirs */
  1081. spin_lock(&dcache_lock);
  1082. spin_lock(&dn->d_lock);
  1083. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1084. spin_unlock(&dn->d_lock);
  1085. spin_unlock(&dcache_lock);
  1086. }
  1087. di = dn->d_fsdata;
  1088. di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
  1089. /* inode */
  1090. if (dn->d_inode) {
  1091. in = dn->d_inode;
  1092. } else {
  1093. in = ceph_get_inode(parent->d_sb, vino);
  1094. if (IS_ERR(in)) {
  1095. dout("new_inode badness\n");
  1096. d_delete(dn);
  1097. dput(dn);
  1098. err = PTR_ERR(in);
  1099. goto out;
  1100. }
  1101. dn = splice_dentry(dn, in, NULL, false);
  1102. if (IS_ERR(dn))
  1103. dn = NULL;
  1104. }
  1105. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1106. req->r_request_started, -1,
  1107. &req->r_caps_reservation) < 0) {
  1108. pr_err("fill_inode badness on %p\n", in);
  1109. goto next_item;
  1110. }
  1111. if (dn)
  1112. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1113. req->r_session,
  1114. req->r_request_started);
  1115. next_item:
  1116. if (dn)
  1117. dput(dn);
  1118. }
  1119. req->r_did_prepopulate = true;
  1120. out:
  1121. if (snapdir) {
  1122. iput(snapdir);
  1123. dput(parent);
  1124. }
  1125. dout("readdir_prepopulate done\n");
  1126. return err;
  1127. }
  1128. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1129. {
  1130. struct ceph_inode_info *ci = ceph_inode(inode);
  1131. int ret = 0;
  1132. spin_lock(&inode->i_lock);
  1133. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1134. inode->i_size = size;
  1135. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1136. /* tell the MDS if we are approaching max_size */
  1137. if ((size << 1) >= ci->i_max_size &&
  1138. (ci->i_reported_size << 1) < ci->i_max_size)
  1139. ret = 1;
  1140. spin_unlock(&inode->i_lock);
  1141. return ret;
  1142. }
  1143. /*
  1144. * Write back inode data in a worker thread. (This can't be done
  1145. * in the message handler context.)
  1146. */
  1147. void ceph_queue_writeback(struct inode *inode)
  1148. {
  1149. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1150. &ceph_inode(inode)->i_wb_work)) {
  1151. dout("ceph_queue_writeback %p\n", inode);
  1152. igrab(inode);
  1153. } else {
  1154. dout("ceph_queue_writeback %p failed\n", inode);
  1155. }
  1156. }
  1157. static void ceph_writeback_work(struct work_struct *work)
  1158. {
  1159. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1160. i_wb_work);
  1161. struct inode *inode = &ci->vfs_inode;
  1162. dout("writeback %p\n", inode);
  1163. filemap_fdatawrite(&inode->i_data);
  1164. iput(inode);
  1165. }
  1166. /*
  1167. * queue an async invalidation
  1168. */
  1169. void ceph_queue_invalidate(struct inode *inode)
  1170. {
  1171. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1172. &ceph_inode(inode)->i_pg_inv_work)) {
  1173. dout("ceph_queue_invalidate %p\n", inode);
  1174. igrab(inode);
  1175. } else {
  1176. dout("ceph_queue_invalidate %p failed\n", inode);
  1177. }
  1178. }
  1179. /*
  1180. * invalidate any pages that are not dirty or under writeback. this
  1181. * includes pages that are clean and mapped.
  1182. */
  1183. static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
  1184. {
  1185. struct pagevec pvec;
  1186. pgoff_t next = 0;
  1187. int i;
  1188. pagevec_init(&pvec, 0);
  1189. while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  1190. for (i = 0; i < pagevec_count(&pvec); i++) {
  1191. struct page *page = pvec.pages[i];
  1192. pgoff_t index;
  1193. int skip_page =
  1194. (PageDirty(page) || PageWriteback(page));
  1195. if (!skip_page)
  1196. skip_page = !trylock_page(page);
  1197. /*
  1198. * We really shouldn't be looking at the ->index of an
  1199. * unlocked page. But we're not allowed to lock these
  1200. * pages. So we rely upon nobody altering the ->index
  1201. * of this (pinned-by-us) page.
  1202. */
  1203. index = page->index;
  1204. if (index > next)
  1205. next = index;
  1206. next++;
  1207. if (skip_page)
  1208. continue;
  1209. generic_error_remove_page(mapping, page);
  1210. unlock_page(page);
  1211. }
  1212. pagevec_release(&pvec);
  1213. cond_resched();
  1214. }
  1215. }
  1216. /*
  1217. * Invalidate inode pages in a worker thread. (This can't be done
  1218. * in the message handler context.)
  1219. */
  1220. static void ceph_invalidate_work(struct work_struct *work)
  1221. {
  1222. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1223. i_pg_inv_work);
  1224. struct inode *inode = &ci->vfs_inode;
  1225. u32 orig_gen;
  1226. int check = 0;
  1227. spin_lock(&inode->i_lock);
  1228. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1229. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1230. if (ci->i_rdcache_gen == 0 ||
  1231. ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1232. BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
  1233. /* nevermind! */
  1234. ci->i_rdcache_revoking = 0;
  1235. spin_unlock(&inode->i_lock);
  1236. goto out;
  1237. }
  1238. orig_gen = ci->i_rdcache_gen;
  1239. spin_unlock(&inode->i_lock);
  1240. ceph_invalidate_nondirty_pages(inode->i_mapping);
  1241. spin_lock(&inode->i_lock);
  1242. if (orig_gen == ci->i_rdcache_gen) {
  1243. dout("invalidate_pages %p gen %d successful\n", inode,
  1244. ci->i_rdcache_gen);
  1245. ci->i_rdcache_gen = 0;
  1246. ci->i_rdcache_revoking = 0;
  1247. check = 1;
  1248. } else {
  1249. dout("invalidate_pages %p gen %d raced, gen now %d\n",
  1250. inode, orig_gen, ci->i_rdcache_gen);
  1251. }
  1252. spin_unlock(&inode->i_lock);
  1253. if (check)
  1254. ceph_check_caps(ci, 0, NULL);
  1255. out:
  1256. iput(inode);
  1257. }
  1258. /*
  1259. * called by trunc_wq; take i_mutex ourselves
  1260. *
  1261. * We also truncate in a separate thread as well.
  1262. */
  1263. static void ceph_vmtruncate_work(struct work_struct *work)
  1264. {
  1265. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1266. i_vmtruncate_work);
  1267. struct inode *inode = &ci->vfs_inode;
  1268. dout("vmtruncate_work %p\n", inode);
  1269. mutex_lock(&inode->i_mutex);
  1270. __ceph_do_pending_vmtruncate(inode);
  1271. mutex_unlock(&inode->i_mutex);
  1272. iput(inode);
  1273. }
  1274. /*
  1275. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1276. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1277. */
  1278. void ceph_queue_vmtruncate(struct inode *inode)
  1279. {
  1280. struct ceph_inode_info *ci = ceph_inode(inode);
  1281. if (queue_work(ceph_sb_to_client(inode->i_sb)->trunc_wq,
  1282. &ci->i_vmtruncate_work)) {
  1283. dout("ceph_queue_vmtruncate %p\n", inode);
  1284. igrab(inode);
  1285. } else {
  1286. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1287. inode, ci->i_truncate_pending);
  1288. }
  1289. }
  1290. /*
  1291. * called with i_mutex held.
  1292. *
  1293. * Make sure any pending truncation is applied before doing anything
  1294. * that may depend on it.
  1295. */
  1296. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1297. {
  1298. struct ceph_inode_info *ci = ceph_inode(inode);
  1299. u64 to;
  1300. int wrbuffer_refs, wake = 0;
  1301. retry:
  1302. spin_lock(&inode->i_lock);
  1303. if (ci->i_truncate_pending == 0) {
  1304. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1305. spin_unlock(&inode->i_lock);
  1306. return;
  1307. }
  1308. /*
  1309. * make sure any dirty snapped pages are flushed before we
  1310. * possibly truncate them.. so write AND block!
  1311. */
  1312. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1313. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1314. inode);
  1315. spin_unlock(&inode->i_lock);
  1316. filemap_write_and_wait_range(&inode->i_data, 0,
  1317. inode->i_sb->s_maxbytes);
  1318. goto retry;
  1319. }
  1320. to = ci->i_truncate_size;
  1321. wrbuffer_refs = ci->i_wrbuffer_ref;
  1322. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1323. ci->i_truncate_pending, to);
  1324. spin_unlock(&inode->i_lock);
  1325. truncate_inode_pages(inode->i_mapping, to);
  1326. spin_lock(&inode->i_lock);
  1327. ci->i_truncate_pending--;
  1328. if (ci->i_truncate_pending == 0)
  1329. wake = 1;
  1330. spin_unlock(&inode->i_lock);
  1331. if (wrbuffer_refs == 0)
  1332. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1333. if (wake)
  1334. wake_up_all(&ci->i_cap_wq);
  1335. }
  1336. /*
  1337. * symlinks
  1338. */
  1339. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1340. {
  1341. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1342. nd_set_link(nd, ci->i_symlink);
  1343. return NULL;
  1344. }
  1345. static const struct inode_operations ceph_symlink_iops = {
  1346. .readlink = generic_readlink,
  1347. .follow_link = ceph_sym_follow_link,
  1348. };
  1349. /*
  1350. * setattr
  1351. */
  1352. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1353. {
  1354. struct inode *inode = dentry->d_inode;
  1355. struct ceph_inode_info *ci = ceph_inode(inode);
  1356. struct inode *parent_inode = dentry->d_parent->d_inode;
  1357. const unsigned int ia_valid = attr->ia_valid;
  1358. struct ceph_mds_request *req;
  1359. struct ceph_mds_client *mdsc = ceph_sb_to_client(dentry->d_sb)->mdsc;
  1360. int issued;
  1361. int release = 0, dirtied = 0;
  1362. int mask = 0;
  1363. int err = 0;
  1364. if (ceph_snap(inode) != CEPH_NOSNAP)
  1365. return -EROFS;
  1366. __ceph_do_pending_vmtruncate(inode);
  1367. err = inode_change_ok(inode, attr);
  1368. if (err != 0)
  1369. return err;
  1370. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1371. USE_AUTH_MDS);
  1372. if (IS_ERR(req))
  1373. return PTR_ERR(req);
  1374. spin_lock(&inode->i_lock);
  1375. issued = __ceph_caps_issued(ci, NULL);
  1376. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1377. if (ia_valid & ATTR_UID) {
  1378. dout("setattr %p uid %d -> %d\n", inode,
  1379. inode->i_uid, attr->ia_uid);
  1380. if (issued & CEPH_CAP_AUTH_EXCL) {
  1381. inode->i_uid = attr->ia_uid;
  1382. dirtied |= CEPH_CAP_AUTH_EXCL;
  1383. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1384. attr->ia_uid != inode->i_uid) {
  1385. req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
  1386. mask |= CEPH_SETATTR_UID;
  1387. release |= CEPH_CAP_AUTH_SHARED;
  1388. }
  1389. }
  1390. if (ia_valid & ATTR_GID) {
  1391. dout("setattr %p gid %d -> %d\n", inode,
  1392. inode->i_gid, attr->ia_gid);
  1393. if (issued & CEPH_CAP_AUTH_EXCL) {
  1394. inode->i_gid = attr->ia_gid;
  1395. dirtied |= CEPH_CAP_AUTH_EXCL;
  1396. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1397. attr->ia_gid != inode->i_gid) {
  1398. req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
  1399. mask |= CEPH_SETATTR_GID;
  1400. release |= CEPH_CAP_AUTH_SHARED;
  1401. }
  1402. }
  1403. if (ia_valid & ATTR_MODE) {
  1404. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1405. attr->ia_mode);
  1406. if (issued & CEPH_CAP_AUTH_EXCL) {
  1407. inode->i_mode = attr->ia_mode;
  1408. dirtied |= CEPH_CAP_AUTH_EXCL;
  1409. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1410. attr->ia_mode != inode->i_mode) {
  1411. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1412. mask |= CEPH_SETATTR_MODE;
  1413. release |= CEPH_CAP_AUTH_SHARED;
  1414. }
  1415. }
  1416. if (ia_valid & ATTR_ATIME) {
  1417. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1418. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1419. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1420. if (issued & CEPH_CAP_FILE_EXCL) {
  1421. ci->i_time_warp_seq++;
  1422. inode->i_atime = attr->ia_atime;
  1423. dirtied |= CEPH_CAP_FILE_EXCL;
  1424. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1425. timespec_compare(&inode->i_atime,
  1426. &attr->ia_atime) < 0) {
  1427. inode->i_atime = attr->ia_atime;
  1428. dirtied |= CEPH_CAP_FILE_WR;
  1429. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1430. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1431. ceph_encode_timespec(&req->r_args.setattr.atime,
  1432. &attr->ia_atime);
  1433. mask |= CEPH_SETATTR_ATIME;
  1434. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1435. CEPH_CAP_FILE_WR;
  1436. }
  1437. }
  1438. if (ia_valid & ATTR_MTIME) {
  1439. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1440. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1441. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1442. if (issued & CEPH_CAP_FILE_EXCL) {
  1443. ci->i_time_warp_seq++;
  1444. inode->i_mtime = attr->ia_mtime;
  1445. dirtied |= CEPH_CAP_FILE_EXCL;
  1446. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1447. timespec_compare(&inode->i_mtime,
  1448. &attr->ia_mtime) < 0) {
  1449. inode->i_mtime = attr->ia_mtime;
  1450. dirtied |= CEPH_CAP_FILE_WR;
  1451. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1452. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1453. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1454. &attr->ia_mtime);
  1455. mask |= CEPH_SETATTR_MTIME;
  1456. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1457. CEPH_CAP_FILE_WR;
  1458. }
  1459. }
  1460. if (ia_valid & ATTR_SIZE) {
  1461. dout("setattr %p size %lld -> %lld\n", inode,
  1462. inode->i_size, attr->ia_size);
  1463. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1464. err = -EINVAL;
  1465. goto out;
  1466. }
  1467. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1468. attr->ia_size > inode->i_size) {
  1469. inode->i_size = attr->ia_size;
  1470. inode->i_blocks =
  1471. (attr->ia_size + (1 << 9) - 1) >> 9;
  1472. inode->i_ctime = attr->ia_ctime;
  1473. ci->i_reported_size = attr->ia_size;
  1474. dirtied |= CEPH_CAP_FILE_EXCL;
  1475. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1476. attr->ia_size != inode->i_size) {
  1477. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1478. req->r_args.setattr.old_size =
  1479. cpu_to_le64(inode->i_size);
  1480. mask |= CEPH_SETATTR_SIZE;
  1481. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1482. CEPH_CAP_FILE_WR;
  1483. }
  1484. }
  1485. /* these do nothing */
  1486. if (ia_valid & ATTR_CTIME) {
  1487. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1488. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1489. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1490. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1491. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1492. only ? "ctime only" : "ignored");
  1493. inode->i_ctime = attr->ia_ctime;
  1494. if (only) {
  1495. /*
  1496. * if kernel wants to dirty ctime but nothing else,
  1497. * we need to choose a cap to dirty under, or do
  1498. * a almost-no-op setattr
  1499. */
  1500. if (issued & CEPH_CAP_AUTH_EXCL)
  1501. dirtied |= CEPH_CAP_AUTH_EXCL;
  1502. else if (issued & CEPH_CAP_FILE_EXCL)
  1503. dirtied |= CEPH_CAP_FILE_EXCL;
  1504. else if (issued & CEPH_CAP_XATTR_EXCL)
  1505. dirtied |= CEPH_CAP_XATTR_EXCL;
  1506. else
  1507. mask |= CEPH_SETATTR_CTIME;
  1508. }
  1509. }
  1510. if (ia_valid & ATTR_FILE)
  1511. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1512. if (dirtied) {
  1513. __ceph_mark_dirty_caps(ci, dirtied);
  1514. inode->i_ctime = CURRENT_TIME;
  1515. }
  1516. release &= issued;
  1517. spin_unlock(&inode->i_lock);
  1518. if (mask) {
  1519. req->r_inode = igrab(inode);
  1520. req->r_inode_drop = release;
  1521. req->r_args.setattr.mask = cpu_to_le32(mask);
  1522. req->r_num_caps = 1;
  1523. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1524. }
  1525. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1526. ceph_cap_string(dirtied), mask);
  1527. ceph_mdsc_put_request(req);
  1528. __ceph_do_pending_vmtruncate(inode);
  1529. return err;
  1530. out:
  1531. spin_unlock(&inode->i_lock);
  1532. ceph_mdsc_put_request(req);
  1533. return err;
  1534. }
  1535. /*
  1536. * Verify that we have a lease on the given mask. If not,
  1537. * do a getattr against an mds.
  1538. */
  1539. int ceph_do_getattr(struct inode *inode, int mask)
  1540. {
  1541. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  1542. struct ceph_mds_client *mdsc = fsc->mdsc;
  1543. struct ceph_mds_request *req;
  1544. int err;
  1545. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1546. dout("do_getattr inode %p SNAPDIR\n", inode);
  1547. return 0;
  1548. }
  1549. dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
  1550. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1551. return 0;
  1552. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1553. if (IS_ERR(req))
  1554. return PTR_ERR(req);
  1555. req->r_inode = igrab(inode);
  1556. req->r_num_caps = 1;
  1557. req->r_args.getattr.mask = cpu_to_le32(mask);
  1558. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1559. ceph_mdsc_put_request(req);
  1560. dout("do_getattr result=%d\n", err);
  1561. return err;
  1562. }
  1563. /*
  1564. * Check inode permissions. We verify we have a valid value for
  1565. * the AUTH cap, then call the generic handler.
  1566. */
  1567. int ceph_permission(struct inode *inode, int mask)
  1568. {
  1569. int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1570. if (!err)
  1571. err = generic_permission(inode, mask, NULL);
  1572. return err;
  1573. }
  1574. /*
  1575. * Get all attributes. Hopefully somedata we'll have a statlite()
  1576. * and can limit the fields we require to be accurate.
  1577. */
  1578. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1579. struct kstat *stat)
  1580. {
  1581. struct inode *inode = dentry->d_inode;
  1582. struct ceph_inode_info *ci = ceph_inode(inode);
  1583. int err;
  1584. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1585. if (!err) {
  1586. generic_fillattr(inode, stat);
  1587. stat->ino = inode->i_ino;
  1588. if (ceph_snap(inode) != CEPH_NOSNAP)
  1589. stat->dev = ceph_snap(inode);
  1590. else
  1591. stat->dev = 0;
  1592. if (S_ISDIR(inode->i_mode)) {
  1593. stat->size = ci->i_rbytes;
  1594. stat->blocks = 0;
  1595. stat->blksize = 65536;
  1596. }
  1597. }
  1598. return err;
  1599. }