dir.c 54 KB

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