inode.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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. int i = 0;
  782. int err = 0;
  783. dout("fill_trace %p is_dentry %d is_target %d\n", req,
  784. rinfo->head->is_dentry, rinfo->head->is_target);
  785. #if 0
  786. /*
  787. * Debugging hook:
  788. *
  789. * If we resend completed ops to a recovering mds, we get no
  790. * trace. Since that is very rare, pretend this is the case
  791. * to ensure the 'no trace' handlers in the callers behave.
  792. *
  793. * Fill in inodes unconditionally to avoid breaking cap
  794. * invariants.
  795. */
  796. if (rinfo->head->op & CEPH_MDS_OP_WRITE) {
  797. pr_info("fill_trace faking empty trace on %lld %s\n",
  798. req->r_tid, ceph_mds_op_name(rinfo->head->op));
  799. if (rinfo->head->is_dentry) {
  800. rinfo->head->is_dentry = 0;
  801. err = fill_inode(req->r_locked_dir,
  802. &rinfo->diri, rinfo->dirfrag,
  803. session, req->r_request_started, -1);
  804. }
  805. if (rinfo->head->is_target) {
  806. rinfo->head->is_target = 0;
  807. ininfo = rinfo->targeti.in;
  808. vino.ino = le64_to_cpu(ininfo->ino);
  809. vino.snap = le64_to_cpu(ininfo->snapid);
  810. in = ceph_get_inode(sb, vino);
  811. err = fill_inode(in, &rinfo->targeti, NULL,
  812. session, req->r_request_started,
  813. req->r_fmode);
  814. iput(in);
  815. }
  816. }
  817. #endif
  818. if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
  819. dout("fill_trace reply is empty!\n");
  820. if (rinfo->head->result == 0 && req->r_locked_dir) {
  821. struct ceph_inode_info *ci =
  822. ceph_inode(req->r_locked_dir);
  823. dout(" clearing %p complete (empty trace)\n",
  824. req->r_locked_dir);
  825. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  826. ci->i_release_count++;
  827. }
  828. return 0;
  829. }
  830. if (rinfo->head->is_dentry) {
  831. struct inode *dir = req->r_locked_dir;
  832. err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag,
  833. session, req->r_request_started, -1,
  834. &req->r_caps_reservation);
  835. if (err < 0)
  836. return err;
  837. }
  838. if (rinfo->head->is_dentry && !req->r_aborted) {
  839. /*
  840. * lookup link rename : null -> possibly existing inode
  841. * mknod symlink mkdir : null -> new inode
  842. * unlink : linked -> null
  843. */
  844. struct inode *dir = req->r_locked_dir;
  845. struct dentry *dn = req->r_dentry;
  846. bool have_dir_cap, have_lease;
  847. BUG_ON(!dn);
  848. BUG_ON(!dir);
  849. BUG_ON(dn->d_parent->d_inode != dir);
  850. BUG_ON(ceph_ino(dir) !=
  851. le64_to_cpu(rinfo->diri.in->ino));
  852. BUG_ON(ceph_snap(dir) !=
  853. le64_to_cpu(rinfo->diri.in->snapid));
  854. /* do we have a lease on the whole dir? */
  855. have_dir_cap =
  856. (le32_to_cpu(rinfo->diri.in->cap.caps) &
  857. CEPH_CAP_FILE_SHARED);
  858. /* do we have a dn lease? */
  859. have_lease = have_dir_cap ||
  860. (le16_to_cpu(rinfo->dlease->mask) &
  861. CEPH_LOCK_DN);
  862. if (!have_lease)
  863. dout("fill_trace no dentry lease or dir cap\n");
  864. /* rename? */
  865. if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
  866. dout(" src %p '%.*s' dst %p '%.*s'\n",
  867. req->r_old_dentry,
  868. req->r_old_dentry->d_name.len,
  869. req->r_old_dentry->d_name.name,
  870. dn, dn->d_name.len, dn->d_name.name);
  871. dout("fill_trace doing d_move %p -> %p\n",
  872. req->r_old_dentry, dn);
  873. d_move(req->r_old_dentry, dn);
  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. /* ensure target dentry is invalidated, despite
  880. rehashing bug in vfs_rename_dir */
  881. dn->d_time = jiffies;
  882. ceph_dentry(dn)->lease_shared_gen = 0;
  883. /* take overwritten dentry's readdir offset */
  884. ceph_dentry(req->r_old_dentry)->offset =
  885. ceph_dentry(dn)->offset;
  886. dn = req->r_old_dentry; /* use old_dentry */
  887. in = dn->d_inode;
  888. }
  889. /* null dentry? */
  890. if (!rinfo->head->is_target) {
  891. dout("fill_trace null dentry\n");
  892. if (dn->d_inode) {
  893. dout("d_delete %p\n", dn);
  894. d_delete(dn);
  895. } else {
  896. dout("d_instantiate %p NULL\n", dn);
  897. d_instantiate(dn, NULL);
  898. if (have_lease && d_unhashed(dn))
  899. d_rehash(dn);
  900. update_dentry_lease(dn, rinfo->dlease,
  901. session,
  902. req->r_request_started);
  903. }
  904. goto done;
  905. }
  906. /* attach proper inode */
  907. ininfo = rinfo->targeti.in;
  908. vino.ino = le64_to_cpu(ininfo->ino);
  909. vino.snap = le64_to_cpu(ininfo->snapid);
  910. if (!dn->d_inode) {
  911. in = ceph_get_inode(sb, vino);
  912. if (IS_ERR(in)) {
  913. pr_err("fill_trace bad get_inode "
  914. "%llx.%llx\n", vino.ino, vino.snap);
  915. err = PTR_ERR(in);
  916. d_delete(dn);
  917. goto done;
  918. }
  919. dn = splice_dentry(dn, in, &have_lease);
  920. if (IS_ERR(dn)) {
  921. err = PTR_ERR(dn);
  922. goto done;
  923. }
  924. req->r_dentry = dn; /* may have spliced */
  925. ceph_set_dentry_offset(dn);
  926. igrab(in);
  927. } else if (ceph_ino(in) == vino.ino &&
  928. ceph_snap(in) == vino.snap) {
  929. igrab(in);
  930. } else {
  931. dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
  932. dn, in, ceph_ino(in), ceph_snap(in),
  933. vino.ino, vino.snap);
  934. have_lease = false;
  935. in = NULL;
  936. }
  937. if (have_lease)
  938. update_dentry_lease(dn, rinfo->dlease, session,
  939. req->r_request_started);
  940. dout(" final dn %p\n", dn);
  941. i++;
  942. } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
  943. req->r_op == CEPH_MDS_OP_MKSNAP) {
  944. struct dentry *dn = req->r_dentry;
  945. /* fill out a snapdir LOOKUPSNAP dentry */
  946. BUG_ON(!dn);
  947. BUG_ON(!req->r_locked_dir);
  948. BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR);
  949. ininfo = rinfo->targeti.in;
  950. vino.ino = le64_to_cpu(ininfo->ino);
  951. vino.snap = le64_to_cpu(ininfo->snapid);
  952. in = ceph_get_inode(sb, vino);
  953. if (IS_ERR(in)) {
  954. pr_err("fill_inode get_inode badness %llx.%llx\n",
  955. vino.ino, vino.snap);
  956. err = PTR_ERR(in);
  957. d_delete(dn);
  958. goto done;
  959. }
  960. dout(" linking snapped dir %p to dn %p\n", in, dn);
  961. dn = splice_dentry(dn, in, NULL);
  962. if (IS_ERR(dn)) {
  963. err = PTR_ERR(dn);
  964. goto done;
  965. }
  966. ceph_set_dentry_offset(dn);
  967. req->r_dentry = dn; /* may have spliced */
  968. igrab(in);
  969. rinfo->head->is_dentry = 1; /* fool notrace handlers */
  970. }
  971. if (rinfo->head->is_target) {
  972. vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
  973. vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
  974. if (in == NULL || ceph_ino(in) != vino.ino ||
  975. ceph_snap(in) != vino.snap) {
  976. in = ceph_get_inode(sb, vino);
  977. if (IS_ERR(in)) {
  978. err = PTR_ERR(in);
  979. goto done;
  980. }
  981. }
  982. req->r_target_inode = in;
  983. err = fill_inode(in,
  984. &rinfo->targeti, NULL,
  985. session, req->r_request_started,
  986. (le32_to_cpu(rinfo->head->result) == 0) ?
  987. req->r_fmode : -1,
  988. &req->r_caps_reservation);
  989. if (err < 0) {
  990. pr_err("fill_inode badness %p %llx.%llx\n",
  991. in, ceph_vinop(in));
  992. goto done;
  993. }
  994. }
  995. done:
  996. dout("fill_trace done err=%d\n", err);
  997. return err;
  998. }
  999. /*
  1000. * Prepopulate our cache with readdir results, leases, etc.
  1001. */
  1002. int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  1003. struct ceph_mds_session *session)
  1004. {
  1005. struct dentry *parent = req->r_dentry;
  1006. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  1007. struct qstr dname;
  1008. struct dentry *dn;
  1009. struct inode *in;
  1010. int err = 0, i;
  1011. struct inode *snapdir = NULL;
  1012. struct ceph_mds_request_head *rhead = req->r_request->front.iov_base;
  1013. u64 frag = le32_to_cpu(rhead->args.readdir.frag);
  1014. struct ceph_dentry_info *di;
  1015. if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
  1016. snapdir = ceph_get_snapdir(parent->d_inode);
  1017. parent = d_find_alias(snapdir);
  1018. dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
  1019. rinfo->dir_nr, parent);
  1020. } else {
  1021. dout("readdir_prepopulate %d items under dn %p\n",
  1022. rinfo->dir_nr, parent);
  1023. if (rinfo->dir_dir)
  1024. ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir);
  1025. }
  1026. for (i = 0; i < rinfo->dir_nr; i++) {
  1027. struct ceph_vino vino;
  1028. dname.name = rinfo->dir_dname[i];
  1029. dname.len = rinfo->dir_dname_len[i];
  1030. dname.hash = full_name_hash(dname.name, dname.len);
  1031. vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino);
  1032. vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid);
  1033. retry_lookup:
  1034. dn = d_lookup(parent, &dname);
  1035. dout("d_lookup on parent=%p name=%.*s got %p\n",
  1036. parent, dname.len, dname.name, dn);
  1037. if (!dn) {
  1038. dn = d_alloc(parent, &dname);
  1039. dout("d_alloc %p '%.*s' = %p\n", parent,
  1040. dname.len, dname.name, dn);
  1041. if (dn == NULL) {
  1042. dout("d_alloc badness\n");
  1043. err = -ENOMEM;
  1044. goto out;
  1045. }
  1046. err = ceph_init_dentry(dn);
  1047. if (err < 0)
  1048. goto out;
  1049. } else if (dn->d_inode &&
  1050. (ceph_ino(dn->d_inode) != vino.ino ||
  1051. ceph_snap(dn->d_inode) != vino.snap)) {
  1052. dout(" dn %p points to wrong inode %p\n",
  1053. dn, dn->d_inode);
  1054. d_delete(dn);
  1055. dput(dn);
  1056. goto retry_lookup;
  1057. } else {
  1058. /* reorder parent's d_subdirs */
  1059. spin_lock(&dcache_lock);
  1060. spin_lock(&dn->d_lock);
  1061. list_move(&dn->d_u.d_child, &parent->d_subdirs);
  1062. spin_unlock(&dn->d_lock);
  1063. spin_unlock(&dcache_lock);
  1064. }
  1065. di = dn->d_fsdata;
  1066. di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset);
  1067. /* inode */
  1068. if (dn->d_inode) {
  1069. in = dn->d_inode;
  1070. } else {
  1071. in = ceph_get_inode(parent->d_sb, vino);
  1072. if (in == NULL) {
  1073. dout("new_inode badness\n");
  1074. d_delete(dn);
  1075. dput(dn);
  1076. err = -ENOMEM;
  1077. goto out;
  1078. }
  1079. dn = splice_dentry(dn, in, NULL);
  1080. }
  1081. if (fill_inode(in, &rinfo->dir_in[i], NULL, session,
  1082. req->r_request_started, -1,
  1083. &req->r_caps_reservation) < 0) {
  1084. pr_err("fill_inode badness on %p\n", in);
  1085. dput(dn);
  1086. continue;
  1087. }
  1088. update_dentry_lease(dn, rinfo->dir_dlease[i],
  1089. req->r_session, req->r_request_started);
  1090. dput(dn);
  1091. }
  1092. req->r_did_prepopulate = true;
  1093. out:
  1094. if (snapdir) {
  1095. iput(snapdir);
  1096. dput(parent);
  1097. }
  1098. dout("readdir_prepopulate done\n");
  1099. return err;
  1100. }
  1101. int ceph_inode_set_size(struct inode *inode, loff_t size)
  1102. {
  1103. struct ceph_inode_info *ci = ceph_inode(inode);
  1104. int ret = 0;
  1105. spin_lock(&inode->i_lock);
  1106. dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size);
  1107. inode->i_size = size;
  1108. inode->i_blocks = (size + (1 << 9) - 1) >> 9;
  1109. /* tell the MDS if we are approaching max_size */
  1110. if ((size << 1) >= ci->i_max_size &&
  1111. (ci->i_reported_size << 1) < ci->i_max_size)
  1112. ret = 1;
  1113. spin_unlock(&inode->i_lock);
  1114. return ret;
  1115. }
  1116. /*
  1117. * Write back inode data in a worker thread. (This can't be done
  1118. * in the message handler context.)
  1119. */
  1120. void ceph_queue_writeback(struct inode *inode)
  1121. {
  1122. if (queue_work(ceph_inode_to_client(inode)->wb_wq,
  1123. &ceph_inode(inode)->i_wb_work)) {
  1124. dout("ceph_queue_writeback %p\n", inode);
  1125. igrab(inode);
  1126. } else {
  1127. dout("ceph_queue_writeback %p failed\n", inode);
  1128. }
  1129. }
  1130. static void ceph_writeback_work(struct work_struct *work)
  1131. {
  1132. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1133. i_wb_work);
  1134. struct inode *inode = &ci->vfs_inode;
  1135. dout("writeback %p\n", inode);
  1136. filemap_fdatawrite(&inode->i_data);
  1137. iput(inode);
  1138. }
  1139. /*
  1140. * queue an async invalidation
  1141. */
  1142. void ceph_queue_invalidate(struct inode *inode)
  1143. {
  1144. if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq,
  1145. &ceph_inode(inode)->i_pg_inv_work)) {
  1146. dout("ceph_queue_invalidate %p\n", inode);
  1147. igrab(inode);
  1148. } else {
  1149. dout("ceph_queue_invalidate %p failed\n", inode);
  1150. }
  1151. }
  1152. /*
  1153. * invalidate any pages that are not dirty or under writeback. this
  1154. * includes pages that are clean and mapped.
  1155. */
  1156. static void ceph_invalidate_nondirty_pages(struct address_space *mapping)
  1157. {
  1158. struct pagevec pvec;
  1159. pgoff_t next = 0;
  1160. int i;
  1161. pagevec_init(&pvec, 0);
  1162. while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  1163. for (i = 0; i < pagevec_count(&pvec); i++) {
  1164. struct page *page = pvec.pages[i];
  1165. pgoff_t index;
  1166. int skip_page =
  1167. (PageDirty(page) || PageWriteback(page));
  1168. if (!skip_page)
  1169. skip_page = !trylock_page(page);
  1170. /*
  1171. * We really shouldn't be looking at the ->index of an
  1172. * unlocked page. But we're not allowed to lock these
  1173. * pages. So we rely upon nobody altering the ->index
  1174. * of this (pinned-by-us) page.
  1175. */
  1176. index = page->index;
  1177. if (index > next)
  1178. next = index;
  1179. next++;
  1180. if (skip_page)
  1181. continue;
  1182. generic_error_remove_page(mapping, page);
  1183. unlock_page(page);
  1184. }
  1185. pagevec_release(&pvec);
  1186. cond_resched();
  1187. }
  1188. }
  1189. /*
  1190. * Invalidate inode pages in a worker thread. (This can't be done
  1191. * in the message handler context.)
  1192. */
  1193. static void ceph_invalidate_work(struct work_struct *work)
  1194. {
  1195. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1196. i_pg_inv_work);
  1197. struct inode *inode = &ci->vfs_inode;
  1198. u32 orig_gen;
  1199. int check = 0;
  1200. spin_lock(&inode->i_lock);
  1201. dout("invalidate_pages %p gen %d revoking %d\n", inode,
  1202. ci->i_rdcache_gen, ci->i_rdcache_revoking);
  1203. if (ci->i_rdcache_gen == 0 ||
  1204. ci->i_rdcache_revoking != ci->i_rdcache_gen) {
  1205. BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen);
  1206. /* nevermind! */
  1207. ci->i_rdcache_revoking = 0;
  1208. spin_unlock(&inode->i_lock);
  1209. goto out;
  1210. }
  1211. orig_gen = ci->i_rdcache_gen;
  1212. spin_unlock(&inode->i_lock);
  1213. ceph_invalidate_nondirty_pages(inode->i_mapping);
  1214. spin_lock(&inode->i_lock);
  1215. if (orig_gen == ci->i_rdcache_gen) {
  1216. dout("invalidate_pages %p gen %d successful\n", inode,
  1217. ci->i_rdcache_gen);
  1218. ci->i_rdcache_gen = 0;
  1219. ci->i_rdcache_revoking = 0;
  1220. check = 1;
  1221. } else {
  1222. dout("invalidate_pages %p gen %d raced, gen now %d\n",
  1223. inode, orig_gen, ci->i_rdcache_gen);
  1224. }
  1225. spin_unlock(&inode->i_lock);
  1226. if (check)
  1227. ceph_check_caps(ci, 0, NULL);
  1228. out:
  1229. iput(inode);
  1230. }
  1231. /*
  1232. * called by trunc_wq; take i_mutex ourselves
  1233. *
  1234. * We also truncate in a separate thread as well.
  1235. */
  1236. static void ceph_vmtruncate_work(struct work_struct *work)
  1237. {
  1238. struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
  1239. i_vmtruncate_work);
  1240. struct inode *inode = &ci->vfs_inode;
  1241. dout("vmtruncate_work %p\n", inode);
  1242. mutex_lock(&inode->i_mutex);
  1243. __ceph_do_pending_vmtruncate(inode);
  1244. mutex_unlock(&inode->i_mutex);
  1245. iput(inode);
  1246. }
  1247. /*
  1248. * Queue an async vmtruncate. If we fail to queue work, we will handle
  1249. * the truncation the next time we call __ceph_do_pending_vmtruncate.
  1250. */
  1251. void ceph_queue_vmtruncate(struct inode *inode)
  1252. {
  1253. struct ceph_inode_info *ci = ceph_inode(inode);
  1254. if (queue_work(ceph_client(inode->i_sb)->trunc_wq,
  1255. &ci->i_vmtruncate_work)) {
  1256. dout("ceph_queue_vmtruncate %p\n", inode);
  1257. igrab(inode);
  1258. } else {
  1259. dout("ceph_queue_vmtruncate %p failed, pending=%d\n",
  1260. inode, ci->i_truncate_pending);
  1261. }
  1262. }
  1263. /*
  1264. * called with i_mutex held.
  1265. *
  1266. * Make sure any pending truncation is applied before doing anything
  1267. * that may depend on it.
  1268. */
  1269. void __ceph_do_pending_vmtruncate(struct inode *inode)
  1270. {
  1271. struct ceph_inode_info *ci = ceph_inode(inode);
  1272. u64 to;
  1273. int wrbuffer_refs, wake = 0;
  1274. retry:
  1275. spin_lock(&inode->i_lock);
  1276. if (ci->i_truncate_pending == 0) {
  1277. dout("__do_pending_vmtruncate %p none pending\n", inode);
  1278. spin_unlock(&inode->i_lock);
  1279. return;
  1280. }
  1281. /*
  1282. * make sure any dirty snapped pages are flushed before we
  1283. * possibly truncate them.. so write AND block!
  1284. */
  1285. if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
  1286. dout("__do_pending_vmtruncate %p flushing snaps first\n",
  1287. inode);
  1288. spin_unlock(&inode->i_lock);
  1289. filemap_write_and_wait_range(&inode->i_data, 0,
  1290. inode->i_sb->s_maxbytes);
  1291. goto retry;
  1292. }
  1293. to = ci->i_truncate_size;
  1294. wrbuffer_refs = ci->i_wrbuffer_ref;
  1295. dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode,
  1296. ci->i_truncate_pending, to);
  1297. spin_unlock(&inode->i_lock);
  1298. truncate_inode_pages(inode->i_mapping, to);
  1299. spin_lock(&inode->i_lock);
  1300. ci->i_truncate_pending--;
  1301. if (ci->i_truncate_pending == 0)
  1302. wake = 1;
  1303. spin_unlock(&inode->i_lock);
  1304. if (wrbuffer_refs == 0)
  1305. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  1306. if (wake)
  1307. wake_up(&ci->i_cap_wq);
  1308. }
  1309. /*
  1310. * symlinks
  1311. */
  1312. static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd)
  1313. {
  1314. struct ceph_inode_info *ci = ceph_inode(dentry->d_inode);
  1315. nd_set_link(nd, ci->i_symlink);
  1316. return NULL;
  1317. }
  1318. static const struct inode_operations ceph_symlink_iops = {
  1319. .readlink = generic_readlink,
  1320. .follow_link = ceph_sym_follow_link,
  1321. };
  1322. /*
  1323. * setattr
  1324. */
  1325. int ceph_setattr(struct dentry *dentry, struct iattr *attr)
  1326. {
  1327. struct inode *inode = dentry->d_inode;
  1328. struct ceph_inode_info *ci = ceph_inode(inode);
  1329. struct inode *parent_inode = dentry->d_parent->d_inode;
  1330. const unsigned int ia_valid = attr->ia_valid;
  1331. struct ceph_mds_request *req;
  1332. struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc;
  1333. int issued;
  1334. int release = 0, dirtied = 0;
  1335. int mask = 0;
  1336. int err = 0;
  1337. if (ceph_snap(inode) != CEPH_NOSNAP)
  1338. return -EROFS;
  1339. __ceph_do_pending_vmtruncate(inode);
  1340. err = inode_change_ok(inode, attr);
  1341. if (err != 0)
  1342. return err;
  1343. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
  1344. USE_AUTH_MDS);
  1345. if (IS_ERR(req))
  1346. return PTR_ERR(req);
  1347. spin_lock(&inode->i_lock);
  1348. issued = __ceph_caps_issued(ci, NULL);
  1349. dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
  1350. if (ia_valid & ATTR_UID) {
  1351. dout("setattr %p uid %d -> %d\n", inode,
  1352. inode->i_uid, attr->ia_uid);
  1353. if (issued & CEPH_CAP_AUTH_EXCL) {
  1354. inode->i_uid = attr->ia_uid;
  1355. dirtied |= CEPH_CAP_AUTH_EXCL;
  1356. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1357. attr->ia_uid != inode->i_uid) {
  1358. req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid);
  1359. mask |= CEPH_SETATTR_UID;
  1360. release |= CEPH_CAP_AUTH_SHARED;
  1361. }
  1362. }
  1363. if (ia_valid & ATTR_GID) {
  1364. dout("setattr %p gid %d -> %d\n", inode,
  1365. inode->i_gid, attr->ia_gid);
  1366. if (issued & CEPH_CAP_AUTH_EXCL) {
  1367. inode->i_gid = attr->ia_gid;
  1368. dirtied |= CEPH_CAP_AUTH_EXCL;
  1369. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1370. attr->ia_gid != inode->i_gid) {
  1371. req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid);
  1372. mask |= CEPH_SETATTR_GID;
  1373. release |= CEPH_CAP_AUTH_SHARED;
  1374. }
  1375. }
  1376. if (ia_valid & ATTR_MODE) {
  1377. dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
  1378. attr->ia_mode);
  1379. if (issued & CEPH_CAP_AUTH_EXCL) {
  1380. inode->i_mode = attr->ia_mode;
  1381. dirtied |= CEPH_CAP_AUTH_EXCL;
  1382. } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
  1383. attr->ia_mode != inode->i_mode) {
  1384. req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
  1385. mask |= CEPH_SETATTR_MODE;
  1386. release |= CEPH_CAP_AUTH_SHARED;
  1387. }
  1388. }
  1389. if (ia_valid & ATTR_ATIME) {
  1390. dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode,
  1391. inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
  1392. attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
  1393. if (issued & CEPH_CAP_FILE_EXCL) {
  1394. ci->i_time_warp_seq++;
  1395. inode->i_atime = attr->ia_atime;
  1396. dirtied |= CEPH_CAP_FILE_EXCL;
  1397. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1398. timespec_compare(&inode->i_atime,
  1399. &attr->ia_atime) < 0) {
  1400. inode->i_atime = attr->ia_atime;
  1401. dirtied |= CEPH_CAP_FILE_WR;
  1402. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1403. !timespec_equal(&inode->i_atime, &attr->ia_atime)) {
  1404. ceph_encode_timespec(&req->r_args.setattr.atime,
  1405. &attr->ia_atime);
  1406. mask |= CEPH_SETATTR_ATIME;
  1407. release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD |
  1408. CEPH_CAP_FILE_WR;
  1409. }
  1410. }
  1411. if (ia_valid & ATTR_MTIME) {
  1412. dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode,
  1413. inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
  1414. attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
  1415. if (issued & CEPH_CAP_FILE_EXCL) {
  1416. ci->i_time_warp_seq++;
  1417. inode->i_mtime = attr->ia_mtime;
  1418. dirtied |= CEPH_CAP_FILE_EXCL;
  1419. } else if ((issued & CEPH_CAP_FILE_WR) &&
  1420. timespec_compare(&inode->i_mtime,
  1421. &attr->ia_mtime) < 0) {
  1422. inode->i_mtime = attr->ia_mtime;
  1423. dirtied |= CEPH_CAP_FILE_WR;
  1424. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1425. !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) {
  1426. ceph_encode_timespec(&req->r_args.setattr.mtime,
  1427. &attr->ia_mtime);
  1428. mask |= CEPH_SETATTR_MTIME;
  1429. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1430. CEPH_CAP_FILE_WR;
  1431. }
  1432. }
  1433. if (ia_valid & ATTR_SIZE) {
  1434. dout("setattr %p size %lld -> %lld\n", inode,
  1435. inode->i_size, attr->ia_size);
  1436. if (attr->ia_size > inode->i_sb->s_maxbytes) {
  1437. err = -EINVAL;
  1438. goto out;
  1439. }
  1440. if ((issued & CEPH_CAP_FILE_EXCL) &&
  1441. attr->ia_size > inode->i_size) {
  1442. inode->i_size = attr->ia_size;
  1443. inode->i_blocks =
  1444. (attr->ia_size + (1 << 9) - 1) >> 9;
  1445. inode->i_ctime = attr->ia_ctime;
  1446. ci->i_reported_size = attr->ia_size;
  1447. dirtied |= CEPH_CAP_FILE_EXCL;
  1448. } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
  1449. attr->ia_size != inode->i_size) {
  1450. req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
  1451. req->r_args.setattr.old_size =
  1452. cpu_to_le64(inode->i_size);
  1453. mask |= CEPH_SETATTR_SIZE;
  1454. release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD |
  1455. CEPH_CAP_FILE_WR;
  1456. }
  1457. }
  1458. /* these do nothing */
  1459. if (ia_valid & ATTR_CTIME) {
  1460. bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
  1461. ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
  1462. dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode,
  1463. inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec,
  1464. attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
  1465. only ? "ctime only" : "ignored");
  1466. inode->i_ctime = attr->ia_ctime;
  1467. if (only) {
  1468. /*
  1469. * if kernel wants to dirty ctime but nothing else,
  1470. * we need to choose a cap to dirty under, or do
  1471. * a almost-no-op setattr
  1472. */
  1473. if (issued & CEPH_CAP_AUTH_EXCL)
  1474. dirtied |= CEPH_CAP_AUTH_EXCL;
  1475. else if (issued & CEPH_CAP_FILE_EXCL)
  1476. dirtied |= CEPH_CAP_FILE_EXCL;
  1477. else if (issued & CEPH_CAP_XATTR_EXCL)
  1478. dirtied |= CEPH_CAP_XATTR_EXCL;
  1479. else
  1480. mask |= CEPH_SETATTR_CTIME;
  1481. }
  1482. }
  1483. if (ia_valid & ATTR_FILE)
  1484. dout("setattr %p ATTR_FILE ... hrm!\n", inode);
  1485. if (dirtied) {
  1486. __ceph_mark_dirty_caps(ci, dirtied);
  1487. inode->i_ctime = CURRENT_TIME;
  1488. }
  1489. release &= issued;
  1490. spin_unlock(&inode->i_lock);
  1491. if (mask) {
  1492. req->r_inode = igrab(inode);
  1493. req->r_inode_drop = release;
  1494. req->r_args.setattr.mask = cpu_to_le32(mask);
  1495. req->r_num_caps = 1;
  1496. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  1497. }
  1498. dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
  1499. ceph_cap_string(dirtied), mask);
  1500. ceph_mdsc_put_request(req);
  1501. __ceph_do_pending_vmtruncate(inode);
  1502. return err;
  1503. out:
  1504. spin_unlock(&inode->i_lock);
  1505. ceph_mdsc_put_request(req);
  1506. return err;
  1507. }
  1508. /*
  1509. * Verify that we have a lease on the given mask. If not,
  1510. * do a getattr against an mds.
  1511. */
  1512. int ceph_do_getattr(struct inode *inode, int mask)
  1513. {
  1514. struct ceph_client *client = ceph_sb_to_client(inode->i_sb);
  1515. struct ceph_mds_client *mdsc = &client->mdsc;
  1516. struct ceph_mds_request *req;
  1517. int err;
  1518. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  1519. dout("do_getattr inode %p SNAPDIR\n", inode);
  1520. return 0;
  1521. }
  1522. dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask));
  1523. if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1))
  1524. return 0;
  1525. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  1526. if (IS_ERR(req))
  1527. return PTR_ERR(req);
  1528. req->r_inode = igrab(inode);
  1529. req->r_num_caps = 1;
  1530. req->r_args.getattr.mask = cpu_to_le32(mask);
  1531. err = ceph_mdsc_do_request(mdsc, NULL, req);
  1532. ceph_mdsc_put_request(req);
  1533. dout("do_getattr result=%d\n", err);
  1534. return err;
  1535. }
  1536. /*
  1537. * Check inode permissions. We verify we have a valid value for
  1538. * the AUTH cap, then call the generic handler.
  1539. */
  1540. int ceph_permission(struct inode *inode, int mask)
  1541. {
  1542. int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED);
  1543. if (!err)
  1544. err = generic_permission(inode, mask, NULL);
  1545. return err;
  1546. }
  1547. /*
  1548. * Get all attributes. Hopefully somedata we'll have a statlite()
  1549. * and can limit the fields we require to be accurate.
  1550. */
  1551. int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1552. struct kstat *stat)
  1553. {
  1554. struct inode *inode = dentry->d_inode;
  1555. struct ceph_inode_info *ci = ceph_inode(inode);
  1556. int err;
  1557. err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL);
  1558. if (!err) {
  1559. generic_fillattr(inode, stat);
  1560. stat->ino = inode->i_ino;
  1561. if (ceph_snap(inode) != CEPH_NOSNAP)
  1562. stat->dev = ceph_snap(inode);
  1563. else
  1564. stat->dev = 0;
  1565. if (S_ISDIR(inode->i_mode)) {
  1566. stat->size = ci->i_rbytes;
  1567. stat->blocks = 0;
  1568. stat->blksize = 65536;
  1569. }
  1570. }
  1571. return err;
  1572. }