inode.c 48 KB

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