inode.c 49 KB

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