dir.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. /* dir.c: AFS filesystem directory handling
  2. *
  3. * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/swap.h>
  16. #include <linux/ctype.h>
  17. #include <linux/sched.h>
  18. #include <linux/task_io_accounting_ops.h>
  19. #include "internal.h"
  20. #include "xdr_fs.h"
  21. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  22. unsigned int flags);
  23. static int afs_dir_open(struct inode *inode, struct file *file);
  24. static int afs_readdir(struct file *file, struct dir_context *ctx);
  25. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
  26. static int afs_d_delete(const struct dentry *dentry);
  27. static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
  28. loff_t fpos, u64 ino, unsigned dtype);
  29. static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
  30. loff_t fpos, u64 ino, unsigned dtype);
  31. static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  32. bool excl);
  33. static int afs_mkdir(struct inode *dir, 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 inode *dir, struct dentry *dentry,
  39. const char *content);
  40. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  41. struct inode *new_dir, struct dentry *new_dentry,
  42. unsigned int flags);
  43. static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
  44. static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
  45. unsigned int length);
  46. static int afs_dir_set_page_dirty(struct page *page)
  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. .listxattr = afs_listxattr,
  70. };
  71. const struct address_space_operations afs_dir_aops = {
  72. .set_page_dirty = afs_dir_set_page_dirty,
  73. .releasepage = afs_dir_releasepage,
  74. .invalidatepage = afs_dir_invalidatepage,
  75. };
  76. const struct dentry_operations afs_fs_dentry_operations = {
  77. .d_revalidate = afs_d_revalidate,
  78. .d_delete = afs_d_delete,
  79. .d_release = afs_d_release,
  80. .d_automount = afs_d_automount,
  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_file_status *statuses;
  95. struct afs_callback *callbacks;
  96. struct afs_fid fids[50];
  97. };
  98. /*
  99. * check that a directory page is valid
  100. */
  101. static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
  102. loff_t i_size)
  103. {
  104. struct afs_xdr_dir_page *dbuf;
  105. loff_t latter, off;
  106. int tmp, qty;
  107. /* Determine how many magic numbers there should be in this page, but
  108. * we must take care because the directory may change size under us.
  109. */
  110. off = page_offset(page);
  111. if (i_size <= off)
  112. goto checked;
  113. latter = i_size - off;
  114. if (latter >= PAGE_SIZE)
  115. qty = PAGE_SIZE;
  116. else
  117. qty = latter;
  118. qty /= sizeof(union afs_xdr_dir_block);
  119. /* check them */
  120. dbuf = kmap(page);
  121. for (tmp = 0; tmp < qty; tmp++) {
  122. if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
  123. printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
  124. __func__, dvnode->vfs_inode.i_ino, tmp, qty,
  125. ntohs(dbuf->blocks[tmp].hdr.magic));
  126. trace_afs_dir_check_failed(dvnode, off, i_size);
  127. kunmap(page);
  128. trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
  129. goto error;
  130. }
  131. /* Make sure each block is NUL terminated so we can reasonably
  132. * use string functions on it. The filenames in the page
  133. * *should* be NUL-terminated anyway.
  134. */
  135. ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
  136. }
  137. kunmap(page);
  138. checked:
  139. afs_stat_v(dvnode, n_read_dir);
  140. return true;
  141. error:
  142. return false;
  143. }
  144. /*
  145. * open an AFS directory file
  146. */
  147. static int afs_dir_open(struct inode *inode, struct file *file)
  148. {
  149. _enter("{%lu}", inode->i_ino);
  150. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  151. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  152. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
  153. return -ENOENT;
  154. return afs_open(inode, file);
  155. }
  156. /*
  157. * Read the directory into the pagecache in one go, scrubbing the previous
  158. * contents. The list of pages is returned, pinning them so that they don't
  159. * get reclaimed during the iteration.
  160. */
  161. static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
  162. __acquires(&dvnode->validate_lock)
  163. {
  164. struct afs_read *req;
  165. loff_t i_size;
  166. int nr_pages, nr_inline, i, n;
  167. int ret = -ENOMEM;
  168. retry:
  169. i_size = i_size_read(&dvnode->vfs_inode);
  170. if (i_size < 2048)
  171. return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
  172. if (i_size > 2048 * 1024) {
  173. trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
  174. return ERR_PTR(-EFBIG);
  175. }
  176. _enter("%llu", i_size);
  177. /* Get a request record to hold the page list. We want to hold it
  178. * inline if we can, but we don't want to make an order 1 allocation.
  179. */
  180. nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
  181. nr_inline = nr_pages;
  182. if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
  183. nr_inline = 0;
  184. req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
  185. GFP_KERNEL);
  186. if (!req)
  187. return ERR_PTR(-ENOMEM);
  188. refcount_set(&req->usage, 1);
  189. req->nr_pages = nr_pages;
  190. req->actual_len = i_size; /* May change */
  191. req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
  192. req->data_version = dvnode->status.data_version; /* May change */
  193. if (nr_inline > 0) {
  194. req->pages = req->array;
  195. } else {
  196. req->pages = kcalloc(nr_pages, sizeof(struct page *),
  197. GFP_KERNEL);
  198. if (!req->pages)
  199. goto error;
  200. }
  201. /* Get a list of all the pages that hold or will hold the directory
  202. * content. We need to fill in any gaps that we might find where the
  203. * memory reclaimer has been at work. If there are any gaps, we will
  204. * need to reread the entire directory contents.
  205. */
  206. i = 0;
  207. do {
  208. n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
  209. req->nr_pages - i,
  210. req->pages + i);
  211. _debug("find %u at %u/%u", n, i, req->nr_pages);
  212. if (n == 0) {
  213. gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
  214. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  215. afs_stat_v(dvnode, n_inval);
  216. ret = -ENOMEM;
  217. req->pages[i] = __page_cache_alloc(gfp);
  218. if (!req->pages[i])
  219. goto error;
  220. ret = add_to_page_cache_lru(req->pages[i],
  221. dvnode->vfs_inode.i_mapping,
  222. i, gfp);
  223. if (ret < 0)
  224. goto error;
  225. set_page_private(req->pages[i], 1);
  226. SetPagePrivate(req->pages[i]);
  227. unlock_page(req->pages[i]);
  228. i++;
  229. } else {
  230. i += n;
  231. }
  232. } while (i < req->nr_pages);
  233. /* If we're going to reload, we need to lock all the pages to prevent
  234. * races.
  235. */
  236. ret = -ERESTARTSYS;
  237. if (down_read_killable(&dvnode->validate_lock) < 0)
  238. goto error;
  239. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  240. goto success;
  241. up_read(&dvnode->validate_lock);
  242. if (down_write_killable(&dvnode->validate_lock) < 0)
  243. goto error;
  244. if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
  245. ret = afs_fetch_data(dvnode, key, req);
  246. if (ret < 0)
  247. goto error_unlock;
  248. task_io_account_read(PAGE_SIZE * req->nr_pages);
  249. if (req->len < req->file_size)
  250. goto content_has_grown;
  251. /* Validate the data we just read. */
  252. ret = -EIO;
  253. for (i = 0; i < req->nr_pages; i++)
  254. if (!afs_dir_check_page(dvnode, req->pages[i],
  255. req->actual_len))
  256. goto error_unlock;
  257. // TODO: Trim excess pages
  258. set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
  259. }
  260. downgrade_write(&dvnode->validate_lock);
  261. success:
  262. return req;
  263. error_unlock:
  264. up_write(&dvnode->validate_lock);
  265. error:
  266. afs_put_read(req);
  267. _leave(" = %d", ret);
  268. return ERR_PTR(ret);
  269. content_has_grown:
  270. up_write(&dvnode->validate_lock);
  271. afs_put_read(req);
  272. goto retry;
  273. }
  274. /*
  275. * deal with one block in an AFS directory
  276. */
  277. static int afs_dir_iterate_block(struct afs_vnode *dvnode,
  278. struct dir_context *ctx,
  279. union afs_xdr_dir_block *block,
  280. unsigned blkoff)
  281. {
  282. union afs_xdr_dirent *dire;
  283. unsigned offset, next, curr;
  284. size_t nlen;
  285. int tmp;
  286. _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
  287. curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
  288. /* walk through the block, an entry at a time */
  289. for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  290. offset < AFS_DIR_SLOTS_PER_BLOCK;
  291. offset = next
  292. ) {
  293. next = offset + 1;
  294. /* skip entries marked unused in the bitmap */
  295. if (!(block->hdr.bitmap[offset / 8] &
  296. (1 << (offset % 8)))) {
  297. _debug("ENT[%zu.%u]: unused",
  298. blkoff / sizeof(union afs_xdr_dir_block), offset);
  299. if (offset >= curr)
  300. ctx->pos = blkoff +
  301. next * sizeof(union afs_xdr_dirent);
  302. continue;
  303. }
  304. /* got a valid entry */
  305. dire = &block->dirents[offset];
  306. nlen = strnlen(dire->u.name,
  307. sizeof(*block) -
  308. offset * sizeof(union afs_xdr_dirent));
  309. _debug("ENT[%zu.%u]: %s %zu \"%s\"",
  310. blkoff / sizeof(union afs_xdr_dir_block), offset,
  311. (offset < curr ? "skip" : "fill"),
  312. nlen, dire->u.name);
  313. /* work out where the next possible entry is */
  314. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
  315. if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
  316. _debug("ENT[%zu.%u]:"
  317. " %u travelled beyond end dir block"
  318. " (len %u/%zu)",
  319. blkoff / sizeof(union afs_xdr_dir_block),
  320. offset, next, tmp, nlen);
  321. return afs_bad(dvnode, afs_file_error_dir_over_end);
  322. }
  323. if (!(block->hdr.bitmap[next / 8] &
  324. (1 << (next % 8)))) {
  325. _debug("ENT[%zu.%u]:"
  326. " %u unmarked extension (len %u/%zu)",
  327. blkoff / sizeof(union afs_xdr_dir_block),
  328. offset, next, tmp, nlen);
  329. return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
  330. }
  331. _debug("ENT[%zu.%u]: ext %u/%zu",
  332. blkoff / sizeof(union afs_xdr_dir_block),
  333. next, tmp, nlen);
  334. next++;
  335. }
  336. /* skip if starts before the current position */
  337. if (offset < curr)
  338. continue;
  339. /* found the next entry */
  340. if (!dir_emit(ctx, dire->u.name, nlen,
  341. ntohl(dire->u.vnode),
  342. (ctx->actor == afs_lookup_filldir ||
  343. ctx->actor == afs_lookup_one_filldir)?
  344. ntohl(dire->u.unique) : DT_UNKNOWN)) {
  345. _leave(" = 0 [full]");
  346. return 0;
  347. }
  348. ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
  349. }
  350. _leave(" = 1 [more]");
  351. return 1;
  352. }
  353. /*
  354. * iterate through the data blob that lists the contents of an AFS directory
  355. */
  356. static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
  357. struct key *key)
  358. {
  359. struct afs_vnode *dvnode = AFS_FS_I(dir);
  360. struct afs_xdr_dir_page *dbuf;
  361. union afs_xdr_dir_block *dblock;
  362. struct afs_read *req;
  363. struct page *page;
  364. unsigned blkoff, limit;
  365. int ret;
  366. _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
  367. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  368. _leave(" = -ESTALE");
  369. return -ESTALE;
  370. }
  371. req = afs_read_dir(dvnode, key);
  372. if (IS_ERR(req))
  373. return PTR_ERR(req);
  374. /* round the file position up to the next entry boundary */
  375. ctx->pos += sizeof(union afs_xdr_dirent) - 1;
  376. ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
  377. /* walk through the blocks in sequence */
  378. ret = 0;
  379. while (ctx->pos < req->actual_len) {
  380. blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
  381. /* Fetch the appropriate page from the directory and re-add it
  382. * to the LRU.
  383. */
  384. page = req->pages[blkoff / PAGE_SIZE];
  385. if (!page) {
  386. ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
  387. break;
  388. }
  389. mark_page_accessed(page);
  390. limit = blkoff & ~(PAGE_SIZE - 1);
  391. dbuf = kmap(page);
  392. /* deal with the individual blocks stashed on this page */
  393. do {
  394. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  395. sizeof(union afs_xdr_dir_block)];
  396. ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
  397. if (ret != 1) {
  398. kunmap(page);
  399. goto out;
  400. }
  401. blkoff += sizeof(union afs_xdr_dir_block);
  402. } while (ctx->pos < dir->i_size && blkoff < limit);
  403. kunmap(page);
  404. ret = 0;
  405. }
  406. out:
  407. up_read(&dvnode->validate_lock);
  408. afs_put_read(req);
  409. _leave(" = %d", ret);
  410. return ret;
  411. }
  412. /*
  413. * read an AFS directory
  414. */
  415. static int afs_readdir(struct file *file, struct dir_context *ctx)
  416. {
  417. return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
  418. }
  419. /*
  420. * Search the directory for a single name
  421. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  422. * uniquifier through dtype
  423. */
  424. static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
  425. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  426. {
  427. struct afs_lookup_one_cookie *cookie =
  428. container_of(ctx, struct afs_lookup_one_cookie, ctx);
  429. _enter("{%s,%u},%s,%u,,%llu,%u",
  430. cookie->name.name, cookie->name.len, name, nlen,
  431. (unsigned long long) ino, dtype);
  432. /* insanity checks first */
  433. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  434. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  435. if (cookie->name.len != nlen ||
  436. memcmp(cookie->name.name, name, nlen) != 0) {
  437. _leave(" = 0 [no]");
  438. return 0;
  439. }
  440. cookie->fid.vnode = ino;
  441. cookie->fid.unique = dtype;
  442. cookie->found = 1;
  443. _leave(" = -1 [found]");
  444. return -1;
  445. }
  446. /*
  447. * Do a lookup of a single name in a directory
  448. * - just returns the FID the dentry name maps to if found
  449. */
  450. static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
  451. struct afs_fid *fid, struct key *key)
  452. {
  453. struct afs_super_info *as = dir->i_sb->s_fs_info;
  454. struct afs_lookup_one_cookie cookie = {
  455. .ctx.actor = afs_lookup_one_filldir,
  456. .name = dentry->d_name,
  457. .fid.vid = as->volume->vid
  458. };
  459. int ret;
  460. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  461. /* search the directory */
  462. ret = afs_dir_iterate(dir, &cookie.ctx, key);
  463. if (ret < 0) {
  464. _leave(" = %d [iter]", ret);
  465. return ret;
  466. }
  467. ret = -ENOENT;
  468. if (!cookie.found) {
  469. _leave(" = -ENOENT [not found]");
  470. return -ENOENT;
  471. }
  472. *fid = cookie.fid;
  473. _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
  474. return 0;
  475. }
  476. /*
  477. * search the directory for a name
  478. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  479. * uniquifier through dtype
  480. */
  481. static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
  482. int nlen, loff_t fpos, u64 ino, unsigned dtype)
  483. {
  484. struct afs_lookup_cookie *cookie =
  485. container_of(ctx, struct afs_lookup_cookie, ctx);
  486. int ret;
  487. _enter("{%s,%u},%s,%u,,%llu,%u",
  488. cookie->name.name, cookie->name.len, name, nlen,
  489. (unsigned long long) ino, dtype);
  490. /* insanity checks first */
  491. BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
  492. BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
  493. if (cookie->found) {
  494. if (cookie->nr_fids < 50) {
  495. cookie->fids[cookie->nr_fids].vnode = ino;
  496. cookie->fids[cookie->nr_fids].unique = dtype;
  497. cookie->nr_fids++;
  498. }
  499. } else if (cookie->name.len == nlen &&
  500. memcmp(cookie->name.name, name, nlen) == 0) {
  501. cookie->fids[0].vnode = ino;
  502. cookie->fids[0].unique = dtype;
  503. cookie->found = 1;
  504. if (cookie->one_only)
  505. return -1;
  506. }
  507. ret = cookie->nr_fids >= 50 ? -1 : 0;
  508. _leave(" = %d", ret);
  509. return ret;
  510. }
  511. /*
  512. * Do a lookup in a directory. We make use of bulk lookup to query a slew of
  513. * files in one go and create inodes for them. The inode of the file we were
  514. * asked for is returned.
  515. */
  516. static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
  517. struct key *key)
  518. {
  519. struct afs_lookup_cookie *cookie;
  520. struct afs_cb_interest *cbi = NULL;
  521. struct afs_super_info *as = dir->i_sb->s_fs_info;
  522. struct afs_iget_data data;
  523. struct afs_fs_cursor fc;
  524. struct afs_vnode *dvnode = AFS_FS_I(dir);
  525. struct inode *inode = NULL;
  526. int ret, i;
  527. _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
  528. cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
  529. if (!cookie)
  530. return ERR_PTR(-ENOMEM);
  531. cookie->ctx.actor = afs_lookup_filldir;
  532. cookie->name = dentry->d_name;
  533. cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
  534. read_seqlock_excl(&dvnode->cb_lock);
  535. if (dvnode->cb_interest &&
  536. dvnode->cb_interest->server &&
  537. test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
  538. cookie->one_only = true;
  539. read_sequnlock_excl(&dvnode->cb_lock);
  540. for (i = 0; i < 50; i++)
  541. cookie->fids[i].vid = as->volume->vid;
  542. /* search the directory */
  543. ret = afs_dir_iterate(dir, &cookie->ctx, key);
  544. if (ret < 0) {
  545. inode = ERR_PTR(ret);
  546. goto out;
  547. }
  548. inode = ERR_PTR(-ENOENT);
  549. if (!cookie->found)
  550. goto out;
  551. /* Check to see if we already have an inode for the primary fid. */
  552. data.volume = dvnode->volume;
  553. data.fid = cookie->fids[0];
  554. inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
  555. if (inode)
  556. goto out;
  557. /* Need space for examining all the selected files */
  558. inode = ERR_PTR(-ENOMEM);
  559. cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
  560. GFP_KERNEL);
  561. if (!cookie->statuses)
  562. goto out;
  563. cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
  564. GFP_KERNEL);
  565. if (!cookie->callbacks)
  566. goto out_s;
  567. /* Try FS.InlineBulkStatus first. Abort codes for the individual
  568. * lookups contained therein are stored in the reply without aborting
  569. * the whole operation.
  570. */
  571. if (cookie->one_only)
  572. goto no_inline_bulk_status;
  573. inode = ERR_PTR(-ERESTARTSYS);
  574. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  575. while (afs_select_fileserver(&fc)) {
  576. if (test_bit(AFS_SERVER_FL_NO_IBULK,
  577. &fc.cbi->server->flags)) {
  578. fc.ac.abort_code = RX_INVALID_OPERATION;
  579. fc.ac.error = -ECONNABORTED;
  580. break;
  581. }
  582. afs_fs_inline_bulk_status(&fc,
  583. afs_v2net(dvnode),
  584. cookie->fids,
  585. cookie->statuses,
  586. cookie->callbacks,
  587. cookie->nr_fids, NULL);
  588. }
  589. if (fc.ac.error == 0)
  590. cbi = afs_get_cb_interest(fc.cbi);
  591. if (fc.ac.abort_code == RX_INVALID_OPERATION)
  592. set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
  593. inode = ERR_PTR(afs_end_vnode_operation(&fc));
  594. }
  595. if (!IS_ERR(inode))
  596. goto success;
  597. if (fc.ac.abort_code != RX_INVALID_OPERATION)
  598. goto out_c;
  599. no_inline_bulk_status:
  600. /* We could try FS.BulkStatus next, but this aborts the entire op if
  601. * any of the lookups fails - so, for the moment, revert to
  602. * FS.FetchStatus for just the primary fid.
  603. */
  604. cookie->nr_fids = 1;
  605. inode = ERR_PTR(-ERESTARTSYS);
  606. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  607. while (afs_select_fileserver(&fc)) {
  608. afs_fs_fetch_status(&fc,
  609. afs_v2net(dvnode),
  610. cookie->fids,
  611. cookie->statuses,
  612. cookie->callbacks,
  613. NULL);
  614. }
  615. if (fc.ac.error == 0)
  616. cbi = afs_get_cb_interest(fc.cbi);
  617. inode = ERR_PTR(afs_end_vnode_operation(&fc));
  618. }
  619. if (IS_ERR(inode))
  620. goto out_c;
  621. for (i = 0; i < cookie->nr_fids; i++)
  622. cookie->statuses[i].abort_code = 0;
  623. success:
  624. /* Turn all the files into inodes and save the first one - which is the
  625. * one we actually want.
  626. */
  627. if (cookie->statuses[0].abort_code != 0)
  628. inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
  629. for (i = 0; i < cookie->nr_fids; i++) {
  630. struct inode *ti;
  631. if (cookie->statuses[i].abort_code != 0)
  632. continue;
  633. ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
  634. &cookie->statuses[i],
  635. &cookie->callbacks[i],
  636. cbi);
  637. if (i == 0) {
  638. inode = ti;
  639. } else {
  640. if (!IS_ERR(ti))
  641. iput(ti);
  642. }
  643. }
  644. out_c:
  645. afs_put_cb_interest(afs_v2net(dvnode), cbi);
  646. kfree(cookie->callbacks);
  647. out_s:
  648. kfree(cookie->statuses);
  649. out:
  650. kfree(cookie);
  651. return inode;
  652. }
  653. /*
  654. * Look up an entry in a directory with @sys substitution.
  655. */
  656. static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
  657. struct key *key)
  658. {
  659. struct afs_sysnames *subs;
  660. struct afs_net *net = afs_i2net(dir);
  661. struct dentry *ret;
  662. char *buf, *p, *name;
  663. int len, i;
  664. _enter("");
  665. ret = ERR_PTR(-ENOMEM);
  666. p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
  667. if (!buf)
  668. goto out_p;
  669. if (dentry->d_name.len > 4) {
  670. memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
  671. p += dentry->d_name.len - 4;
  672. }
  673. /* There is an ordered list of substitutes that we have to try. */
  674. read_lock(&net->sysnames_lock);
  675. subs = net->sysnames;
  676. refcount_inc(&subs->usage);
  677. read_unlock(&net->sysnames_lock);
  678. for (i = 0; i < subs->nr; i++) {
  679. name = subs->subs[i];
  680. len = dentry->d_name.len - 4 + strlen(name);
  681. if (len >= AFSNAMEMAX) {
  682. ret = ERR_PTR(-ENAMETOOLONG);
  683. goto out_s;
  684. }
  685. strcpy(p, name);
  686. ret = lookup_one_len(buf, dentry->d_parent, len);
  687. if (IS_ERR(ret) || d_is_positive(ret))
  688. goto out_s;
  689. dput(ret);
  690. }
  691. /* We don't want to d_add() the @sys dentry here as we don't want to
  692. * the cached dentry to hide changes to the sysnames list.
  693. */
  694. ret = NULL;
  695. out_s:
  696. afs_put_sysnames(subs);
  697. kfree(buf);
  698. out_p:
  699. key_put(key);
  700. return ret;
  701. }
  702. /*
  703. * look up an entry in a directory
  704. */
  705. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  706. unsigned int flags)
  707. {
  708. struct afs_vnode *dvnode = AFS_FS_I(dir);
  709. struct inode *inode;
  710. struct dentry *d;
  711. struct key *key;
  712. int ret;
  713. _enter("{%llx:%llu},%p{%pd},",
  714. dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
  715. ASSERTCMP(d_inode(dentry), ==, NULL);
  716. if (dentry->d_name.len >= AFSNAMEMAX) {
  717. _leave(" = -ENAMETOOLONG");
  718. return ERR_PTR(-ENAMETOOLONG);
  719. }
  720. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
  721. _leave(" = -ESTALE");
  722. return ERR_PTR(-ESTALE);
  723. }
  724. key = afs_request_key(dvnode->volume->cell);
  725. if (IS_ERR(key)) {
  726. _leave(" = %ld [key]", PTR_ERR(key));
  727. return ERR_CAST(key);
  728. }
  729. ret = afs_validate(dvnode, key);
  730. if (ret < 0) {
  731. key_put(key);
  732. _leave(" = %d [val]", ret);
  733. return ERR_PTR(ret);
  734. }
  735. if (dentry->d_name.len >= 4 &&
  736. dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
  737. dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
  738. dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
  739. dentry->d_name.name[dentry->d_name.len - 1] == 's')
  740. return afs_lookup_atsys(dir, dentry, key);
  741. afs_stat_v(dvnode, n_lookup);
  742. inode = afs_do_lookup(dir, dentry, key);
  743. key_put(key);
  744. if (inode == ERR_PTR(-ENOENT)) {
  745. inode = afs_try_auto_mntpt(dentry, dir);
  746. } else {
  747. dentry->d_fsdata =
  748. (void *)(unsigned long)dvnode->status.data_version;
  749. }
  750. d = d_splice_alias(inode, dentry);
  751. if (!IS_ERR_OR_NULL(d))
  752. d->d_fsdata = dentry->d_fsdata;
  753. return d;
  754. }
  755. /*
  756. * check that a dentry lookup hit has found a valid entry
  757. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  758. * inode
  759. */
  760. static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
  761. {
  762. struct afs_vnode *vnode, *dir;
  763. struct afs_fid uninitialized_var(fid);
  764. struct dentry *parent;
  765. struct inode *inode;
  766. struct key *key;
  767. long dir_version, de_version;
  768. int ret;
  769. if (flags & LOOKUP_RCU)
  770. return -ECHILD;
  771. if (d_really_is_positive(dentry)) {
  772. vnode = AFS_FS_I(d_inode(dentry));
  773. _enter("{v={%llx:%llu} n=%pd fl=%lx},",
  774. vnode->fid.vid, vnode->fid.vnode, dentry,
  775. vnode->flags);
  776. } else {
  777. _enter("{neg n=%pd}", dentry);
  778. }
  779. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  780. if (IS_ERR(key))
  781. key = NULL;
  782. if (d_really_is_positive(dentry)) {
  783. inode = d_inode(dentry);
  784. if (inode) {
  785. vnode = AFS_FS_I(inode);
  786. afs_validate(vnode, key);
  787. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  788. goto out_bad;
  789. }
  790. }
  791. /* lock down the parent dentry so we can peer at it */
  792. parent = dget_parent(dentry);
  793. dir = AFS_FS_I(d_inode(parent));
  794. /* validate the parent directory */
  795. afs_validate(dir, key);
  796. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  797. _debug("%pd: parent dir deleted", dentry);
  798. goto out_bad_parent;
  799. }
  800. /* We only need to invalidate a dentry if the server's copy changed
  801. * behind our back. If we made the change, it's no problem. Note that
  802. * on a 32-bit system, we only have 32 bits in the dentry to store the
  803. * version.
  804. */
  805. dir_version = (long)dir->status.data_version;
  806. de_version = (long)dentry->d_fsdata;
  807. if (de_version == dir_version)
  808. goto out_valid;
  809. dir_version = (long)dir->invalid_before;
  810. if (de_version - dir_version >= 0)
  811. goto out_valid;
  812. _debug("dir modified");
  813. afs_stat_v(dir, n_reval);
  814. /* search the directory for this vnode */
  815. ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
  816. switch (ret) {
  817. case 0:
  818. /* the filename maps to something */
  819. if (d_really_is_negative(dentry))
  820. goto out_bad_parent;
  821. inode = d_inode(dentry);
  822. if (is_bad_inode(inode)) {
  823. printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
  824. dentry);
  825. goto out_bad_parent;
  826. }
  827. vnode = AFS_FS_I(inode);
  828. /* if the vnode ID has changed, then the dirent points to a
  829. * different file */
  830. if (fid.vnode != vnode->fid.vnode) {
  831. _debug("%pd: dirent changed [%llu != %llu]",
  832. dentry, fid.vnode,
  833. vnode->fid.vnode);
  834. goto not_found;
  835. }
  836. /* if the vnode ID uniqifier has changed, then the file has
  837. * been deleted and replaced, and the original vnode ID has
  838. * been reused */
  839. if (fid.unique != vnode->fid.unique) {
  840. _debug("%pd: file deleted (uq %u -> %u I:%u)",
  841. dentry, fid.unique,
  842. vnode->fid.unique,
  843. vnode->vfs_inode.i_generation);
  844. write_seqlock(&vnode->cb_lock);
  845. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  846. write_sequnlock(&vnode->cb_lock);
  847. goto not_found;
  848. }
  849. goto out_valid;
  850. case -ENOENT:
  851. /* the filename is unknown */
  852. _debug("%pd: dirent not found", dentry);
  853. if (d_really_is_positive(dentry))
  854. goto not_found;
  855. goto out_valid;
  856. default:
  857. _debug("failed to iterate dir %pd: %d",
  858. parent, ret);
  859. goto out_bad_parent;
  860. }
  861. out_valid:
  862. dentry->d_fsdata = (void *)dir_version;
  863. dput(parent);
  864. key_put(key);
  865. _leave(" = 1 [valid]");
  866. return 1;
  867. /* the dirent, if it exists, now points to a different vnode */
  868. not_found:
  869. spin_lock(&dentry->d_lock);
  870. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  871. spin_unlock(&dentry->d_lock);
  872. out_bad_parent:
  873. _debug("dropping dentry %pd2", dentry);
  874. dput(parent);
  875. out_bad:
  876. key_put(key);
  877. _leave(" = 0 [bad]");
  878. return 0;
  879. }
  880. /*
  881. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  882. * sleep)
  883. * - called from dput() when d_count is going to 0.
  884. * - return 1 to request dentry be unhashed, 0 otherwise
  885. */
  886. static int afs_d_delete(const struct dentry *dentry)
  887. {
  888. _enter("%pd", dentry);
  889. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  890. goto zap;
  891. if (d_really_is_positive(dentry) &&
  892. (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
  893. test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
  894. goto zap;
  895. _leave(" = 0 [keep]");
  896. return 0;
  897. zap:
  898. _leave(" = 1 [zap]");
  899. return 1;
  900. }
  901. /*
  902. * handle dentry release
  903. */
  904. void afs_d_release(struct dentry *dentry)
  905. {
  906. _enter("%pd", dentry);
  907. }
  908. /*
  909. * Create a new inode for create/mkdir/symlink
  910. */
  911. static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
  912. struct dentry *new_dentry,
  913. struct afs_fid *newfid,
  914. struct afs_file_status *newstatus,
  915. struct afs_callback *newcb)
  916. {
  917. struct afs_vnode *vnode;
  918. struct inode *inode;
  919. if (fc->ac.error < 0)
  920. return;
  921. inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
  922. newfid, newstatus, newcb, fc->cbi);
  923. if (IS_ERR(inode)) {
  924. /* ENOMEM or EINTR at a really inconvenient time - just abandon
  925. * the new directory on the server.
  926. */
  927. fc->ac.error = PTR_ERR(inode);
  928. return;
  929. }
  930. vnode = AFS_FS_I(inode);
  931. set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  932. afs_vnode_commit_status(fc, vnode, 0);
  933. d_instantiate(new_dentry, inode);
  934. }
  935. /*
  936. * create a directory on an AFS filesystem
  937. */
  938. static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  939. {
  940. struct afs_file_status newstatus;
  941. struct afs_fs_cursor fc;
  942. struct afs_callback newcb;
  943. struct afs_vnode *dvnode = AFS_FS_I(dir);
  944. struct afs_fid newfid;
  945. struct key *key;
  946. u64 data_version = dvnode->status.data_version;
  947. int ret;
  948. mode |= S_IFDIR;
  949. _enter("{%llx:%llu},{%pd},%ho",
  950. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  951. key = afs_request_key(dvnode->volume->cell);
  952. if (IS_ERR(key)) {
  953. ret = PTR_ERR(key);
  954. goto error;
  955. }
  956. ret = -ERESTARTSYS;
  957. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  958. while (afs_select_fileserver(&fc)) {
  959. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  960. afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
  961. &newfid, &newstatus, &newcb);
  962. }
  963. afs_check_for_remote_deletion(&fc, fc.vnode);
  964. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  965. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
  966. ret = afs_end_vnode_operation(&fc);
  967. if (ret < 0)
  968. goto error_key;
  969. } else {
  970. goto error_key;
  971. }
  972. if (ret == 0 &&
  973. test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  974. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  975. afs_edit_dir_for_create);
  976. key_put(key);
  977. _leave(" = 0");
  978. return 0;
  979. error_key:
  980. key_put(key);
  981. error:
  982. d_drop(dentry);
  983. _leave(" = %d", ret);
  984. return ret;
  985. }
  986. /*
  987. * Remove a subdir from a directory.
  988. */
  989. static void afs_dir_remove_subdir(struct dentry *dentry)
  990. {
  991. if (d_really_is_positive(dentry)) {
  992. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  993. clear_nlink(&vnode->vfs_inode);
  994. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  995. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  996. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  997. }
  998. }
  999. /*
  1000. * remove a directory from an AFS filesystem
  1001. */
  1002. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  1003. {
  1004. struct afs_fs_cursor fc;
  1005. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
  1006. struct key *key;
  1007. u64 data_version = dvnode->status.data_version;
  1008. int ret;
  1009. _enter("{%llx:%llu},{%pd}",
  1010. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1011. key = afs_request_key(dvnode->volume->cell);
  1012. if (IS_ERR(key)) {
  1013. ret = PTR_ERR(key);
  1014. goto error;
  1015. }
  1016. /* Try to make sure we have a callback promise on the victim. */
  1017. if (d_really_is_positive(dentry)) {
  1018. vnode = AFS_FS_I(d_inode(dentry));
  1019. ret = afs_validate(vnode, key);
  1020. if (ret < 0)
  1021. goto error_key;
  1022. }
  1023. ret = -ERESTARTSYS;
  1024. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1025. while (afs_select_fileserver(&fc)) {
  1026. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1027. afs_fs_remove(&fc, vnode, dentry->d_name.name, true,
  1028. data_version);
  1029. }
  1030. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1031. ret = afs_end_vnode_operation(&fc);
  1032. if (ret == 0) {
  1033. afs_dir_remove_subdir(dentry);
  1034. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1035. afs_edit_dir_remove(dvnode, &dentry->d_name,
  1036. afs_edit_dir_for_rmdir);
  1037. }
  1038. }
  1039. error_key:
  1040. key_put(key);
  1041. error:
  1042. return ret;
  1043. }
  1044. /*
  1045. * Remove a link to a file or symlink from a directory.
  1046. *
  1047. * If the file was not deleted due to excess hard links, the fileserver will
  1048. * break the callback promise on the file - if it had one - before it returns
  1049. * to us, and if it was deleted, it won't
  1050. *
  1051. * However, if we didn't have a callback promise outstanding, or it was
  1052. * outstanding on a different server, then it won't break it either...
  1053. */
  1054. static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
  1055. unsigned long d_version_before,
  1056. unsigned long d_version_after)
  1057. {
  1058. bool dir_valid;
  1059. int ret = 0;
  1060. /* There were no intervening changes on the server if the version
  1061. * number we got back was incremented by exactly 1.
  1062. */
  1063. dir_valid = (d_version_after == d_version_before + 1);
  1064. if (d_really_is_positive(dentry)) {
  1065. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  1066. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  1067. /* Already done */
  1068. } else if (dir_valid) {
  1069. drop_nlink(&vnode->vfs_inode);
  1070. if (vnode->vfs_inode.i_nlink == 0) {
  1071. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  1072. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  1073. }
  1074. ret = 0;
  1075. } else {
  1076. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  1077. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  1078. kdebug("AFS_VNODE_DELETED");
  1079. ret = afs_validate(vnode, key);
  1080. if (ret == -ESTALE)
  1081. ret = 0;
  1082. }
  1083. _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
  1084. }
  1085. return ret;
  1086. }
  1087. /*
  1088. * Remove a file or symlink from an AFS filesystem.
  1089. */
  1090. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  1091. {
  1092. struct afs_fs_cursor fc;
  1093. struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
  1094. struct key *key;
  1095. unsigned long d_version = (unsigned long)dentry->d_fsdata;
  1096. u64 data_version = dvnode->status.data_version;
  1097. int ret;
  1098. _enter("{%llx:%llu},{%pd}",
  1099. dvnode->fid.vid, dvnode->fid.vnode, dentry);
  1100. if (dentry->d_name.len >= AFSNAMEMAX)
  1101. return -ENAMETOOLONG;
  1102. key = afs_request_key(dvnode->volume->cell);
  1103. if (IS_ERR(key)) {
  1104. ret = PTR_ERR(key);
  1105. goto error;
  1106. }
  1107. /* Try to make sure we have a callback promise on the victim. */
  1108. if (d_really_is_positive(dentry)) {
  1109. vnode = AFS_FS_I(d_inode(dentry));
  1110. ret = afs_validate(vnode, key);
  1111. if (ret < 0)
  1112. goto error_key;
  1113. }
  1114. ret = -ERESTARTSYS;
  1115. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1116. while (afs_select_fileserver(&fc)) {
  1117. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1118. if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
  1119. !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
  1120. yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
  1121. data_version);
  1122. if (fc.ac.error != -ECONNABORTED ||
  1123. fc.ac.abort_code != RXGEN_OPCODE)
  1124. continue;
  1125. set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
  1126. }
  1127. afs_fs_remove(&fc, vnode, dentry->d_name.name, false,
  1128. data_version);
  1129. }
  1130. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1131. ret = afs_end_vnode_operation(&fc);
  1132. if (ret == 0)
  1133. ret = afs_dir_remove_link(
  1134. dentry, key, d_version,
  1135. (unsigned long)dvnode->status.data_version);
  1136. if (ret == 0 &&
  1137. test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1138. afs_edit_dir_remove(dvnode, &dentry->d_name,
  1139. afs_edit_dir_for_unlink);
  1140. }
  1141. error_key:
  1142. key_put(key);
  1143. error:
  1144. _leave(" = %d", ret);
  1145. return ret;
  1146. }
  1147. /*
  1148. * create a regular file on an AFS filesystem
  1149. */
  1150. static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1151. bool excl)
  1152. {
  1153. struct afs_fs_cursor fc;
  1154. struct afs_file_status newstatus;
  1155. struct afs_callback newcb;
  1156. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1157. struct afs_fid newfid;
  1158. struct key *key;
  1159. u64 data_version = dvnode->status.data_version;
  1160. int ret;
  1161. mode |= S_IFREG;
  1162. _enter("{%llx:%llu},{%pd},%ho,",
  1163. dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
  1164. ret = -ENAMETOOLONG;
  1165. if (dentry->d_name.len >= AFSNAMEMAX)
  1166. goto error;
  1167. key = afs_request_key(dvnode->volume->cell);
  1168. if (IS_ERR(key)) {
  1169. ret = PTR_ERR(key);
  1170. goto error;
  1171. }
  1172. ret = -ERESTARTSYS;
  1173. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1174. while (afs_select_fileserver(&fc)) {
  1175. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1176. afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
  1177. &newfid, &newstatus, &newcb);
  1178. }
  1179. afs_check_for_remote_deletion(&fc, fc.vnode);
  1180. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1181. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
  1182. ret = afs_end_vnode_operation(&fc);
  1183. if (ret < 0)
  1184. goto error_key;
  1185. } else {
  1186. goto error_key;
  1187. }
  1188. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1189. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  1190. afs_edit_dir_for_create);
  1191. key_put(key);
  1192. _leave(" = 0");
  1193. return 0;
  1194. error_key:
  1195. key_put(key);
  1196. error:
  1197. d_drop(dentry);
  1198. _leave(" = %d", ret);
  1199. return ret;
  1200. }
  1201. /*
  1202. * create a hard link between files in an AFS filesystem
  1203. */
  1204. static int afs_link(struct dentry *from, struct inode *dir,
  1205. struct dentry *dentry)
  1206. {
  1207. struct afs_fs_cursor fc;
  1208. struct afs_vnode *dvnode, *vnode;
  1209. struct key *key;
  1210. u64 data_version;
  1211. int ret;
  1212. vnode = AFS_FS_I(d_inode(from));
  1213. dvnode = AFS_FS_I(dir);
  1214. data_version = dvnode->status.data_version;
  1215. _enter("{%llx:%llu},{%llx:%llu},{%pd}",
  1216. vnode->fid.vid, vnode->fid.vnode,
  1217. dvnode->fid.vid, dvnode->fid.vnode,
  1218. dentry);
  1219. ret = -ENAMETOOLONG;
  1220. if (dentry->d_name.len >= AFSNAMEMAX)
  1221. goto error;
  1222. key = afs_request_key(dvnode->volume->cell);
  1223. if (IS_ERR(key)) {
  1224. ret = PTR_ERR(key);
  1225. goto error;
  1226. }
  1227. ret = -ERESTARTSYS;
  1228. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1229. if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
  1230. afs_end_vnode_operation(&fc);
  1231. goto error_key;
  1232. }
  1233. while (afs_select_fileserver(&fc)) {
  1234. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1235. fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
  1236. afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
  1237. }
  1238. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1239. afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
  1240. ihold(&vnode->vfs_inode);
  1241. d_instantiate(dentry, &vnode->vfs_inode);
  1242. mutex_unlock(&vnode->io_lock);
  1243. ret = afs_end_vnode_operation(&fc);
  1244. if (ret < 0)
  1245. goto error_key;
  1246. } else {
  1247. goto error_key;
  1248. }
  1249. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1250. afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
  1251. afs_edit_dir_for_link);
  1252. key_put(key);
  1253. _leave(" = 0");
  1254. return 0;
  1255. error_key:
  1256. key_put(key);
  1257. error:
  1258. d_drop(dentry);
  1259. _leave(" = %d", ret);
  1260. return ret;
  1261. }
  1262. /*
  1263. * create a symlink in an AFS filesystem
  1264. */
  1265. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  1266. const char *content)
  1267. {
  1268. struct afs_fs_cursor fc;
  1269. struct afs_file_status newstatus;
  1270. struct afs_vnode *dvnode = AFS_FS_I(dir);
  1271. struct afs_fid newfid;
  1272. struct key *key;
  1273. u64 data_version = dvnode->status.data_version;
  1274. int ret;
  1275. _enter("{%llx:%llu},{%pd},%s",
  1276. dvnode->fid.vid, dvnode->fid.vnode, dentry,
  1277. content);
  1278. ret = -ENAMETOOLONG;
  1279. if (dentry->d_name.len >= AFSNAMEMAX)
  1280. goto error;
  1281. ret = -EINVAL;
  1282. if (strlen(content) >= AFSPATHMAX)
  1283. goto error;
  1284. key = afs_request_key(dvnode->volume->cell);
  1285. if (IS_ERR(key)) {
  1286. ret = PTR_ERR(key);
  1287. goto error;
  1288. }
  1289. ret = -ERESTARTSYS;
  1290. if (afs_begin_vnode_operation(&fc, dvnode, key)) {
  1291. while (afs_select_fileserver(&fc)) {
  1292. fc.cb_break = afs_calc_vnode_cb_break(dvnode);
  1293. afs_fs_symlink(&fc, dentry->d_name.name,
  1294. content, data_version,
  1295. &newfid, &newstatus);
  1296. }
  1297. afs_check_for_remote_deletion(&fc, fc.vnode);
  1298. afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
  1299. afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
  1300. ret = afs_end_vnode_operation(&fc);
  1301. if (ret < 0)
  1302. goto error_key;
  1303. } else {
  1304. goto error_key;
  1305. }
  1306. if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1307. afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
  1308. afs_edit_dir_for_symlink);
  1309. key_put(key);
  1310. _leave(" = 0");
  1311. return 0;
  1312. error_key:
  1313. key_put(key);
  1314. error:
  1315. d_drop(dentry);
  1316. _leave(" = %d", ret);
  1317. return ret;
  1318. }
  1319. /*
  1320. * rename a file in an AFS filesystem and/or move it between directories
  1321. */
  1322. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1323. struct inode *new_dir, struct dentry *new_dentry,
  1324. unsigned int flags)
  1325. {
  1326. struct afs_fs_cursor fc;
  1327. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  1328. struct key *key;
  1329. u64 orig_data_version, new_data_version;
  1330. bool new_negative = d_is_negative(new_dentry);
  1331. int ret;
  1332. if (flags)
  1333. return -EINVAL;
  1334. vnode = AFS_FS_I(d_inode(old_dentry));
  1335. orig_dvnode = AFS_FS_I(old_dir);
  1336. new_dvnode = AFS_FS_I(new_dir);
  1337. orig_data_version = orig_dvnode->status.data_version;
  1338. new_data_version = new_dvnode->status.data_version;
  1339. _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
  1340. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  1341. vnode->fid.vid, vnode->fid.vnode,
  1342. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  1343. new_dentry);
  1344. key = afs_request_key(orig_dvnode->volume->cell);
  1345. if (IS_ERR(key)) {
  1346. ret = PTR_ERR(key);
  1347. goto error;
  1348. }
  1349. ret = -ERESTARTSYS;
  1350. if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
  1351. if (orig_dvnode != new_dvnode) {
  1352. if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
  1353. afs_end_vnode_operation(&fc);
  1354. goto error_key;
  1355. }
  1356. }
  1357. while (afs_select_fileserver(&fc)) {
  1358. fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
  1359. fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
  1360. afs_fs_rename(&fc, old_dentry->d_name.name,
  1361. new_dvnode, new_dentry->d_name.name,
  1362. orig_data_version, new_data_version);
  1363. }
  1364. afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
  1365. afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
  1366. if (orig_dvnode != new_dvnode)
  1367. mutex_unlock(&new_dvnode->io_lock);
  1368. ret = afs_end_vnode_operation(&fc);
  1369. if (ret < 0)
  1370. goto error_key;
  1371. }
  1372. if (ret == 0) {
  1373. if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
  1374. afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
  1375. afs_edit_dir_for_rename);
  1376. if (!new_negative &&
  1377. test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
  1378. afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
  1379. afs_edit_dir_for_rename);
  1380. if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
  1381. afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
  1382. &vnode->fid, afs_edit_dir_for_rename);
  1383. }
  1384. error_key:
  1385. key_put(key);
  1386. error:
  1387. _leave(" = %d", ret);
  1388. return ret;
  1389. }
  1390. /*
  1391. * Release a directory page and clean up its private state if it's not busy
  1392. * - return true if the page can now be released, false if not
  1393. */
  1394. static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
  1395. {
  1396. struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
  1397. _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
  1398. set_page_private(page, 0);
  1399. ClearPagePrivate(page);
  1400. /* The directory will need reloading. */
  1401. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1402. afs_stat_v(dvnode, n_relpg);
  1403. return 1;
  1404. }
  1405. /*
  1406. * invalidate part or all of a page
  1407. * - release a page and clean up its private data if offset is 0 (indicating
  1408. * the entire page)
  1409. */
  1410. static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
  1411. unsigned int length)
  1412. {
  1413. struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
  1414. _enter("{%lu},%u,%u", page->index, offset, length);
  1415. BUG_ON(!PageLocked(page));
  1416. /* The directory will need reloading. */
  1417. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
  1418. afs_stat_v(dvnode, n_inval);
  1419. /* we clean up only if the entire page is being invalidated */
  1420. if (offset == 0 && length == PAGE_SIZE) {
  1421. set_page_private(page, 0);
  1422. ClearPagePrivate(page);
  1423. }
  1424. }