dir.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* dir.c: AFS filesystem directory handling
  3. *
  4. * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/fs.h>
  9. #include <linux/namei.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/swap.h>
  12. #include <linux/ctype.h>
  13. #include <linux/sched.h>
  14. #include <linux/task_io_accounting_ops.h>
  15. #include "internal.h"
  16. #include "afs_fs.h"
  17. #include "xdr_fs.h"
  18. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  19. unsigned int flags);
  20. static int afs_dir_open(struct inode *inode, struct file *file);
  21. static int afs_readdir(struct file *file, struct dir_context *ctx);
  22. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
  23. static int afs_d_delete(const struct dentry *dentry);
  24. static void afs_d_iput(struct dentry *dentry, struct inode *inode);
  25. static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
  26. loff_t fpos, u64 ino, unsigned dtype);
  27. static bool afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
  28. loff_t fpos, u64 ino, unsigned dtype);
  29. static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
  30. struct dentry *dentry, umode_t mode, bool excl);
  31. static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
  32. struct dentry *dentry, umode_t mode);
  33. static int afs_rmdir(struct inode *dir, struct dentry *dentry);
  34. static int afs_unlink(struct inode *dir, struct dentry *dentry);
  35. static int afs_link(struct dentry *from, struct inode *dir,
  36. struct dentry *dentry);
  37. static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
  38. struct dentry *dentry, const char *content);
  39. static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
  40. struct dentry *old_dentry, struct inode *new_dir,
  41. struct dentry *new_dentry, unsigned int flags);
  42. static bool afs_dir_release_folio(struct folio *folio, gfp_t gfp_flags);
  43. static void afs_dir_invalidate_folio(struct folio *folio, size_t offset,
  44. size_t length);
  45. static bool afs_dir_dirty_folio(struct address_space *mapping,
  46. struct folio *folio)
  47. {
  48. BUG(); /* This should never happen. */
  49. }
  50. const struct file_operations afs_dir_file_operations = {
  51. .open = afs_dir_open,
  52. .release = afs_release,
  53. .iterate_shared = afs_readdir,
  54. .lock = afs_lock,
  55. .llseek = generic_file_llseek,
  56. };
  57. const struct inode_operations afs_dir_inode_operations = {
  58. .create = afs_create,
  59. .lookup = afs_lookup,
  60. .link = afs_link,
  61. .unlink = afs_unlink,
  62. .symlink = afs_symlink,
  63. .mkdir = afs_mkdir,
  64. .rmdir = afs_rmdir,
  65. .rename = afs_rename,
  66. .permission = afs_permission,
  67. .getattr = afs_getattr,
  68. .setattr = afs_setattr,
  69. };
  70. const struct address_space_operations afs_dir_aops = {
  71. .dirty_folio = afs_dir_dirty_folio,
  72. .release_folio = afs_dir_release_folio,
  73. .invalidate_folio = afs_dir_invalidate_folio,
  74. };
  75. const struct dentry_operations afs_fs_dentry_operations = {
  76. .d_revalidate = afs_d_revalidate,
  77. .d_delete = afs_d_delete,
  78. .d_release = afs_d_release,
  79. .d_automount = afs_d_automount,
  80. .d_iput = afs_d_iput,
  81. };
  82. struct afs_lookup_one_cookie {
  83. struct dir_context ctx;
  84. struct qstr name;
  85. bool found;
  86. struct afs_fid fid;
  87. };
  88. struct afs_lookup_cookie {
  89. struct dir_context ctx;
  90. struct qstr name;
  91. bool found;
  92. bool one_only;
  93. unsigned short nr_fids;
  94. struct afs_fid fids[50];
  95. };
  96. /*
  97. * Drop the refs that we're holding on the folios we were reading into. We've
  98. * got refs on the first nr_pages pages.
  99. */
  100. static void afs_dir_read_cleanup(struct afs_read *req)
  101. {
  102. struct address_space *mapping = req->vnode->netfs.inode.i_mapping;
  103. struct folio *folio;
  104. pgoff_t last = req->nr_pages - 1;
  105. XA_STATE(xas, &mapping->i_pages, 0);
  106. if (unlikely(!req->nr_pages))
  107. return;
  108. rcu_read_lock();
  109. xas_for_each(&xas, folio, last) {
  110. if (xas_retry(&xas, folio))
  111. continue;
  112. BUG_ON(xa_is_value(folio));
  113. ASSERTCMP(folio_file_mapping(folio), ==, mapping);
  114. folio_put(folio);
  115. }
  116. rcu_read_unlock();
  117. }
  118. /*
  119. * check that a directory folio is valid
  120. */
  121. static bool afs_dir_check_folio(struct afs_vnode *dvnode, struct folio *folio,
  122. loff_t i_size)
  123. {
  124. union afs_xdr_dir_block *block;
  125. size_t offset, size;
  126. loff_t pos;
  127. /* Determine how many magic numbers there should be in this folio, but
  128. * we must take care because the directory may change size under us.
  129. */
  130. pos = folio_pos(folio);
  131. if (i_size <= pos)
  132. goto checked;
  133. size = min_t(loff_t, folio_size(folio), i_size - pos);
  134. for (offset = 0; offset < size; offset += sizeof(*block)) {
  135. block = kmap_local_folio(folio, offset);
  136. if (block->hdr.magic != AFS_DIR_MAGIC) {
  137. printk("kAFS: %s(%lx): [%llx] bad magic %zx/%zx is %04hx\n",
  138. __func__, dvnode->netfs.inode.i_ino,
  139. pos, offset, size, ntohs(block->hdr.magic));
  140. trace_afs_dir_check_failed(dvnode, pos + offset, i_size);
  141. kunmap_local(block);
  142. trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
  143. goto error;
  144. }
  145. /* Make sure each block is NUL terminated so we can reasonably
  146. * use string functions on it. The filenames in the folio
  147. * *should* be NUL-terminated anyway.
  148. */
  149. ((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0;
  150. kunmap_local(block);
  151. }
  152. checked:
  153. afs_stat_v(dvnode, n_read_dir);
  154. return true;
  155. error:
  156. return false;
  157. }
  158. /*
  159. * Dump the contents of a directory.
  160. */
  161. static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req)
  162. {
  163. union afs_xdr_dir_block *block;
  164. struct address_space *mapping = dvnode->netfs.inode.i_mapping;
  165. struct folio *folio;
  166. pgoff_t last = req->nr_pages - 1;
  167. size_t offset, size;
  168. XA_STATE(xas, &mapping->i_pages, 0);
  169. pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n",
  170. dvnode->fid.vid, dvnode->fid.vnode,
  171. req->file_size, req->len, req->actual_len);
  172. pr_warn("DIR %llx %x %zx %zx\n",
  173. req->pos, req->nr_pages,
  174. req->iter->iov_offset, iov_iter_count(req->iter));
  175. xas_for_each(&xas, folio, last) {
  176. if (xas_retry(&xas, folio))
  177. continue;
  178. BUG_ON(folio_file_mapping(folio) != mapping);
  179. size = min_t(loff_t, folio_size(folio), req->actual_len - folio_pos(folio));
  180. for (offset = 0; offset < size; offset += sizeof(*block)) {
  181. block = kmap_local_folio(folio, offset);
  182. pr_warn("[%02lx] %32phN\n", folio_index(folio) + offset, block);
  183. kunmap_local(block);
  184. }
  185. }
  186. }
  187. /*
  188. * Check all the blocks in a directory. All the folios are held pinned.
  189. */
  190. static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req)
  191. {
  192. struct address_space *mapping = dvnode->netfs.inode.i_mapping;
  193. struct folio *folio;
  194. pgoff_t last = req->nr_pages - 1;
  195. int ret = 0;
  196. XA_STATE(xas, &mapping->i_pages, 0);
  197. if (unlikely(!req->nr_pages))
  198. return 0;
  199. rcu_read_lock();
  200. xas_for_each(&xas, folio, last) {
  201. if (xas_retry(&xas, folio))
  202. continue;
  203. BUG_ON(folio_file_mapping(folio) != mapping);
  204. if (!afs_dir_check_folio(dvnode, folio, req->actual_len)) {
  205. afs_dir_dump(dvnode, req);
  206. ret = -EIO;
  207. break;
  208. }
  209. }
  210. rcu_read_unlock();
  211. return ret;
  212. }
  213. /*
  214. * open an AFS directory file
  215. */
  216. static int afs_dir_open(struct inode *inode, struct file *file)
  217. {
  218. _enter("{%lu}", inode->i_ino);
  219. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  220. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  221. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
  222. return -ENOENT;
  223. return afs_open(inode, file);
  224. }
  225. /*
  226. * Read the directory into the pagecache in one go, scrubbing the previous
  227. * contents. The list of folios is returned, pinning them so that they don't
  228. * get reclaimed during the iteration.
  229. */
  230. static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
  231. __acquires(&dvnode->validate_lock)
  232. {
  233. struct address_space *mapping = dvnode->netfs.inode.i_mapping;
  234. struct afs_read *req;
  235. loff_t i_size;
  236. int nr_pages, i;
  237. int ret;
  238. loff_t remote_size = 0;
  239. _enter("");
  240. req = kzalloc(sizeof(*req), GFP_KERNEL);
  241. if (!req)
  242. return ERR_PTR(-ENOMEM);
  243. refcount_set(&req->usage, 1);
  244. req->vnode = dvnode;
  245. req->key = key_get(key);
  246. req->cleanup = afs_dir_read_cleanup;
  247. expand:
  248. i_size = i_size_read(&dvnode->netfs.inode);
  249. if (i_size < remote_size)
  250. i_size = remote_size;
  251. if (i_size < 2048) {
  252. ret = afs_bad(dvnode, afs_file_error_dir_small);
  253. goto error;
  254. }
  255. if (i_size > 2048 * 1024) {
  256. trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
  257. ret = -EFBIG;
  258. goto error;
  259. }
  260. _enter("%llu", i_size);
  261. nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
  262. req->actual_len = i_size; /* May change */
  263. req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
  264. req->data_version = dvnode->status.data_version; /* May change */
  265. iov_iter_xarray(&req->def_iter, ITER_DEST, &dvnode->netfs.inode.i_mapping->i_pages,
  266. 0, i_size);
  267. req->iter = &req->def_iter;
  268. /* Fill in any gaps that we might find where the memory reclaimer has
  269. * been at work and pin all the folios. If there are any gaps, we will
  270. * need to reread the entire directory contents.
  271. */
  272. i = req->nr_pages;
  273. while (i < nr_pages) {
  274. struct folio *folio;
  275. folio = filemap_get_folio(mapping, i);
  276. if (!folio) {
  277. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  278. afs_stat_v(dvnode, n_inval);
  279. ret = -ENOMEM;
  280. folio = __filemap_get_folio(mapping,
  281. i, FGP_LOCK | FGP_CREAT,
  282. mapping->gfp_mask);
  283. if (!folio)
  284. goto error;
  285. folio_attach_private(folio, (void *)1);
  286. folio_unlock(folio);
  287. }
  288. req->nr_pages += folio_nr_pages(folio);
  289. i += folio_nr_pages(folio);
  290. }
  291. /* If we're going to reload, we need to lock all the pages to prevent
  292. * races.
  293. */
  294. ret = -ERESTARTSYS;
  295. if (down_read_killable(&dvnode->validate_lock) < 0)
  296. goto error;
  297. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  298. goto success;
  299. up_read(&dvnode->validate_lock);
  300. if (down_write_killable(&dvnode->validate_lock) < 0)
  301. goto error;
  302. if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
  303. trace_afs_reload_dir(dvnode);
  304. ret = afs_fetch_data(dvnode, req);
  305. if (ret < 0)
  306. goto error_unlock;
  307. task_io_account_read(PAGE_SIZE * req->nr_pages);
  308. if (req->len < req->file_size) {
  309. /* The content has grown, so we need to expand the
  310. * buffer.
  311. */
  312. up_write(&dvnode->validate_lock);
  313. remote_size = req->file_size;
  314. goto expand;
  315. }
  316. /* Validate the data we just read. */
  317. ret = afs_dir_check(dvnode, req);
  318. if (ret < 0)
  319. goto error_unlock;
  320. // TODO: Trim excess pages
  321. set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
  322. }
  323. downgrade_write(&dvnode->validate_lock);
  324. success:
  325. return req;
  326. error_unlock:
  327. up_write(&dvnode->validate_lock);
  328. error:
  329. afs_put_read(req);
  330. _leave(" = %d", ret);
  331. return ERR_PTR(ret);
  332. }
  333. /*
  334. * deal with one block in an AFS directory
  335. */
  336. static int afs_dir_iterate_block(struct afs_vnode *dvnode,
  337. struct dir_context *ctx,
  338. union afs_xdr_dir_block *block,
  339. unsigned blkoff)
  340. {
  341. union afs_xdr_dirent *dire;
  342. unsigned offset, next, curr, nr_slots;
  343. size_t nlen;
  344. int tmp;
  345. _enter("%llx,%x", ctx->pos, blkoff);
  346. curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
  347. /* walk through the block, an entry at a time */
  348. for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  349. offset < AFS_DIR_SLOTS_PER_BLOCK;
  350. offset = next
  351. ) {
  352. /* skip entries marked unused in the bitmap */
  353. if (!(block->hdr.bitmap[offset / 8] &
  354. (1 << (offset % 8)))) {
  355. _debug("ENT[%zu.%u]: unused",
  356. blkoff / sizeof(union afs_xdr_dir_block), offset);
  357. next = offset + 1;
  358. if (offset >= curr)
  359. ctx->pos = blkoff +
  360. next * sizeof(union afs_xdr_dirent);
  361. continue;
  362. }
  363. /* got a valid entry */
  364. dire = &block->dirents[offset];
  365. nlen = strnlen(dire->u.name,
  366. sizeof(*block) -
  367. offset * sizeof(union afs_xdr_dirent));
  368. if (nlen > AFSNAMEMAX - 1) {
  369. _debug("ENT[%zu]: name too long (len %u/%zu)",
  370. blkoff / sizeof(union afs_xdr_dir_block),
  371. offset, nlen);
  372. return afs_bad(dvnode, afs_file_error_dir_name_too_long);
  373. }
  374. _debug("ENT[%zu.%u]: %s %zu \"%s\"",
  375. blkoff / sizeof(union afs_xdr_dir_block), offset,
  376. (offset < curr ? "skip" : "fill"),
  377. nlen, dire->u.name);
  378. nr_slots = afs_dir_calc_slots(nlen);
  379. next = offset + nr_slots;
  380. if (next > AFS_DIR_SLOTS_PER_BLOCK) {
  381. _debug("ENT[%zu.%u]:"
  382. " %u extends beyond end dir block"
  383. " (len %zu)",
  384. blkoff / sizeof(union afs_xdr_dir_block),
  385. offset, next, nlen);
  386. return afs_bad(dvnode, afs_file_error_dir_over_end);
  387. }
  388. /* Check that the name-extension dirents are all allocated */
  389. for (tmp = 1; tmp < nr_slots; tmp++) {
  390. unsigned int ix = offset + tmp;
  391. if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) {
  392. _debug("ENT[%zu.u]:"
  393. " %u unmarked extension (%u/%u)",
  394. blkoff / sizeof(union afs_xdr_dir_block),
  395. offset, tmp, nr_slots);
  396. return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
  397. }
  398. }
  399. /* skip if starts before the current position */
  400. if (offset < curr) {
  401. if (next > curr)
  402. ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
  403. continue;
  404. }
  405. /* found the next entry */
  406. if (!dir_emit(ctx, dire->u.name, nlen,
  407. ntohl(dire->u.vnode),
  408. (ctx->actor == afs_lookup_filldir ||
  409. ctx->actor == afs_lookup_one_filldir)?
  410. ntohl(dire->u.unique) : DT_UNKNOWN)) {
  411. _leave(" = 0 [full]");
  412. return 0;
  413. }
  414. ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
  415. }
  416. _leave(" = 1 [more]");
  417. return 1;
  418. }
  419. /*
  420. * iterate through the data blob that lists the contents of an AFS directory
  421. */
  422. static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
  423. struct key *key, afs_dataversion_t *_dir_version)
  424. {
  425. struct afs_vnode *dvnode = AFS_FS_I(dir);
  426. union afs_xdr_dir_block *dblock;
  427. struct afs_read *req;
  428. struct folio *folio;
  429. unsigned offset, size;
  430. int ret;
  431. _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
  432. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  433. _leave(" = -ESTALE");
  434. return -ESTALE;
  435. }
  436. req = afs_read_dir(dvnode, key);
  437. if (IS_ERR(req))
  438. return PTR_ERR(req);
  439. *_dir_version = req->data_version;
  440. /* round the file position up to the next entry boundary */
  441. ctx->pos += sizeof(union afs_xdr_dirent) - 1;
  442. ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
  443. /* walk through the blocks in sequence */
  444. ret = 0;
  445. while (ctx->pos < req->actual_len) {
  446. /* Fetch the appropriate folio from the directory and re-add it
  447. * to the LRU. We have all the pages pinned with an extra ref.
  448. */
  449. folio = __filemap_get_folio(dir->i_mapping, ctx->pos / PAGE_SIZE,
  450. FGP_ACCESSED, 0);
  451. if (!folio) {
  452. ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
  453. break;
  454. }
  455. offset = round_down(ctx->pos, sizeof(*dblock)) - folio_file_pos(folio);
  456. size = min_t(loff_t, folio_size(folio),
  457. req->actual_len - folio_file_pos(folio));
  458. do {
  459. dblock = kmap_local_folio(folio, offset);
  460. ret = afs_dir_iterate_block(dvnode, ctx, dblock,
  461. folio_file_pos(folio) + offset);
  462. kunmap_local(dblock);
  463. if (ret != 1)
  464. goto out;
  465. } while (offset += sizeof(*dblock), offset < size);
  466. ret = 0;
  467. }
  468. out:
  469. up_read(&dvnode->validate_lock);
  470. afs_put_read(req);
  471. _leave(" = %d", ret);
  472. return ret;
  473. }
  474. /*
  475. * read an AFS directory
  476. */
  477. static int afs_readdir(struct file *file, struct dir_context *ctx)
  478. {
  479. afs_dataversion_t dir_version;
  480. return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
  481. &dir_version);
  482. }
  483. /*
  484. * Search the directory for a single name
  485. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  486. * uniquifier through dtype
  487. */
  488. static bool afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
  489. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  490. {
  491. struct afs_lookup_one_cookie *cookie =
  492. container_of(ctx, struct afs_lookup_one_cookie, ctx);
  493. _enter("{%s,%u},%s,%u,,%llu,%u",
  494. cookie->name.name, cookie->name.len, name, nlen,
  495. (unsigned long long) ino, dtype);
  496. /* insanity checks first */
  497. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  498. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  499. if (cookie->name.len != nlen ||
  500. memcmp(cookie->name.name, name, nlen) != 0) {
  501. _leave(" = true [keep looking]");
  502. return true;
  503. }
  504. cookie->fid.vnode = ino;
  505. cookie->fid.unique = dtype;
  506. cookie->found = 1;
  507. _leave(" = false [found]");
  508. return false;
  509. }
  510. /*
  511. * Do a lookup of a single name in a directory
  512. * - just returns the FID the dentry name maps to if found
  513. */
  514. static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
  515. struct afs_fid *fid, struct key *key,
  516. afs_dataversion_t *_dir_version)
  517. {
  518. struct afs_super_info *as = dir->i_sb->s_fs_info;
  519. struct afs_lookup_one_cookie cookie = {
  520. .ctx.actor = afs_lookup_one_filldir,
  521. .name = dentry->d_name,
  522. .fid.vid = as->volume->vid
  523. };
  524. int ret;
  525. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  526. /* search the directory */
  527. ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
  528. if (ret < 0) {
  529. _leave(" = %d [iter]", ret);
  530. return ret;
  531. }
  532. if (!cookie.found) {
  533. _leave(" = -ENOENT [not found]");
  534. return -ENOENT;
  535. }
  536. *fid = cookie.fid;
  537. _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
  538. return 0;
  539. }
  540. /*
  541. * search the directory for a name
  542. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  543. * uniquifier through dtype
  544. */
  545. static bool afs_lookup_filldir(struct dir_context *ctx, const char *name,
  546. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  547. {
  548. struct afs_lookup_cookie *cookie =
  549. container_of(ctx, struct afs_lookup_cookie, ctx);
  550. _enter("{%s,%u},%s,%u,,%llu,%u",
  551. cookie->name.name, cookie->name.len, name, nlen,
  552. (unsigned long long) ino, dtype);
  553. /* insanity checks first */
  554. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  555. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  556. if (cookie->found) {
  557. if (cookie->nr_fids < 50) {
  558. cookie->fids[cookie->nr_fids].vnode = ino;
  559. cookie->fids[cookie->nr_fids].unique = dtype;
  560. cookie->nr_fids++;
  561. }
  562. } else if (cookie->name.len == nlen &&
  563. memcmp(cookie->name.name, name, nlen) == 0) {
  564. cookie->fids[1].vnode = ino;
  565. cookie->fids[1].unique = dtype;
  566. cookie->found = 1;
  567. if (cookie->one_only)
  568. return false;
  569. }
  570. return cookie->nr_fids < 50;
  571. }
  572. /*
  573. * Deal with the result of a successful lookup operation. Turn all the files
  574. * into inodes and save the first one - which is the one we actually want.
  575. */
  576. static void afs_do_lookup_success(struct afs_operation *op)
  577. {
  578. struct afs_vnode_param *vp;
  579. struct afs_vnode *vnode;
  580. struct inode *inode;
  581. u32 abort_code;
  582. int i;
  583. _enter("");
  584. for (i = 0; i < op->nr_files; i++) {
  585. switch (i) {
  586. case 0:
  587. vp = &op->file[0];
  588. abort_code = vp->scb.status.abort_code;
  589. if (abort_code != 0) {
  590. op->ac.abort_code = abort_code;
  591. op->error = afs_abort_to_error(abort_code);
  592. }
  593. break;
  594. case 1:
  595. vp = &op->file[1];
  596. break;
  597. default:
  598. vp = &op->more_files[i - 2];
  599. break;
  600. }
  601. if (!vp->scb.have_status && !vp->scb.have_error)
  602. continue;
  603. _debug("do [%u]", i);
  604. if (vp->vnode) {
  605. if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
  606. afs_vnode_commit_status(op, vp);
  607. } else if (vp->scb.status.abort_code == 0) {
  608. inode = afs_iget(op, vp);
  609. if (!IS_ERR(inode)) {
  610. vnode = AFS_FS_I(inode);
  611. afs_cache_permit(vnode, op->key,
  612. 0 /* Assume vnode->cb_break is 0 */ +
  613. op->cb_v_break,
  614. &vp->scb);
  615. vp->vnode = vnode;
  616. vp->put_vnode = true;
  617. }
  618. } else {
  619. _debug("- abort %d %llx:%llx.%x",
  620. vp->scb.status.abort_code,
  621. vp->fid.vid, vp->fid.vnode, vp->fid.unique);
  622. }
  623. }
  624. _leave("");
  625. }
  626. static const struct afs_operation_ops afs_inline_bulk_status_operation = {
  627. .issue_afs_rpc = afs_fs_inline_bulk_status,
  628. .issue_yfs_rpc = yfs_fs_inline_bulk_status,
  629. .success = afs_do_lookup_success,
  630. };
  631. static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
  632. .issue_afs_rpc = afs_fs_fetch_status,
  633. .issue_yfs_rpc = yfs_fs_fetch_status,
  634. .success = afs_do_lookup_success,
  635. .aborted = afs_check_for_remote_deletion,
  636. };
  637. /*
  638. * See if we know that the server we expect to use doesn't support
  639. * FS.InlineBulkStatus.
  640. */
  641. static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
  642. {
  643. struct afs_server_list *slist;
  644. struct afs_volume *volume = dvnode->volume;
  645. struct afs_server *server;
  646. bool ret = true;
  647. int i;
  648. if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
  649. return true;
  650. rcu_read_lock();
  651. slist = rcu_dereference(volume->servers);
  652. for (i = 0; i < slist->nr_servers; i++) {
  653. server = slist->servers[i].server;
  654. if (server == dvnode->cb_server) {
  655. if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
  656. ret = false;
  657. break;
  658. }
  659. }
  660. rcu_read_unlock();
  661. return ret;
  662. }
  663. /*
  664. * Do a lookup in a directory. We make use of bulk lookup to query a slew of
  665. * files in one go and create inodes for them. The inode of the file we were
  666. * asked for is returned.
  667. */
  668. static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
  669. struct key *key)
  670. {
  671. struct afs_lookup_cookie *cookie;
  672. struct afs_vnode_param *vp;
  673. struct afs_operation *op;
  674. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
  675. struct inode *inode = NULL, *ti;
  676. afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
  677. long ret;
  678. int i;
  679. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  680. cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
  681. if (!cookie)
  682. return ERR_PTR(-ENOMEM);
  683. for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
  684. cookie->fids[i].vid = dvnode->fid.vid;
  685. cookie->ctx.actor = afs_lookup_filldir;
  686. cookie->name = dentry->d_name;
  687. cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want
  688. * and slot 1 for the directory */
  689. if (!afs_server_supports_ibulk(dvnode))
  690. cookie->one_only = true;
  691. /* search the directory */
  692. ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
  693. if (ret < 0)
  694. goto out;
  695. dentry->d_fsdata = (void *)(unsigned long)data_version;
  696. ret = -ENOENT;
  697. if (!cookie->found)
  698. goto out;
  699. /* Check to see if we already have an inode for the primary fid. */
  700. inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
  701. afs_ilookup5_test_by_fid, &cookie->fids[1]);
  702. if (inode)
  703. goto out; /* We do */
  704. /* Okay, we didn't find it. We need to query the server - and whilst
  705. * we're doing that, we're going to attempt to look up a bunch of other
  706. * vnodes also.
  707. */
  708. op = afs_alloc_operation(NULL, dvnode->volume);
  709. if (IS_ERR(op)) {
  710. ret = PTR_ERR(op);
  711. goto out;
  712. }
  713. afs_op_set_vnode(op, 0, dvnode);
  714. afs_op_set_fid(op, 1, &cookie->fids[1]);
  715. op->nr_files = cookie->nr_fids;
  716. _debug("nr_files %u", op->nr_files);
  717. /* Need space for examining all the selected files */
  718. op->error = -ENOMEM;
  719. if (op->nr_files > 2) {
  720. op->more_files = kvcalloc(op->nr_files - 2,
  721. sizeof(struct afs_vnode_param),
  722. GFP_KERNEL);
  723. if (!op->more_files)
  724. goto out_op;
  725. for (i = 2; i < op->nr_files; i++) {
  726. vp = &op->more_files[i - 2];
  727. vp->fid = cookie->fids[i];
  728. /* Find any inodes that already exist and get their
  729. * callback counters.
  730. */
  731. ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
  732. afs_ilookup5_test_by_fid, &vp->fid);
  733. if (!IS_ERR_OR_NULL(ti)) {
  734. vnode = AFS_FS_I(ti);
  735. vp->dv_before = vnode->status.data_version;
  736. vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
  737. vp->vnode = vnode;
  738. vp->put_vnode = true;
  739. vp->speculative = true; /* vnode not locked */
  740. }
  741. }
  742. }
  743. /* Try FS.InlineBulkStatus first. Abort codes for the individual
  744. * lookups contained therein are stored in the reply without aborting
  745. * the whole operation.
  746. */
  747. op->error = -ENOTSUPP;
  748. if (!cookie->one_only) {
  749. op->ops = &afs_inline_bulk_status_operation;
  750. afs_begin_vnode_operation(op);
  751. afs_wait_for_operation(op);
  752. }
  753. if (op->error == -ENOTSUPP) {
  754. /* We could try FS.BulkStatus next, but this aborts the entire
  755. * op if any of the lookups fails - so, for the moment, revert
  756. * to FS.FetchStatus for op->file[1].
  757. */
  758. op->fetch_status.which = 1;
  759. op->ops = &afs_lookup_fetch_status_operation;
  760. afs_begin_vnode_operation(op);
  761. afs_wait_for_operation(op);
  762. }
  763. inode = ERR_PTR(op->error);
  764. out_op:
  765. if (op->error == 0) {
  766. inode = &op->file[1].vnode->netfs.inode;
  767. op->file[1].vnode = NULL;
  768. }
  769. if (op->file[0].scb.have_status)
  770. dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
  771. else
  772. dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
  773. ret = afs_put_operation(op);
  774. out:
  775. kfree(cookie);
  776. _leave("");
  777. return inode ?: ERR_PTR(ret);
  778. }
  779. /*
  780. * Look up an entry in a directory with @sys substitution.
  781. */
  782. static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
  783. struct key *key)
  784. {
  785. struct afs_sysnames *subs;
  786. struct afs_net *net = afs_i2net(dir);
  787. struct dentry *ret;
  788. char *buf, *p, *name;
  789. int len, i;
  790. _enter("");
  791. ret = ERR_PTR(-ENOMEM);
  792. p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
  793. if (!buf)
  794. goto out_p;
  795. if (dentry->d_name.len > 4) {
  796. memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
  797. p += dentry->d_name.len - 4;
  798. }
  799. /* There is an ordered list of substitutes that we have to try. */
  800. read_lock(&net->sysnames_lock);
  801. subs = net->sysnames;
  802. refcount_inc(&subs->usage);
  803. read_unlock(&net->sysnames_lock);
  804. for (i = 0; i < subs->nr; i++) {
  805. name = subs->subs[i];
  806. len = dentry->d_name.len - 4 + strlen(name);
  807. if (len >= AFSNAMEMAX) {
  808. ret = ERR_PTR(-ENAMETOOLONG);
  809. goto out_s;
  810. }
  811. strcpy(p, name);
  812. ret = lookup_one_len(buf, dentry->d_parent, len);
  813. if (IS_ERR(ret) || d_is_positive(ret))
  814. goto out_s;
  815. dput(ret);
  816. }
  817. /* We don't want to d_add() the @sys dentry here as we don't want to
  818. * the cached dentry to hide changes to the sysnames list.
  819. */
  820. ret = NULL;
  821. out_s:
  822. afs_put_sysnames(subs);
  823. kfree(buf);
  824. out_p:
  825. key_put(key);
  826. return ret;
  827. }
  828. /*
  829. * look up an entry in a directory
  830. */
  831. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  832. unsigned int flags)
  833. {
  834. struct afs_vnode *dvnode = AFS_FS_I(dir);
  835. struct afs_fid fid = {};
  836. struct inode *inode;
  837. struct dentry *d;
  838. struct key *key;
  839. int ret;
  840. _enter("{%llx:%llu},%p{%pd},",
  841. dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
  842. ASSERTCMP(d_inode(dentry), ==, NULL);
  843. if (dentry->d_name.len >= AFSNAMEMAX) {
  844. _leave(" = -ENAMETOOLONG");
  845. return ERR_PTR(-ENAMETOOLONG);
  846. }
  847. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
  848. _leave(" = -ESTALE");
  849. return ERR_PTR(-ESTALE);
  850. }
  851. key = afs_request_key(dvnode->volume->cell);
  852. if (IS_ERR(key)) {
  853. _leave(" = %ld [key]", PTR_ERR(key));
  854. return ERR_CAST(key);
  855. }
  856. ret = afs_validate(dvnode, key);
  857. if (ret < 0) {
  858. key_put(key);
  859. _leave(" = %d [val]", ret);
  860. return ERR_PTR(ret);
  861. }
  862. if (dentry->d_name.len >= 4 &&
  863. dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
  864. dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
  865. dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
  866. dentry->d_name.name[dentry->d_name.len - 1] == 's')
  867. return afs_lookup_atsys(dir, dentry, key);
  868. afs_stat_v(dvnode, n_lookup);
  869. inode = afs_do_lookup(dir, dentry, key);
  870. key_put(key);
  871. if (inode == ERR_PTR(-ENOENT))
  872. inode = afs_try_auto_mntpt(dentry, dir);
  873. if (!IS_ERR_OR_NULL(inode))
  874. fid = AFS_FS_I(inode)->fid;
  875. _debug("splice %p", dentry->d_inode);
  876. d = d_splice_alias(inode, dentry);
  877. if (!IS_ERR_OR_NULL(d)) {
  878. d->d_fsdata = dentry->d_fsdata;
  879. trace_afs_lookup(dvnode, &d->d_name, &fid);
  880. } else {
  881. trace_afs_lookup(dvnode, &dentry->d_name, &fid);
  882. }
  883. _leave("");
  884. return d;
  885. }
  886. /*
  887. * Check the validity of a dentry under RCU conditions.
  888. */
  889. static int afs_d_revalidate_rcu(struct dentry *dentry)
  890. {
  891. struct afs_vnode *dvnode;
  892. struct dentry *parent;
  893. struct inode *dir;
  894. long dir_version, de_version;
  895. _enter("%p", dentry);
  896. /* Check the parent directory is still valid first. */
  897. parent = READ_ONCE(dentry->d_parent);
  898. dir = d_inode_rcu(parent);
  899. if (!dir)
  900. return -ECHILD;
  901. dvnode = AFS_FS_I(dir);
  902. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
  903. return -ECHILD;
  904. if (!afs_check_validity(dvnode))
  905. return -ECHILD;
  906. /* We only need to invalidate a dentry if the server's copy changed
  907. * behind our back. If we made the change, it's no problem. Note that
  908. * on a 32-bit system, we only have 32 bits in the dentry to store the
  909. * version.
  910. */
  911. dir_version = (long)READ_ONCE(dvnode->status.data_version);
  912. de_version = (long)READ_ONCE(dentry->d_fsdata);
  913. if (de_version != dir_version) {
  914. dir_version = (long)READ_ONCE(dvnode->invalid_before);
  915. if (de_version - dir_version < 0)
  916. return -ECHILD;
  917. }
  918. return 1; /* Still valid */
  919. }
  920. /*
  921. * check that a dentry lookup hit has found a valid entry
  922. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  923. * inode
  924. */
  925. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
  926. {
  927. struct afs_vnode *vnode, *dir;
  928. struct afs_fid fid;
  929. struct dentry *parent;
  930. struct inode *inode;
  931. struct key *key;
  932. afs_dataversion_t dir_version, invalid_before;
  933. long de_version;
  934. int ret;
  935. if (flags & LOOKUP_RCU)
  936. return afs_d_revalidate_rcu(dentry);
  937. if (d_really_is_positive(dentry)) {
  938. vnode = AFS_FS_I(d_inode(dentry));
  939. _enter("{v={%llx:%llu} n=%pd fl=%lx},",
  940. vnode->fid.vid, vnode->fid.vnode, dentry,
  941. vnode->flags);
  942. } else {
  943. _enter("{neg n=%pd}", dentry);
  944. }
  945. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  946. if (IS_ERR(key))
  947. key = NULL;
  948. /* Hold the parent dentry so we can peer at it */
  949. parent = dget_parent(dentry);
  950. dir = AFS_FS_I(d_inode(parent));
  951. /* validate the parent directory */
  952. afs_validate(dir, key);
  953. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  954. _debug("%pd: parent dir deleted", dentry);
  955. goto not_found;
  956. }
  957. /* We only need to invalidate a dentry if the server's copy changed
  958. * behind our back. If we made the change, it's no problem. Note that
  959. * on a 32-bit system, we only have 32 bits in the dentry to store the
  960. * version.
  961. */
  962. dir_version = dir->status.data_version;
  963. de_version = (long)dentry->d_fsdata;
  964. if (de_version == (long)dir_version)
  965. goto out_valid_noupdate;
  966. invalid_before = dir->invalid_before;
  967. if (de_version - (long)invalid_before >= 0)
  968. goto out_valid;
  969. _debug("dir modified");
  970. afs_stat_v(dir, n_reval);
  971. /* search the directory for this vnode */
  972. ret = afs_do_lookup_one(&dir->netfs.inode, dentry, &fid, key, &dir_version);
  973. switch (ret) {
  974. case 0:
  975. /* the filename maps to something */
  976. if (d_really_is_negative(dentry))
  977. goto not_found;
  978. inode = d_inode(dentry);
  979. if (is_bad_inode(inode)) {
  980. printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
  981. dentry);
  982. goto not_found;
  983. }
  984. vnode = AFS_FS_I(inode);
  985. /* if the vnode ID has changed, then the dirent points to a
  986. * different file */
  987. if (fid.vnode != vnode->fid.vnode) {
  988. _debug("%pd: dirent changed [%llu != %llu]",
  989. dentry, fid.vnode,
  990. vnode->fid.vnode);
  991. goto not_found;
  992. }
  993. /* if the vnode ID uniqifier has changed, then the file has
  994. * been deleted and replaced, and the original vnode ID has
  995. * been reused */
  996. if (fid.unique != vnode->fid.unique) {
  997. _debug("%pd: file deleted (uq %u -> %u I:%u)",
  998. dentry, fid.unique,
  999. vnode->fid.unique,
  1000. vnode->netfs.inode.i_generation);
  1001. goto not_found;
  1002. }
  1003. goto out_valid;
  1004. case -ENOENT:
  1005. /* the filename is unknown */
  1006. _debug("%pd: dirent not found", dentry);
  1007. if (d_really_is_positive(dentry))
  1008. goto not_found;
  1009. goto out_valid;
  1010. default:
  1011. _debug("failed to iterate dir %pd: %d",
  1012. parent, ret);
  1013. goto not_found;
  1014. }
  1015. out_valid:
  1016. dentry->d_fsdata = (void *)(unsigned long)dir_version;
  1017. out_valid_noupdate:
  1018. dput(parent);
  1019. key_put(key);
  1020. _leave(" = 1 [valid]");
  1021. return 1;
  1022. not_found:
  1023. _debug("dropping dentry %pd2", dentry);
  1024. dput(parent);
  1025. key_put(key);
  1026. _leave(" = 0 [bad]");
  1027. return 0;
  1028. }
  1029. /*
  1030. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  1031. * sleep)
  1032. * - called from dput() when d_count is going to 0.
  1033. * - return 1 to request dentry be unhashed, 0 otherwise
  1034. */
  1035. static int afs_d_delete(const struct dentry *dentry)
  1036. {
  1037. _enter("%pd", dentry);
  1038. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1039. goto zap;
  1040. if (d_really_is_positive(dentry) &&
  1041. (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
  1042. test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
  1043. goto zap;
  1044. _leave(" = 0 [keep]");
  1045. return 0;
  1046. zap:
  1047. _leave(" = 1 [zap]");
  1048. return 1;
  1049. }
  1050. /*
  1051. * Clean up sillyrename files on dentry removal.
  1052. */
  1053. static void afs_d_iput(struct dentry *dentry, struct inode *inode)
  1054. {
  1055. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1056. afs_silly_iput(dentry, inode);
  1057. iput(inode);
  1058. }
  1059. /*
  1060. * handle dentry release
  1061. */
  1062. void afs_d_release(struct dentry *dentry)
  1063. {
  1064. _enter("%pd", dentry);
  1065. }
  1066. void afs_check_for_remote_deletion(struct afs_operation *op)
  1067. {
  1068. struct afs_vnode *vnode = op->file[0].vnode;
  1069. switch (op->ac.abort_code) {
  1070. case VNOVNODE:
  1071. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1072. afs_break_callback(vnode, afs_cb_break_for_deleted);
  1073. }
  1074. }
  1075. /*
  1076. * Create a new inode for create/mkdir/symlink
  1077. */
  1078. static void afs_vnode_new_inode(struct afs_operation *op)
  1079. {
  1080. struct afs_vnode_param *vp = &op->file[1];
  1081. struct afs_vnode *vnode;
  1082. struct inode *inode;
  1083. _enter("");
  1084. ASSERTCMP(op->error, ==, 0);
  1085. inode = afs_iget(op, vp);
  1086. if (IS_ERR(inode)) {
  1087. /* ENOMEM or EINTR at a really inconvenient time - just abandon
  1088. * the new directory on the server.
  1089. */
  1090. op->error = PTR_ERR(inode);
  1091. return;
  1092. }
  1093. vnode = AFS_FS_I(inode);
  1094. set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  1095. if (!op->error)
  1096. afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
  1097. d_instantiate(op->dentry, inode);
  1098. }
  1099. static void afs_create_success(struct afs_operation *op)
  1100. {
  1101. _enter("op=%08x", op->debug_id);
  1102. op->ctime = op->file[0].scb.status.mtime_client;
  1103. afs_vnode_commit_status(op, &op->file[0]);
  1104. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1105. afs_vnode_new_inode(op);
  1106. }
  1107. static void afs_create_edit_dir(struct afs_operation *op)
  1108. {
  1109. struct afs_vnode_param *dvp = &op->file[0];
  1110. struct afs_vnode_param *vp = &op->file[1];
  1111. struct afs_vnode *dvnode = dvp->vnode;
  1112. _enter("op=%08x", op->debug_id);
  1113. down_write(&dvnode->validate_lock);
  1114. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1115. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1116. afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
  1117. op->create.reason);
  1118. up_write(&dvnode->validate_lock);
  1119. }
  1120. static void afs_create_put(struct afs_operation *op)
  1121. {
  1122. _enter("op=%08x", op->debug_id);
  1123. if (op->error)
  1124. d_drop(op->dentry);
  1125. }
  1126. static const struct afs_operation_ops afs_mkdir_operation = {
  1127. .issue_afs_rpc = afs_fs_make_dir,
  1128. .issue_yfs_rpc = yfs_fs_make_dir,
  1129. .success = afs_create_success,
  1130. .aborted = afs_check_for_remote_deletion,
  1131. .edit_dir = afs_create_edit_dir,
  1132. .put = afs_create_put,
  1133. };
  1134. /*
  1135. * create a directory on an AFS filesystem
  1136. */
  1137. static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
  1138. struct dentry *dentry, umode_t mode)
  1139. {
  1140. struct afs_operation *op;
  1141. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1142. _enter("{%llx:%llu},{%pd},%ho",
  1143. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1144. op = afs_alloc_operation(NULL, dvnode->volume);
  1145. if (IS_ERR(op)) {
  1146. d_drop(dentry);
  1147. return PTR_ERR(op);
  1148. }
  1149. afs_op_set_vnode(op, 0, dvnode);
  1150. op->file[0].dv_delta = 1;
  1151. op->file[0].modification = true;
  1152. op->file[0].update_ctime = true;
  1153. op->dentry = dentry;
  1154. op->create.mode = S_IFDIR | mode;
  1155. op->create.reason = afs_edit_dir_for_mkdir;
  1156. op->mtime = current_time(dir);
  1157. op->ops = &afs_mkdir_operation;
  1158. return afs_do_sync_operation(op);
  1159. }
  1160. /*
  1161. * Remove a subdir from a directory.
  1162. */
  1163. static void afs_dir_remove_subdir(struct dentry *dentry)
  1164. {
  1165. if (d_really_is_positive(dentry)) {
  1166. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1167. clear_nlink(&vnode->netfs.inode);
  1168. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1169. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  1170. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  1171. }
  1172. }
  1173. static void afs_rmdir_success(struct afs_operation *op)
  1174. {
  1175. _enter("op=%08x", op->debug_id);
  1176. op->ctime = op->file[0].scb.status.mtime_client;
  1177. afs_vnode_commit_status(op, &op->file[0]);
  1178. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1179. }
  1180. static void afs_rmdir_edit_dir(struct afs_operation *op)
  1181. {
  1182. struct afs_vnode_param *dvp = &op->file[0];
  1183. struct afs_vnode *dvnode = dvp->vnode;
  1184. _enter("op=%08x", op->debug_id);
  1185. afs_dir_remove_subdir(op->dentry);
  1186. down_write(&dvnode->validate_lock);
  1187. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1188. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1189. afs_edit_dir_remove(dvnode, &op->dentry->d_name,
  1190. afs_edit_dir_for_rmdir);
  1191. up_write(&dvnode->validate_lock);
  1192. }
  1193. static void afs_rmdir_put(struct afs_operation *op)
  1194. {
  1195. _enter("op=%08x", op->debug_id);
  1196. if (op->file[1].vnode)
  1197. up_write(&op->file[1].vnode->rmdir_lock);
  1198. }
  1199. static const struct afs_operation_ops afs_rmdir_operation = {
  1200. .issue_afs_rpc = afs_fs_remove_dir,
  1201. .issue_yfs_rpc = yfs_fs_remove_dir,
  1202. .success = afs_rmdir_success,
  1203. .aborted = afs_check_for_remote_deletion,
  1204. .edit_dir = afs_rmdir_edit_dir,
  1205. .put = afs_rmdir_put,
  1206. };
  1207. /*
  1208. * remove a directory from an AFS filesystem
  1209. */
  1210. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  1211. {
  1212. struct afs_operation *op;
  1213. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
  1214. int ret;
  1215. _enter("{%llx:%llu},{%pd}",
  1216. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1217. op = afs_alloc_operation(NULL, dvnode->volume);
  1218. if (IS_ERR(op))
  1219. return PTR_ERR(op);
  1220. afs_op_set_vnode(op, 0, dvnode);
  1221. op->file[0].dv_delta = 1;
  1222. op->file[0].modification = true;
  1223. op->file[0].update_ctime = true;
  1224. op->dentry = dentry;
  1225. op->ops = &afs_rmdir_operation;
  1226. /* Try to make sure we have a callback promise on the victim. */
  1227. if (d_really_is_positive(dentry)) {
  1228. vnode = AFS_FS_I(d_inode(dentry));
  1229. ret = afs_validate(vnode, op->key);
  1230. if (ret < 0)
  1231. goto error;
  1232. }
  1233. if (vnode) {
  1234. ret = down_write_killable(&vnode->rmdir_lock);
  1235. if (ret < 0)
  1236. goto error;
  1237. op->file[1].vnode = vnode;
  1238. }
  1239. return afs_do_sync_operation(op);
  1240. error:
  1241. return afs_put_operation(op);
  1242. }
  1243. /*
  1244. * Remove a link to a file or symlink from a directory.
  1245. *
  1246. * If the file was not deleted due to excess hard links, the fileserver will
  1247. * break the callback promise on the file - if it had one - before it returns
  1248. * to us, and if it was deleted, it won't
  1249. *
  1250. * However, if we didn't have a callback promise outstanding, or it was
  1251. * outstanding on a different server, then it won't break it either...
  1252. */
  1253. static void afs_dir_remove_link(struct afs_operation *op)
  1254. {
  1255. struct afs_vnode *dvnode = op->file[0].vnode;
  1256. struct afs_vnode *vnode = op->file[1].vnode;
  1257. struct dentry *dentry = op->dentry;
  1258. int ret;
  1259. if (op->error != 0 ||
  1260. (op->file[1].scb.have_status && op->file[1].scb.have_error))
  1261. return;
  1262. if (d_really_is_positive(dentry))
  1263. return;
  1264. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  1265. /* Already done */
  1266. } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
  1267. write_seqlock(&vnode->cb_lock);
  1268. drop_nlink(&vnode->netfs.inode);
  1269. if (vnode->netfs.inode.i_nlink == 0) {
  1270. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1271. __afs_break_callback(vnode, afs_cb_break_for_unlink);
  1272. }
  1273. write_sequnlock(&vnode->cb_lock);
  1274. } else {
  1275. afs_break_callback(vnode, afs_cb_break_for_unlink);
  1276. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  1277. _debug("AFS_VNODE_DELETED");
  1278. ret = afs_validate(vnode, op->key);
  1279. if (ret != -ESTALE)
  1280. op->error = ret;
  1281. }
  1282. _debug("nlink %d [val %d]", vnode->netfs.inode.i_nlink, op->error);
  1283. }
  1284. static void afs_unlink_success(struct afs_operation *op)
  1285. {
  1286. _enter("op=%08x", op->debug_id);
  1287. op->ctime = op->file[0].scb.status.mtime_client;
  1288. afs_check_dir_conflict(op, &op->file[0]);
  1289. afs_vnode_commit_status(op, &op->file[0]);
  1290. afs_vnode_commit_status(op, &op->file[1]);
  1291. afs_update_dentry_version(op, &op->file[0], op->dentry);
  1292. afs_dir_remove_link(op);
  1293. }
  1294. static void afs_unlink_edit_dir(struct afs_operation *op)
  1295. {
  1296. struct afs_vnode_param *dvp = &op->file[0];
  1297. struct afs_vnode *dvnode = dvp->vnode;
  1298. _enter("op=%08x", op->debug_id);
  1299. down_write(&dvnode->validate_lock);
  1300. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
  1301. dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
  1302. afs_edit_dir_remove(dvnode, &op->dentry->d_name,
  1303. afs_edit_dir_for_unlink);
  1304. up_write(&dvnode->validate_lock);
  1305. }
  1306. static void afs_unlink_put(struct afs_operation *op)
  1307. {
  1308. _enter("op=%08x", op->debug_id);
  1309. if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT)
  1310. d_rehash(op->dentry);
  1311. }
  1312. static const struct afs_operation_ops afs_unlink_operation = {
  1313. .issue_afs_rpc = afs_fs_remove_file,
  1314. .issue_yfs_rpc = yfs_fs_remove_file,
  1315. .success = afs_unlink_success,
  1316. .aborted = afs_check_for_remote_deletion,
  1317. .edit_dir = afs_unlink_edit_dir,
  1318. .put = afs_unlink_put,
  1319. };
  1320. /*
  1321. * Remove a file or symlink from an AFS filesystem.
  1322. */
  1323. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  1324. {
  1325. struct afs_operation *op;
  1326. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1327. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1328. int ret;
  1329. _enter("{%llx:%llu},{%pd}",
  1330. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1331. if (dentry->d_name.len >= AFSNAMEMAX)
  1332. return -ENAMETOOLONG;
  1333. op = afs_alloc_operation(NULL, dvnode->volume);
  1334. if (IS_ERR(op))
  1335. return PTR_ERR(op);
  1336. afs_op_set_vnode(op, 0, dvnode);
  1337. op->file[0].dv_delta = 1;
  1338. op->file[0].modification = true;
  1339. op->file[0].update_ctime = true;
  1340. /* Try to make sure we have a callback promise on the victim. */
  1341. ret = afs_validate(vnode, op->key);
  1342. if (ret < 0) {
  1343. op->error = ret;
  1344. goto error;
  1345. }
  1346. spin_lock(&dentry->d_lock);
  1347. if (d_count(dentry) > 1) {
  1348. spin_unlock(&dentry->d_lock);
  1349. /* Start asynchronous writeout of the inode */
  1350. write_inode_now(d_inode(dentry), 0);
  1351. op->error = afs_sillyrename(dvnode, vnode, dentry, op->key);
  1352. goto error;
  1353. }
  1354. if (!d_unhashed(dentry)) {
  1355. /* Prevent a race with RCU lookup. */
  1356. __d_drop(dentry);
  1357. op->unlink.need_rehash = true;
  1358. }
  1359. spin_unlock(&dentry->d_lock);
  1360. op->file[1].vnode = vnode;
  1361. op->file[1].update_ctime = true;
  1362. op->file[1].op_unlinked = true;
  1363. op->dentry = dentry;
  1364. op->ops = &afs_unlink_operation;
  1365. afs_begin_vnode_operation(op);
  1366. afs_wait_for_operation(op);
  1367. /* If there was a conflict with a third party, check the status of the
  1368. * unlinked vnode.
  1369. */
  1370. if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
  1371. op->file[1].update_ctime = false;
  1372. op->fetch_status.which = 1;
  1373. op->ops = &afs_fetch_status_operation;
  1374. afs_begin_vnode_operation(op);
  1375. afs_wait_for_operation(op);
  1376. }
  1377. return afs_put_operation(op);
  1378. error:
  1379. return afs_put_operation(op);
  1380. }
  1381. static const struct afs_operation_ops afs_create_operation = {
  1382. .issue_afs_rpc = afs_fs_create_file,
  1383. .issue_yfs_rpc = yfs_fs_create_file,
  1384. .success = afs_create_success,
  1385. .aborted = afs_check_for_remote_deletion,
  1386. .edit_dir = afs_create_edit_dir,
  1387. .put = afs_create_put,
  1388. };
  1389. /*
  1390. * create a regular file on an AFS filesystem
  1391. */
  1392. static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
  1393. struct dentry *dentry, umode_t mode, bool excl)
  1394. {
  1395. struct afs_operation *op;
  1396. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1397. int ret = -ENAMETOOLONG;
  1398. _enter("{%llx:%llu},{%pd},%ho",
  1399. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1400. if (dentry->d_name.len >= AFSNAMEMAX)
  1401. goto error;
  1402. op = afs_alloc_operation(NULL, dvnode->volume);
  1403. if (IS_ERR(op)) {
  1404. ret = PTR_ERR(op);
  1405. goto error;
  1406. }
  1407. afs_op_set_vnode(op, 0, dvnode);
  1408. op->file[0].dv_delta = 1;
  1409. op->file[0].modification = true;
  1410. op->file[0].update_ctime = true;
  1411. op->dentry = dentry;
  1412. op->create.mode = S_IFREG | mode;
  1413. op->create.reason = afs_edit_dir_for_create;
  1414. op->mtime = current_time(dir);
  1415. op->ops = &afs_create_operation;
  1416. return afs_do_sync_operation(op);
  1417. error:
  1418. d_drop(dentry);
  1419. _leave(" = %d", ret);
  1420. return ret;
  1421. }
  1422. static void afs_link_success(struct afs_operation *op)
  1423. {
  1424. struct afs_vnode_param *dvp = &op->file[0];
  1425. struct afs_vnode_param *vp = &op->file[1];
  1426. _enter("op=%08x", op->debug_id);
  1427. op->ctime = dvp->scb.status.mtime_client;
  1428. afs_vnode_commit_status(op, dvp);
  1429. afs_vnode_commit_status(op, vp);
  1430. afs_update_dentry_version(op, dvp, op->dentry);
  1431. if (op->dentry_2->d_parent == op->dentry->d_parent)
  1432. afs_update_dentry_version(op, dvp, op->dentry_2);
  1433. ihold(&vp->vnode->netfs.inode);
  1434. d_instantiate(op->dentry, &vp->vnode->netfs.inode);
  1435. }
  1436. static void afs_link_put(struct afs_operation *op)
  1437. {
  1438. _enter("op=%08x", op->debug_id);
  1439. if (op->error)
  1440. d_drop(op->dentry);
  1441. }
  1442. static const struct afs_operation_ops afs_link_operation = {
  1443. .issue_afs_rpc = afs_fs_link,
  1444. .issue_yfs_rpc = yfs_fs_link,
  1445. .success = afs_link_success,
  1446. .aborted = afs_check_for_remote_deletion,
  1447. .edit_dir = afs_create_edit_dir,
  1448. .put = afs_link_put,
  1449. };
  1450. /*
  1451. * create a hard link between files in an AFS filesystem
  1452. */
  1453. static int afs_link(struct dentry *from, struct inode *dir,
  1454. struct dentry *dentry)
  1455. {
  1456. struct afs_operation *op;
  1457. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1458. struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
  1459. int ret = -ENAMETOOLONG;
  1460. _enter("{%llx:%llu},{%llx:%llu},{%pd}",
  1461. vnode->fid.vid, vnode->fid.vnode,
  1462. dvnode->fid.vid, dvnode->fid.vnode,
  1463. dentry);
  1464. if (dentry->d_name.len >= AFSNAMEMAX)
  1465. goto error;
  1466. op = afs_alloc_operation(NULL, dvnode->volume);
  1467. if (IS_ERR(op)) {
  1468. ret = PTR_ERR(op);
  1469. goto error;
  1470. }
  1471. ret = afs_validate(vnode, op->key);
  1472. if (ret < 0)
  1473. goto error_op;
  1474. afs_op_set_vnode(op, 0, dvnode);
  1475. afs_op_set_vnode(op, 1, vnode);
  1476. op->file[0].dv_delta = 1;
  1477. op->file[0].modification = true;
  1478. op->file[0].update_ctime = true;
  1479. op->file[1].update_ctime = true;
  1480. op->dentry = dentry;
  1481. op->dentry_2 = from;
  1482. op->ops = &afs_link_operation;
  1483. op->create.reason = afs_edit_dir_for_link;
  1484. return afs_do_sync_operation(op);
  1485. error_op:
  1486. afs_put_operation(op);
  1487. error:
  1488. d_drop(dentry);
  1489. _leave(" = %d", ret);
  1490. return ret;
  1491. }
  1492. static const struct afs_operation_ops afs_symlink_operation = {
  1493. .issue_afs_rpc = afs_fs_symlink,
  1494. .issue_yfs_rpc = yfs_fs_symlink,
  1495. .success = afs_create_success,
  1496. .aborted = afs_check_for_remote_deletion,
  1497. .edit_dir = afs_create_edit_dir,
  1498. .put = afs_create_put,
  1499. };
  1500. /*
  1501. * create a symlink in an AFS filesystem
  1502. */
  1503. static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
  1504. struct dentry *dentry, const char *content)
  1505. {
  1506. struct afs_operation *op;
  1507. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1508. int ret;
  1509. _enter("{%llx:%llu},{%pd},%s",
  1510. dvnode->fid.vid, dvnode->fid.vnode, dentry,
  1511. content);
  1512. ret = -ENAMETOOLONG;
  1513. if (dentry->d_name.len >= AFSNAMEMAX)
  1514. goto error;
  1515. ret = -EINVAL;
  1516. if (strlen(content) >= AFSPATHMAX)
  1517. goto error;
  1518. op = afs_alloc_operation(NULL, dvnode->volume);
  1519. if (IS_ERR(op)) {
  1520. ret = PTR_ERR(op);
  1521. goto error;
  1522. }
  1523. afs_op_set_vnode(op, 0, dvnode);
  1524. op->file[0].dv_delta = 1;
  1525. op->dentry = dentry;
  1526. op->ops = &afs_symlink_operation;
  1527. op->create.reason = afs_edit_dir_for_symlink;
  1528. op->create.symlink = content;
  1529. op->mtime = current_time(dir);
  1530. return afs_do_sync_operation(op);
  1531. error:
  1532. d_drop(dentry);
  1533. _leave(" = %d", ret);
  1534. return ret;
  1535. }
  1536. static void afs_rename_success(struct afs_operation *op)
  1537. {
  1538. _enter("op=%08x", op->debug_id);
  1539. op->ctime = op->file[0].scb.status.mtime_client;
  1540. afs_check_dir_conflict(op, &op->file[1]);
  1541. afs_vnode_commit_status(op, &op->file[0]);
  1542. if (op->file[1].vnode != op->file[0].vnode) {
  1543. op->ctime = op->file[1].scb.status.mtime_client;
  1544. afs_vnode_commit_status(op, &op->file[1]);
  1545. }
  1546. }
  1547. static void afs_rename_edit_dir(struct afs_operation *op)
  1548. {
  1549. struct afs_vnode_param *orig_dvp = &op->file[0];
  1550. struct afs_vnode_param *new_dvp = &op->file[1];
  1551. struct afs_vnode *orig_dvnode = orig_dvp->vnode;
  1552. struct afs_vnode *new_dvnode = new_dvp->vnode;
  1553. struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
  1554. struct dentry *old_dentry = op->dentry;
  1555. struct dentry *new_dentry = op->dentry_2;
  1556. struct inode *new_inode;
  1557. _enter("op=%08x", op->debug_id);
  1558. if (op->rename.rehash) {
  1559. d_rehash(op->rename.rehash);
  1560. op->rename.rehash = NULL;
  1561. }
  1562. down_write(&orig_dvnode->validate_lock);
  1563. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
  1564. orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
  1565. afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
  1566. afs_edit_dir_for_rename_0);
  1567. if (new_dvnode != orig_dvnode) {
  1568. up_write(&orig_dvnode->validate_lock);
  1569. down_write(&new_dvnode->validate_lock);
  1570. }
  1571. if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
  1572. new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
  1573. if (!op->rename.new_negative)
  1574. afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
  1575. afs_edit_dir_for_rename_1);
  1576. afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
  1577. &vnode->fid, afs_edit_dir_for_rename_2);
  1578. }
  1579. new_inode = d_inode(new_dentry);
  1580. if (new_inode) {
  1581. spin_lock(&new_inode->i_lock);
  1582. if (S_ISDIR(new_inode->i_mode))
  1583. clear_nlink(new_inode);
  1584. else if (new_inode->i_nlink > 0)
  1585. drop_nlink(new_inode);
  1586. spin_unlock(&new_inode->i_lock);
  1587. }
  1588. /* Now we can update d_fsdata on the dentries to reflect their
  1589. * new parent's data_version.
  1590. *
  1591. * Note that if we ever implement RENAME_EXCHANGE, we'll have
  1592. * to update both dentries with opposing dir versions.
  1593. */
  1594. afs_update_dentry_version(op, new_dvp, op->dentry);
  1595. afs_update_dentry_version(op, new_dvp, op->dentry_2);
  1596. d_move(old_dentry, new_dentry);
  1597. up_write(&new_dvnode->validate_lock);
  1598. }
  1599. static void afs_rename_put(struct afs_operation *op)
  1600. {
  1601. _enter("op=%08x", op->debug_id);
  1602. if (op->rename.rehash)
  1603. d_rehash(op->rename.rehash);
  1604. dput(op->rename.tmp);
  1605. if (op->error)
  1606. d_rehash(op->dentry);
  1607. }
  1608. static const struct afs_operation_ops afs_rename_operation = {
  1609. .issue_afs_rpc = afs_fs_rename,
  1610. .issue_yfs_rpc = yfs_fs_rename,
  1611. .success = afs_rename_success,
  1612. .edit_dir = afs_rename_edit_dir,
  1613. .put = afs_rename_put,
  1614. };
  1615. /*
  1616. * rename a file in an AFS filesystem and/or move it between directories
  1617. */
  1618. static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
  1619. struct dentry *old_dentry, struct inode *new_dir,
  1620. struct dentry *new_dentry, unsigned int flags)
  1621. {
  1622. struct afs_operation *op;
  1623. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  1624. int ret;
  1625. if (flags)
  1626. return -EINVAL;
  1627. /* Don't allow silly-rename files be moved around. */
  1628. if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1629. return -EINVAL;
  1630. vnode = AFS_FS_I(d_inode(old_dentry));
  1631. orig_dvnode = AFS_FS_I(old_dir);
  1632. new_dvnode = AFS_FS_I(new_dir);
  1633. _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
  1634. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  1635. vnode->fid.vid, vnode->fid.vnode,
  1636. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  1637. new_dentry);
  1638. op = afs_alloc_operation(NULL, orig_dvnode->volume);
  1639. if (IS_ERR(op))
  1640. return PTR_ERR(op);
  1641. ret = afs_validate(vnode, op->key);
  1642. op->error = ret;
  1643. if (ret < 0)
  1644. goto error;
  1645. afs_op_set_vnode(op, 0, orig_dvnode);
  1646. afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
  1647. op->file[0].dv_delta = 1;
  1648. op->file[1].dv_delta = 1;
  1649. op->file[0].modification = true;
  1650. op->file[1].modification = true;
  1651. op->file[0].update_ctime = true;
  1652. op->file[1].update_ctime = true;
  1653. op->dentry = old_dentry;
  1654. op->dentry_2 = new_dentry;
  1655. op->rename.new_negative = d_is_negative(new_dentry);
  1656. op->ops = &afs_rename_operation;
  1657. /* For non-directories, check whether the target is busy and if so,
  1658. * make a copy of the dentry and then do a silly-rename. If the
  1659. * silly-rename succeeds, the copied dentry is hashed and becomes the
  1660. * new target.
  1661. */
  1662. if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
  1663. /* To prevent any new references to the target during the
  1664. * rename, we unhash the dentry in advance.
  1665. */
  1666. if (!d_unhashed(new_dentry)) {
  1667. d_drop(new_dentry);
  1668. op->rename.rehash = new_dentry;
  1669. }
  1670. if (d_count(new_dentry) > 2) {
  1671. /* copy the target dentry's name */
  1672. op->rename.tmp = d_alloc(new_dentry->d_parent,
  1673. &new_dentry->d_name);
  1674. if (!op->rename.tmp) {
  1675. op->error = -ENOMEM;
  1676. goto error;
  1677. }
  1678. ret = afs_sillyrename(new_dvnode,
  1679. AFS_FS_I(d_inode(new_dentry)),
  1680. new_dentry, op->key);
  1681. if (ret) {
  1682. op->error = ret;
  1683. goto error;
  1684. }
  1685. op->dentry_2 = op->rename.tmp;
  1686. op->rename.rehash = NULL;
  1687. op->rename.new_negative = true;
  1688. }
  1689. }
  1690. /* This bit is potentially nasty as there's a potential race with
  1691. * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
  1692. * to reflect it's new parent's new data_version after the op, but
  1693. * d_revalidate may see old_dentry between the op having taken place
  1694. * and the version being updated.
  1695. *
  1696. * So drop the old_dentry for now to make other threads go through
  1697. * lookup instead - which we hold a lock against.
  1698. */
  1699. d_drop(old_dentry);
  1700. return afs_do_sync_operation(op);
  1701. error:
  1702. return afs_put_operation(op);
  1703. }
  1704. /*
  1705. * Release a directory folio and clean up its private state if it's not busy
  1706. * - return true if the folio can now be released, false if not
  1707. */
  1708. static bool afs_dir_release_folio(struct folio *folio, gfp_t gfp_flags)
  1709. {
  1710. struct afs_vnode *dvnode = AFS_FS_I(folio_inode(folio));
  1711. _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, folio_index(folio));
  1712. folio_detach_private(folio);
  1713. /* The directory will need reloading. */
  1714. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1715. afs_stat_v(dvnode, n_relpg);
  1716. return true;
  1717. }
  1718. /*
  1719. * Invalidate part or all of a folio.
  1720. */
  1721. static void afs_dir_invalidate_folio(struct folio *folio, size_t offset,
  1722. size_t length)
  1723. {
  1724. struct afs_vnode *dvnode = AFS_FS_I(folio_inode(folio));
  1725. _enter("{%lu},%zu,%zu", folio->index, offset, length);
  1726. BUG_ON(!folio_test_locked(folio));
  1727. /* The directory will need reloading. */
  1728. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1729. afs_stat_v(dvnode, n_inval);
  1730. /* we clean up only if the entire folio is being invalidated */
  1731. if (offset == 0 && length == folio_size(folio))
  1732. folio_detach_private(folio);
  1733. }