inode.c 53 KB

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