inode.c 49 KB

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