dir.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/module.h>
  20. #include <linux/time.h>
  21. #include <linux/errno.h>
  22. #include <linux/stat.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/string.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/sunrpc/clnt.h>
  29. #include <linux/nfs_fs.h>
  30. #include <linux/nfs_mount.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/swap.h>
  36. #include <linux/sched.h>
  37. #include <linux/kmemleak.h>
  38. #include <linux/xattr.h>
  39. #include "delegation.h"
  40. #include "iostat.h"
  41. #include "internal.h"
  42. #include "fscache.h"
  43. #include "nfstrace.h"
  44. /* #define NFS_DEBUG_VERBOSE 1 */
  45. static int nfs_opendir(struct inode *, struct file *);
  46. static int nfs_closedir(struct inode *, struct file *);
  47. static int nfs_readdir(struct file *, struct dir_context *);
  48. static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
  49. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  50. static void nfs_readdir_clear_array(struct page*);
  51. const struct file_operations nfs_dir_operations = {
  52. .llseek = nfs_llseek_dir,
  53. .read = generic_read_dir,
  54. .iterate_shared = nfs_readdir,
  55. .open = nfs_opendir,
  56. .release = nfs_closedir,
  57. .fsync = nfs_fsync_dir,
  58. };
  59. const struct address_space_operations nfs_dir_aops = {
  60. .freepage = nfs_readdir_clear_array,
  61. };
  62. static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
  63. {
  64. struct nfs_inode *nfsi = NFS_I(dir);
  65. struct nfs_open_dir_context *ctx;
  66. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  67. if (ctx != NULL) {
  68. ctx->duped = 0;
  69. ctx->attr_gencount = nfsi->attr_gencount;
  70. ctx->dir_cookie = 0;
  71. ctx->dup_cookie = 0;
  72. ctx->cred = get_rpccred(cred);
  73. spin_lock(&dir->i_lock);
  74. list_add(&ctx->list, &nfsi->open_files);
  75. spin_unlock(&dir->i_lock);
  76. return ctx;
  77. }
  78. return ERR_PTR(-ENOMEM);
  79. }
  80. static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
  81. {
  82. spin_lock(&dir->i_lock);
  83. list_del(&ctx->list);
  84. spin_unlock(&dir->i_lock);
  85. put_rpccred(ctx->cred);
  86. kfree(ctx);
  87. }
  88. /*
  89. * Open file
  90. */
  91. static int
  92. nfs_opendir(struct inode *inode, struct file *filp)
  93. {
  94. int res = 0;
  95. struct nfs_open_dir_context *ctx;
  96. struct rpc_cred *cred;
  97. dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
  98. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  99. cred = rpc_lookup_cred();
  100. if (IS_ERR(cred))
  101. return PTR_ERR(cred);
  102. ctx = alloc_nfs_open_dir_context(inode, cred);
  103. if (IS_ERR(ctx)) {
  104. res = PTR_ERR(ctx);
  105. goto out;
  106. }
  107. filp->private_data = ctx;
  108. if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
  109. /* This is a mountpoint, so d_revalidate will never
  110. * have been called, so we need to refresh the
  111. * inode (for close-open consistency) ourselves.
  112. */
  113. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  114. }
  115. out:
  116. put_rpccred(cred);
  117. return res;
  118. }
  119. static int
  120. nfs_closedir(struct inode *inode, struct file *filp)
  121. {
  122. put_nfs_open_dir_context(file_inode(filp), filp->private_data);
  123. return 0;
  124. }
  125. struct nfs_cache_array_entry {
  126. u64 cookie;
  127. u64 ino;
  128. struct qstr string;
  129. unsigned char d_type;
  130. };
  131. struct nfs_cache_array {
  132. atomic_t refcount;
  133. int size;
  134. int eof_index;
  135. u64 last_cookie;
  136. struct nfs_cache_array_entry array[0];
  137. };
  138. typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
  139. typedef struct {
  140. struct file *file;
  141. struct page *page;
  142. struct dir_context *ctx;
  143. unsigned long page_index;
  144. u64 *dir_cookie;
  145. u64 last_cookie;
  146. loff_t current_index;
  147. decode_dirent_t decode;
  148. unsigned long timestamp;
  149. unsigned long gencount;
  150. unsigned int cache_entry_index;
  151. unsigned int plus:1;
  152. unsigned int eof:1;
  153. } nfs_readdir_descriptor_t;
  154. /*
  155. * The caller is responsible for calling nfs_readdir_release_array(page)
  156. */
  157. static
  158. struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
  159. {
  160. void *ptr;
  161. if (page == NULL)
  162. return ERR_PTR(-EIO);
  163. ptr = kmap(page);
  164. if (ptr == NULL)
  165. return ERR_PTR(-ENOMEM);
  166. return ptr;
  167. }
  168. static
  169. void nfs_readdir_release_array(struct page *page)
  170. {
  171. kunmap(page);
  172. }
  173. /*
  174. * we are freeing strings created by nfs_add_to_readdir_array()
  175. */
  176. static
  177. void nfs_readdir_clear_array(struct page *page)
  178. {
  179. struct nfs_cache_array *array;
  180. int i;
  181. array = kmap_atomic(page);
  182. if (atomic_dec_and_test(&array->refcount))
  183. for (i = 0; i < array->size; i++)
  184. kfree(array->array[i].string.name);
  185. kunmap_atomic(array);
  186. }
  187. static bool grab_page(struct page *page)
  188. {
  189. struct nfs_cache_array *array = kmap_atomic(page);
  190. bool res = atomic_inc_not_zero(&array->refcount);
  191. kunmap_atomic(array);
  192. return res;
  193. }
  194. /*
  195. * the caller is responsible for freeing qstr.name
  196. * when called by nfs_readdir_add_to_array, the strings will be freed in
  197. * nfs_clear_readdir_array()
  198. */
  199. static
  200. int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
  201. {
  202. string->len = len;
  203. string->name = kmemdup(name, len, GFP_KERNEL);
  204. if (string->name == NULL)
  205. return -ENOMEM;
  206. /*
  207. * Avoid a kmemleak false positive. The pointer to the name is stored
  208. * in a page cache page which kmemleak does not scan.
  209. */
  210. kmemleak_not_leak(string->name);
  211. string->hash = full_name_hash(NULL, name, len);
  212. return 0;
  213. }
  214. static
  215. int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
  216. {
  217. struct nfs_cache_array *array = nfs_readdir_get_array(page);
  218. struct nfs_cache_array_entry *cache_entry;
  219. int ret;
  220. if (IS_ERR(array))
  221. return PTR_ERR(array);
  222. cache_entry = &array->array[array->size];
  223. /* Check that this entry lies within the page bounds */
  224. ret = -ENOSPC;
  225. if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
  226. goto out;
  227. cache_entry->cookie = entry->prev_cookie;
  228. cache_entry->ino = entry->ino;
  229. cache_entry->d_type = entry->d_type;
  230. ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
  231. if (ret)
  232. goto out;
  233. array->last_cookie = entry->cookie;
  234. array->size++;
  235. if (entry->eof != 0)
  236. array->eof_index = array->size;
  237. out:
  238. nfs_readdir_release_array(page);
  239. return ret;
  240. }
  241. static
  242. int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  243. {
  244. loff_t diff = desc->ctx->pos - desc->current_index;
  245. unsigned int index;
  246. if (diff < 0)
  247. goto out_eof;
  248. if (diff >= array->size) {
  249. if (array->eof_index >= 0)
  250. goto out_eof;
  251. return -EAGAIN;
  252. }
  253. index = (unsigned int)diff;
  254. *desc->dir_cookie = array->array[index].cookie;
  255. desc->cache_entry_index = index;
  256. return 0;
  257. out_eof:
  258. desc->eof = 1;
  259. return -EBADCOOKIE;
  260. }
  261. static bool
  262. nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
  263. {
  264. if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
  265. return false;
  266. smp_rmb();
  267. return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
  268. }
  269. static
  270. int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
  271. {
  272. int i;
  273. loff_t new_pos;
  274. int status = -EAGAIN;
  275. for (i = 0; i < array->size; i++) {
  276. if (array->array[i].cookie == *desc->dir_cookie) {
  277. struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
  278. struct nfs_open_dir_context *ctx = desc->file->private_data;
  279. new_pos = desc->current_index + i;
  280. if (ctx->attr_gencount != nfsi->attr_gencount ||
  281. !nfs_readdir_inode_mapping_valid(nfsi)) {
  282. ctx->duped = 0;
  283. ctx->attr_gencount = nfsi->attr_gencount;
  284. } else if (new_pos < desc->ctx->pos) {
  285. if (ctx->duped > 0
  286. && ctx->dup_cookie == *desc->dir_cookie) {
  287. if (printk_ratelimit()) {
  288. pr_notice("NFS: directory %pD2 contains a readdir loop."
  289. "Please contact your server vendor. "
  290. "The file: %.*s has duplicate cookie %llu\n",
  291. desc->file, array->array[i].string.len,
  292. array->array[i].string.name, *desc->dir_cookie);
  293. }
  294. status = -ELOOP;
  295. goto out;
  296. }
  297. ctx->dup_cookie = *desc->dir_cookie;
  298. ctx->duped = -1;
  299. }
  300. desc->ctx->pos = new_pos;
  301. desc->cache_entry_index = i;
  302. return 0;
  303. }
  304. }
  305. if (array->eof_index >= 0) {
  306. status = -EBADCOOKIE;
  307. if (*desc->dir_cookie == array->last_cookie)
  308. desc->eof = 1;
  309. }
  310. out:
  311. return status;
  312. }
  313. static
  314. int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
  315. {
  316. struct nfs_cache_array *array;
  317. int status;
  318. array = nfs_readdir_get_array(desc->page);
  319. if (IS_ERR(array)) {
  320. status = PTR_ERR(array);
  321. goto out;
  322. }
  323. if (*desc->dir_cookie == 0)
  324. status = nfs_readdir_search_for_pos(array, desc);
  325. else
  326. status = nfs_readdir_search_for_cookie(array, desc);
  327. if (status == -EAGAIN) {
  328. desc->last_cookie = array->last_cookie;
  329. desc->current_index += array->size;
  330. desc->page_index++;
  331. }
  332. nfs_readdir_release_array(desc->page);
  333. out:
  334. return status;
  335. }
  336. /* Fill a page with xdr information before transferring to the cache page */
  337. static
  338. int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
  339. struct nfs_entry *entry, struct file *file, struct inode *inode)
  340. {
  341. struct nfs_open_dir_context *ctx = file->private_data;
  342. struct rpc_cred *cred = ctx->cred;
  343. unsigned long timestamp, gencount;
  344. int error;
  345. again:
  346. timestamp = jiffies;
  347. gencount = nfs_inc_attr_generation_counter();
  348. error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
  349. NFS_SERVER(inode)->dtsize, desc->plus);
  350. if (error < 0) {
  351. /* We requested READDIRPLUS, but the server doesn't grok it */
  352. if (error == -ENOTSUPP && desc->plus) {
  353. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  354. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  355. desc->plus = 0;
  356. goto again;
  357. }
  358. goto error;
  359. }
  360. desc->timestamp = timestamp;
  361. desc->gencount = gencount;
  362. error:
  363. return error;
  364. }
  365. static int xdr_decode(nfs_readdir_descriptor_t *desc,
  366. struct nfs_entry *entry, struct xdr_stream *xdr)
  367. {
  368. int error;
  369. error = desc->decode(xdr, entry, desc->plus);
  370. if (error)
  371. return error;
  372. entry->fattr->time_start = desc->timestamp;
  373. entry->fattr->gencount = desc->gencount;
  374. return 0;
  375. }
  376. /* Match file and dirent using either filehandle or fileid
  377. * Note: caller is responsible for checking the fsid
  378. */
  379. static
  380. int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
  381. {
  382. struct inode *inode;
  383. struct nfs_inode *nfsi;
  384. if (d_really_is_negative(dentry))
  385. return 0;
  386. inode = d_inode(dentry);
  387. if (is_bad_inode(inode) || NFS_STALE(inode))
  388. return 0;
  389. nfsi = NFS_I(inode);
  390. if (entry->fattr->fileid != nfsi->fileid)
  391. return 0;
  392. if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
  393. return 0;
  394. return 1;
  395. }
  396. static
  397. bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
  398. {
  399. if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
  400. return false;
  401. if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
  402. return true;
  403. if (ctx->pos == 0)
  404. return true;
  405. return false;
  406. }
  407. /*
  408. * This function is called by the lookup code to request the use of
  409. * readdirplus to accelerate any future lookups in the same
  410. * directory.
  411. */
  412. static
  413. void nfs_advise_use_readdirplus(struct inode *dir)
  414. {
  415. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
  416. }
  417. /*
  418. * This function is mainly for use by nfs_getattr().
  419. *
  420. * If this is an 'ls -l', we want to force use of readdirplus.
  421. * Do this by checking if there is an active file descriptor
  422. * and calling nfs_advise_use_readdirplus, then forcing a
  423. * cache flush.
  424. */
  425. void nfs_force_use_readdirplus(struct inode *dir)
  426. {
  427. if (!list_empty(&NFS_I(dir)->open_files)) {
  428. nfs_advise_use_readdirplus(dir);
  429. invalidate_mapping_pages(dir->i_mapping, 0, -1);
  430. }
  431. }
  432. static
  433. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  434. {
  435. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  436. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  437. struct dentry *dentry;
  438. struct dentry *alias;
  439. struct inode *dir = d_inode(parent);
  440. struct inode *inode;
  441. int status;
  442. if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
  443. return;
  444. if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
  445. return;
  446. if (filename.len == 0)
  447. return;
  448. /* Validate that the name doesn't contain any illegal '\0' */
  449. if (strnlen(filename.name, filename.len) != filename.len)
  450. return;
  451. /* ...or '/' */
  452. if (strnchr(filename.name, filename.len, '/'))
  453. return;
  454. if (filename.name[0] == '.') {
  455. if (filename.len == 1)
  456. return;
  457. if (filename.len == 2 && filename.name[1] == '.')
  458. return;
  459. }
  460. filename.hash = full_name_hash(parent, filename.name, filename.len);
  461. dentry = d_lookup(parent, &filename);
  462. again:
  463. if (!dentry) {
  464. dentry = d_alloc_parallel(parent, &filename, &wq);
  465. if (IS_ERR(dentry))
  466. return;
  467. }
  468. if (!d_in_lookup(dentry)) {
  469. /* Is there a mountpoint here? If so, just exit */
  470. if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
  471. &entry->fattr->fsid))
  472. goto out;
  473. if (nfs_same_file(dentry, entry)) {
  474. if (!entry->fh->size)
  475. goto out;
  476. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  477. status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
  478. if (!status)
  479. nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
  480. goto out;
  481. } else {
  482. d_invalidate(dentry);
  483. dput(dentry);
  484. dentry = NULL;
  485. goto again;
  486. }
  487. }
  488. if (!entry->fh->size) {
  489. d_lookup_done(dentry);
  490. goto out;
  491. }
  492. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
  493. alias = d_splice_alias(inode, dentry);
  494. d_lookup_done(dentry);
  495. if (alias) {
  496. if (IS_ERR(alias))
  497. goto out;
  498. dput(dentry);
  499. dentry = alias;
  500. }
  501. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  502. out:
  503. dput(dentry);
  504. }
  505. /* Perform conversion from xdr to cache array */
  506. static
  507. int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
  508. struct page **xdr_pages, struct page *page, unsigned int buflen)
  509. {
  510. struct xdr_stream stream;
  511. struct xdr_buf buf;
  512. struct page *scratch;
  513. struct nfs_cache_array *array;
  514. unsigned int count = 0;
  515. int status;
  516. scratch = alloc_page(GFP_KERNEL);
  517. if (scratch == NULL)
  518. return -ENOMEM;
  519. if (buflen == 0)
  520. goto out_nopages;
  521. xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
  522. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  523. do {
  524. status = xdr_decode(desc, entry, &stream);
  525. if (status != 0) {
  526. if (status == -EAGAIN)
  527. status = 0;
  528. break;
  529. }
  530. count++;
  531. if (desc->plus != 0)
  532. nfs_prime_dcache(file_dentry(desc->file), entry);
  533. status = nfs_readdir_add_to_array(entry, page);
  534. if (status != 0)
  535. break;
  536. } while (!entry->eof);
  537. out_nopages:
  538. if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
  539. array = nfs_readdir_get_array(page);
  540. if (!IS_ERR(array)) {
  541. array->eof_index = array->size;
  542. status = 0;
  543. nfs_readdir_release_array(page);
  544. } else
  545. status = PTR_ERR(array);
  546. }
  547. put_page(scratch);
  548. return status;
  549. }
  550. static
  551. void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
  552. {
  553. unsigned int i;
  554. for (i = 0; i < npages; i++)
  555. put_page(pages[i]);
  556. }
  557. /*
  558. * nfs_readdir_large_page will allocate pages that must be freed with a call
  559. * to nfs_readdir_free_pagearray
  560. */
  561. static
  562. int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
  563. {
  564. unsigned int i;
  565. for (i = 0; i < npages; i++) {
  566. struct page *page = alloc_page(GFP_KERNEL);
  567. if (page == NULL)
  568. goto out_freepages;
  569. pages[i] = page;
  570. }
  571. return 0;
  572. out_freepages:
  573. nfs_readdir_free_pages(pages, i);
  574. return -ENOMEM;
  575. }
  576. static
  577. int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
  578. {
  579. struct page *pages[NFS_MAX_READDIR_PAGES];
  580. struct nfs_entry entry;
  581. struct file *file = desc->file;
  582. struct nfs_cache_array *array;
  583. int status = -ENOMEM;
  584. unsigned int array_size = ARRAY_SIZE(pages);
  585. entry.prev_cookie = 0;
  586. entry.cookie = desc->last_cookie;
  587. entry.eof = 0;
  588. entry.fh = nfs_alloc_fhandle();
  589. entry.fattr = nfs_alloc_fattr();
  590. entry.server = NFS_SERVER(inode);
  591. if (entry.fh == NULL || entry.fattr == NULL)
  592. goto out;
  593. entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  594. if (IS_ERR(entry.label)) {
  595. status = PTR_ERR(entry.label);
  596. goto out;
  597. }
  598. array = nfs_readdir_get_array(page);
  599. if (IS_ERR(array)) {
  600. status = PTR_ERR(array);
  601. goto out_label_free;
  602. }
  603. memset(array, 0, sizeof(struct nfs_cache_array));
  604. atomic_set(&array->refcount, 1);
  605. array->eof_index = -1;
  606. status = nfs_readdir_alloc_pages(pages, array_size);
  607. if (status < 0)
  608. goto out_release_array;
  609. do {
  610. unsigned int pglen;
  611. status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
  612. if (status < 0)
  613. break;
  614. pglen = status;
  615. status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
  616. if (status < 0) {
  617. if (status == -ENOSPC)
  618. status = 0;
  619. break;
  620. }
  621. } while (array->eof_index < 0);
  622. nfs_readdir_free_pages(pages, array_size);
  623. out_release_array:
  624. nfs_readdir_release_array(page);
  625. out_label_free:
  626. nfs4_label_free(entry.label);
  627. out:
  628. nfs_free_fattr(entry.fattr);
  629. nfs_free_fhandle(entry.fh);
  630. return status;
  631. }
  632. /*
  633. * Now we cache directories properly, by converting xdr information
  634. * to an array that can be used for lookups later. This results in
  635. * fewer cache pages, since we can store more information on each page.
  636. * We only need to convert from xdr once so future lookups are much simpler
  637. */
  638. static
  639. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
  640. {
  641. struct inode *inode = file_inode(desc->file);
  642. int ret;
  643. ret = nfs_readdir_xdr_to_array(desc, page, inode);
  644. if (ret < 0)
  645. goto error;
  646. SetPageUptodate(page);
  647. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  648. /* Should never happen */
  649. nfs_zap_mapping(inode, inode->i_mapping);
  650. }
  651. unlock_page(page);
  652. return 0;
  653. error:
  654. unlock_page(page);
  655. return ret;
  656. }
  657. static
  658. void cache_page_release(nfs_readdir_descriptor_t *desc)
  659. {
  660. nfs_readdir_clear_array(desc->page);
  661. put_page(desc->page);
  662. desc->page = NULL;
  663. }
  664. static
  665. struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
  666. {
  667. struct page *page;
  668. for (;;) {
  669. page = read_cache_page(desc->file->f_mapping,
  670. desc->page_index, (filler_t *)nfs_readdir_filler, desc);
  671. if (IS_ERR(page) || grab_page(page))
  672. break;
  673. put_page(page);
  674. }
  675. return page;
  676. }
  677. /*
  678. * Returns 0 if desc->dir_cookie was found on page desc->page_index
  679. */
  680. static
  681. int find_cache_page(nfs_readdir_descriptor_t *desc)
  682. {
  683. int res;
  684. desc->page = get_cache_page(desc);
  685. if (IS_ERR(desc->page))
  686. return PTR_ERR(desc->page);
  687. res = nfs_readdir_search_array(desc);
  688. if (res != 0)
  689. cache_page_release(desc);
  690. return res;
  691. }
  692. /* Search for desc->dir_cookie from the beginning of the page cache */
  693. static inline
  694. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  695. {
  696. int res;
  697. if (desc->page_index == 0) {
  698. desc->current_index = 0;
  699. desc->last_cookie = 0;
  700. }
  701. do {
  702. res = find_cache_page(desc);
  703. } while (res == -EAGAIN);
  704. return res;
  705. }
  706. /*
  707. * Once we've found the start of the dirent within a page: fill 'er up...
  708. */
  709. static
  710. int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
  711. {
  712. struct file *file = desc->file;
  713. int i = 0;
  714. int res = 0;
  715. struct nfs_cache_array *array = NULL;
  716. struct nfs_open_dir_context *ctx = file->private_data;
  717. array = nfs_readdir_get_array(desc->page);
  718. if (IS_ERR(array)) {
  719. res = PTR_ERR(array);
  720. goto out;
  721. }
  722. for (i = desc->cache_entry_index; i < array->size; i++) {
  723. struct nfs_cache_array_entry *ent;
  724. ent = &array->array[i];
  725. if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
  726. nfs_compat_user_ino64(ent->ino), ent->d_type)) {
  727. desc->eof = 1;
  728. break;
  729. }
  730. desc->ctx->pos++;
  731. if (i < (array->size-1))
  732. *desc->dir_cookie = array->array[i+1].cookie;
  733. else
  734. *desc->dir_cookie = array->last_cookie;
  735. if (ctx->duped != 0)
  736. ctx->duped = 1;
  737. }
  738. if (array->eof_index >= 0)
  739. desc->eof = 1;
  740. nfs_readdir_release_array(desc->page);
  741. out:
  742. cache_page_release(desc);
  743. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  744. (unsigned long long)*desc->dir_cookie, res);
  745. return res;
  746. }
  747. /*
  748. * If we cannot find a cookie in our cache, we suspect that this is
  749. * because it points to a deleted file, so we ask the server to return
  750. * whatever it thinks is the next entry. We then feed this to filldir.
  751. * If all goes well, we should then be able to find our way round the
  752. * cache on the next call to readdir_search_pagecache();
  753. *
  754. * NOTE: we cannot add the anonymous page to the pagecache because
  755. * the data it contains might not be page aligned. Besides,
  756. * we should already have a complete representation of the
  757. * directory in the page cache by the time we get here.
  758. */
  759. static inline
  760. int uncached_readdir(nfs_readdir_descriptor_t *desc)
  761. {
  762. struct page *page = NULL;
  763. int status;
  764. struct inode *inode = file_inode(desc->file);
  765. struct nfs_open_dir_context *ctx = desc->file->private_data;
  766. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  767. (unsigned long long)*desc->dir_cookie);
  768. page = alloc_page(GFP_HIGHUSER);
  769. if (!page) {
  770. status = -ENOMEM;
  771. goto out;
  772. }
  773. desc->page_index = 0;
  774. desc->last_cookie = *desc->dir_cookie;
  775. desc->page = page;
  776. ctx->duped = 0;
  777. status = nfs_readdir_xdr_to_array(desc, page, inode);
  778. if (status < 0)
  779. goto out_release;
  780. status = nfs_do_filldir(desc);
  781. out:
  782. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  783. __func__, status);
  784. return status;
  785. out_release:
  786. cache_page_release(desc);
  787. goto out;
  788. }
  789. /* The file offset position represents the dirent entry number. A
  790. last cookie cache takes care of the common case of reading the
  791. whole directory.
  792. */
  793. static int nfs_readdir(struct file *file, struct dir_context *ctx)
  794. {
  795. struct dentry *dentry = file_dentry(file);
  796. struct inode *inode = d_inode(dentry);
  797. nfs_readdir_descriptor_t my_desc,
  798. *desc = &my_desc;
  799. struct nfs_open_dir_context *dir_ctx = file->private_data;
  800. int res = 0;
  801. dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
  802. file, (long long)ctx->pos);
  803. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  804. /*
  805. * ctx->pos points to the dirent entry number.
  806. * *desc->dir_cookie has the cookie for the next entry. We have
  807. * to either find the entry with the appropriate number or
  808. * revalidate the cookie.
  809. */
  810. memset(desc, 0, sizeof(*desc));
  811. desc->file = file;
  812. desc->ctx = ctx;
  813. desc->dir_cookie = &dir_ctx->dir_cookie;
  814. desc->decode = NFS_PROTO(inode)->decode_dirent;
  815. desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
  816. if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
  817. res = nfs_revalidate_mapping(inode, file->f_mapping);
  818. if (res < 0)
  819. goto out;
  820. do {
  821. res = readdir_search_pagecache(desc);
  822. if (res == -EBADCOOKIE) {
  823. res = 0;
  824. /* This means either end of directory */
  825. if (*desc->dir_cookie && desc->eof == 0) {
  826. /* Or that the server has 'lost' a cookie */
  827. res = uncached_readdir(desc);
  828. if (res == 0)
  829. continue;
  830. }
  831. break;
  832. }
  833. if (res == -ETOOSMALL && desc->plus) {
  834. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  835. nfs_zap_caches(inode);
  836. desc->page_index = 0;
  837. desc->plus = 0;
  838. desc->eof = 0;
  839. continue;
  840. }
  841. if (res < 0)
  842. break;
  843. res = nfs_do_filldir(desc);
  844. if (res < 0)
  845. break;
  846. } while (!desc->eof);
  847. out:
  848. if (res > 0)
  849. res = 0;
  850. dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
  851. return res;
  852. }
  853. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
  854. {
  855. struct nfs_open_dir_context *dir_ctx = filp->private_data;
  856. dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
  857. filp, offset, whence);
  858. switch (whence) {
  859. case 1:
  860. offset += filp->f_pos;
  861. case 0:
  862. if (offset >= 0)
  863. break;
  864. default:
  865. return -EINVAL;
  866. }
  867. if (offset != filp->f_pos) {
  868. filp->f_pos = offset;
  869. dir_ctx->dir_cookie = 0;
  870. dir_ctx->duped = 0;
  871. }
  872. return offset;
  873. }
  874. /*
  875. * All directory operations under NFS are synchronous, so fsync()
  876. * is a dummy operation.
  877. */
  878. static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
  879. int datasync)
  880. {
  881. struct inode *inode = file_inode(filp);
  882. dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
  883. inode_lock(inode);
  884. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  885. inode_unlock(inode);
  886. return 0;
  887. }
  888. /**
  889. * nfs_force_lookup_revalidate - Mark the directory as having changed
  890. * @dir - pointer to directory inode
  891. *
  892. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  893. * full lookup on all child dentries of 'dir' whenever a change occurs
  894. * on the server that might have invalidated our dcache.
  895. *
  896. * The caller should be holding dir->i_lock
  897. */
  898. void nfs_force_lookup_revalidate(struct inode *dir)
  899. {
  900. NFS_I(dir)->cache_change_attribute++;
  901. }
  902. EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
  903. /*
  904. * A check for whether or not the parent directory has changed.
  905. * In the case it has, we assume that the dentries are untrustworthy
  906. * and may need to be looked up again.
  907. * If rcu_walk prevents us from performing a full check, return 0.
  908. */
  909. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
  910. int rcu_walk)
  911. {
  912. int ret;
  913. if (IS_ROOT(dentry))
  914. return 1;
  915. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  916. return 0;
  917. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  918. return 0;
  919. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  920. if (rcu_walk)
  921. ret = nfs_revalidate_inode_rcu(NFS_SERVER(dir), dir);
  922. else
  923. ret = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  924. if (ret < 0)
  925. return 0;
  926. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  927. return 0;
  928. return 1;
  929. }
  930. /*
  931. * Use intent information to check whether or not we're going to do
  932. * an O_EXCL create using this path component.
  933. */
  934. static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
  935. {
  936. if (NFS_PROTO(dir)->version == 2)
  937. return 0;
  938. return flags & LOOKUP_EXCL;
  939. }
  940. /*
  941. * Inode and filehandle revalidation for lookups.
  942. *
  943. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  944. * or if the intent information indicates that we're about to open this
  945. * particular file and the "nocto" mount flag is not set.
  946. *
  947. */
  948. static
  949. int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
  950. {
  951. struct nfs_server *server = NFS_SERVER(inode);
  952. int ret;
  953. if (IS_AUTOMOUNT(inode))
  954. return 0;
  955. /* VFS wants an on-the-wire revalidation */
  956. if (flags & LOOKUP_REVAL)
  957. goto out_force;
  958. /* This is an open(2) */
  959. if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
  960. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  961. goto out_force;
  962. out:
  963. return (inode->i_nlink == 0) ? -ENOENT : 0;
  964. out_force:
  965. if (flags & LOOKUP_RCU)
  966. return -ECHILD;
  967. ret = __nfs_revalidate_inode(server, inode);
  968. if (ret != 0)
  969. return ret;
  970. goto out;
  971. }
  972. /*
  973. * We judge how long we want to trust negative
  974. * dentries by looking at the parent inode mtime.
  975. *
  976. * If parent mtime has changed, we revalidate, else we wait for a
  977. * period corresponding to the parent's attribute cache timeout value.
  978. *
  979. * If LOOKUP_RCU prevents us from performing a full check, return 1
  980. * suggesting a reval is needed.
  981. */
  982. static inline
  983. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  984. unsigned int flags)
  985. {
  986. /* Don't revalidate a negative dentry if we're creating a new file */
  987. if (flags & LOOKUP_CREATE)
  988. return 0;
  989. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  990. return 1;
  991. return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
  992. }
  993. /*
  994. * This is called every time the dcache has a lookup hit,
  995. * and we should check whether we can really trust that
  996. * lookup.
  997. *
  998. * NOTE! The hit can be a negative hit too, don't assume
  999. * we have an inode!
  1000. *
  1001. * If the parent directory is seen to have changed, we throw out the
  1002. * cached dentry and do a new lookup.
  1003. */
  1004. static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  1005. {
  1006. struct inode *dir;
  1007. struct inode *inode;
  1008. struct dentry *parent;
  1009. struct nfs_fh *fhandle = NULL;
  1010. struct nfs_fattr *fattr = NULL;
  1011. struct nfs4_label *label = NULL;
  1012. int error;
  1013. if (flags & LOOKUP_RCU) {
  1014. parent = ACCESS_ONCE(dentry->d_parent);
  1015. dir = d_inode_rcu(parent);
  1016. if (!dir)
  1017. return -ECHILD;
  1018. } else {
  1019. parent = dget_parent(dentry);
  1020. dir = d_inode(parent);
  1021. }
  1022. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  1023. inode = d_inode(dentry);
  1024. if (!inode) {
  1025. if (nfs_neg_need_reval(dir, dentry, flags)) {
  1026. if (flags & LOOKUP_RCU)
  1027. return -ECHILD;
  1028. goto out_bad;
  1029. }
  1030. goto out_valid_noent;
  1031. }
  1032. if (is_bad_inode(inode)) {
  1033. if (flags & LOOKUP_RCU)
  1034. return -ECHILD;
  1035. dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
  1036. __func__, dentry);
  1037. goto out_bad;
  1038. }
  1039. if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
  1040. goto out_set_verifier;
  1041. /* Force a full look up iff the parent directory has changed */
  1042. if (!nfs_is_exclusive_create(dir, flags) &&
  1043. nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
  1044. error = nfs_lookup_verify_inode(inode, flags);
  1045. if (error) {
  1046. if (flags & LOOKUP_RCU)
  1047. return -ECHILD;
  1048. if (error == -ESTALE)
  1049. goto out_zap_parent;
  1050. goto out_error;
  1051. }
  1052. goto out_valid;
  1053. }
  1054. if (flags & LOOKUP_RCU)
  1055. return -ECHILD;
  1056. if (NFS_STALE(inode))
  1057. goto out_bad;
  1058. error = -ENOMEM;
  1059. fhandle = nfs_alloc_fhandle();
  1060. fattr = nfs_alloc_fattr();
  1061. if (fhandle == NULL || fattr == NULL)
  1062. goto out_error;
  1063. label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
  1064. if (IS_ERR(label))
  1065. goto out_error;
  1066. trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
  1067. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
  1068. trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
  1069. if (error == -ESTALE || error == -ENOENT)
  1070. goto out_bad;
  1071. if (error)
  1072. goto out_error;
  1073. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  1074. goto out_bad;
  1075. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  1076. goto out_bad;
  1077. nfs_setsecurity(inode, fattr, label);
  1078. nfs_free_fattr(fattr);
  1079. nfs_free_fhandle(fhandle);
  1080. nfs4_label_free(label);
  1081. out_set_verifier:
  1082. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1083. out_valid:
  1084. /* Success: notify readdir to use READDIRPLUS */
  1085. nfs_advise_use_readdirplus(dir);
  1086. out_valid_noent:
  1087. if (flags & LOOKUP_RCU) {
  1088. if (parent != ACCESS_ONCE(dentry->d_parent))
  1089. return -ECHILD;
  1090. } else
  1091. dput(parent);
  1092. dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
  1093. __func__, dentry);
  1094. return 1;
  1095. out_zap_parent:
  1096. nfs_zap_caches(dir);
  1097. out_bad:
  1098. WARN_ON(flags & LOOKUP_RCU);
  1099. nfs_free_fattr(fattr);
  1100. nfs_free_fhandle(fhandle);
  1101. nfs4_label_free(label);
  1102. nfs_mark_for_revalidate(dir);
  1103. if (inode && S_ISDIR(inode->i_mode)) {
  1104. /* Purge readdir caches. */
  1105. nfs_zap_caches(inode);
  1106. /*
  1107. * We can't d_drop the root of a disconnected tree:
  1108. * its d_hash is on the s_anon list and d_drop() would hide
  1109. * it from shrink_dcache_for_unmount(), leading to busy
  1110. * inodes on unmount and further oopses.
  1111. */
  1112. if (IS_ROOT(dentry))
  1113. goto out_valid;
  1114. }
  1115. dput(parent);
  1116. dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
  1117. __func__, dentry);
  1118. return 0;
  1119. out_error:
  1120. WARN_ON(flags & LOOKUP_RCU);
  1121. nfs_free_fattr(fattr);
  1122. nfs_free_fhandle(fhandle);
  1123. nfs4_label_free(label);
  1124. dput(parent);
  1125. dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
  1126. __func__, dentry, error);
  1127. return error;
  1128. }
  1129. /*
  1130. * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
  1131. * when we don't really care about the dentry name. This is called when a
  1132. * pathwalk ends on a dentry that was not found via a normal lookup in the
  1133. * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
  1134. *
  1135. * In this situation, we just want to verify that the inode itself is OK
  1136. * since the dentry might have changed on the server.
  1137. */
  1138. static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
  1139. {
  1140. int error;
  1141. struct inode *inode = d_inode(dentry);
  1142. /*
  1143. * I believe we can only get a negative dentry here in the case of a
  1144. * procfs-style symlink. Just assume it's correct for now, but we may
  1145. * eventually need to do something more here.
  1146. */
  1147. if (!inode) {
  1148. dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
  1149. __func__, dentry);
  1150. return 1;
  1151. }
  1152. if (is_bad_inode(inode)) {
  1153. dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
  1154. __func__, dentry);
  1155. return 0;
  1156. }
  1157. error = nfs_lookup_verify_inode(inode, flags);
  1158. dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
  1159. __func__, inode->i_ino, error ? "invalid" : "valid");
  1160. return !error;
  1161. }
  1162. /*
  1163. * This is called from dput() when d_count is going to 0.
  1164. */
  1165. static int nfs_dentry_delete(const struct dentry *dentry)
  1166. {
  1167. dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
  1168. dentry, dentry->d_flags);
  1169. /* Unhash any dentry with a stale inode */
  1170. if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
  1171. return 1;
  1172. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1173. /* Unhash it, so that ->d_iput() would be called */
  1174. return 1;
  1175. }
  1176. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  1177. /* Unhash it, so that ancestors of killed async unlink
  1178. * files will be cleaned up during umount */
  1179. return 1;
  1180. }
  1181. return 0;
  1182. }
  1183. /* Ensure that we revalidate inode->i_nlink */
  1184. static void nfs_drop_nlink(struct inode *inode)
  1185. {
  1186. spin_lock(&inode->i_lock);
  1187. /* drop the inode if we're reasonably sure this is the last link */
  1188. if (inode->i_nlink == 1)
  1189. clear_nlink(inode);
  1190. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  1191. spin_unlock(&inode->i_lock);
  1192. }
  1193. /*
  1194. * Called when the dentry loses inode.
  1195. * We use it to clean up silly-renamed files.
  1196. */
  1197. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  1198. {
  1199. if (S_ISDIR(inode->i_mode))
  1200. /* drop any readdir cache as it could easily be old */
  1201. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  1202. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1203. nfs_complete_unlink(dentry, inode);
  1204. nfs_drop_nlink(inode);
  1205. }
  1206. iput(inode);
  1207. }
  1208. static void nfs_d_release(struct dentry *dentry)
  1209. {
  1210. /* free cached devname value, if it survived that far */
  1211. if (unlikely(dentry->d_fsdata)) {
  1212. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1213. WARN_ON(1);
  1214. else
  1215. kfree(dentry->d_fsdata);
  1216. }
  1217. }
  1218. const struct dentry_operations nfs_dentry_operations = {
  1219. .d_revalidate = nfs_lookup_revalidate,
  1220. .d_weak_revalidate = nfs_weak_revalidate,
  1221. .d_delete = nfs_dentry_delete,
  1222. .d_iput = nfs_dentry_iput,
  1223. .d_automount = nfs_d_automount,
  1224. .d_release = nfs_d_release,
  1225. };
  1226. EXPORT_SYMBOL_GPL(nfs_dentry_operations);
  1227. struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
  1228. {
  1229. struct dentry *res;
  1230. struct inode *inode = NULL;
  1231. struct nfs_fh *fhandle = NULL;
  1232. struct nfs_fattr *fattr = NULL;
  1233. struct nfs4_label *label = NULL;
  1234. int error;
  1235. dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
  1236. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  1237. if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
  1238. return ERR_PTR(-ENAMETOOLONG);
  1239. /*
  1240. * If we're doing an exclusive create, optimize away the lookup
  1241. * but don't hash the dentry.
  1242. */
  1243. if (nfs_is_exclusive_create(dir, flags))
  1244. return NULL;
  1245. res = ERR_PTR(-ENOMEM);
  1246. fhandle = nfs_alloc_fhandle();
  1247. fattr = nfs_alloc_fattr();
  1248. if (fhandle == NULL || fattr == NULL)
  1249. goto out;
  1250. label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
  1251. if (IS_ERR(label))
  1252. goto out;
  1253. trace_nfs_lookup_enter(dir, dentry, flags);
  1254. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
  1255. if (error == -ENOENT)
  1256. goto no_entry;
  1257. if (error < 0) {
  1258. res = ERR_PTR(error);
  1259. goto out_label;
  1260. }
  1261. inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
  1262. res = ERR_CAST(inode);
  1263. if (IS_ERR(res))
  1264. goto out_label;
  1265. /* Success: notify readdir to use READDIRPLUS */
  1266. nfs_advise_use_readdirplus(dir);
  1267. no_entry:
  1268. res = d_splice_alias(inode, dentry);
  1269. if (res != NULL) {
  1270. if (IS_ERR(res))
  1271. goto out_label;
  1272. dentry = res;
  1273. }
  1274. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1275. out_label:
  1276. trace_nfs_lookup_exit(dir, dentry, flags, error);
  1277. nfs4_label_free(label);
  1278. out:
  1279. nfs_free_fattr(fattr);
  1280. nfs_free_fhandle(fhandle);
  1281. return res;
  1282. }
  1283. EXPORT_SYMBOL_GPL(nfs_lookup);
  1284. #if IS_ENABLED(CONFIG_NFS_V4)
  1285. static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
  1286. const struct dentry_operations nfs4_dentry_operations = {
  1287. .d_revalidate = nfs4_lookup_revalidate,
  1288. .d_weak_revalidate = nfs_weak_revalidate,
  1289. .d_delete = nfs_dentry_delete,
  1290. .d_iput = nfs_dentry_iput,
  1291. .d_automount = nfs_d_automount,
  1292. .d_release = nfs_d_release,
  1293. };
  1294. EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
  1295. static fmode_t flags_to_mode(int flags)
  1296. {
  1297. fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
  1298. if ((flags & O_ACCMODE) != O_WRONLY)
  1299. res |= FMODE_READ;
  1300. if ((flags & O_ACCMODE) != O_RDONLY)
  1301. res |= FMODE_WRITE;
  1302. return res;
  1303. }
  1304. static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
  1305. {
  1306. return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
  1307. }
  1308. static int do_open(struct inode *inode, struct file *filp)
  1309. {
  1310. nfs_fscache_open_file(inode, filp);
  1311. return 0;
  1312. }
  1313. static int nfs_finish_open(struct nfs_open_context *ctx,
  1314. struct dentry *dentry,
  1315. struct file *file, unsigned open_flags,
  1316. int *opened)
  1317. {
  1318. int err;
  1319. err = finish_open(file, dentry, do_open, opened);
  1320. if (err)
  1321. goto out;
  1322. nfs_file_set_open_context(file, ctx);
  1323. out:
  1324. return err;
  1325. }
  1326. int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  1327. struct file *file, unsigned open_flags,
  1328. umode_t mode, int *opened)
  1329. {
  1330. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1331. struct nfs_open_context *ctx;
  1332. struct dentry *res;
  1333. struct iattr attr = { .ia_valid = ATTR_OPEN };
  1334. struct inode *inode;
  1335. unsigned int lookup_flags = 0;
  1336. bool switched = false;
  1337. int err;
  1338. /* Expect a negative dentry */
  1339. BUG_ON(d_inode(dentry));
  1340. dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
  1341. dir->i_sb->s_id, dir->i_ino, dentry);
  1342. err = nfs_check_flags(open_flags);
  1343. if (err)
  1344. return err;
  1345. /* NFS only supports OPEN on regular files */
  1346. if ((open_flags & O_DIRECTORY)) {
  1347. if (!d_in_lookup(dentry)) {
  1348. /*
  1349. * Hashed negative dentry with O_DIRECTORY: dentry was
  1350. * revalidated and is fine, no need to perform lookup
  1351. * again
  1352. */
  1353. return -ENOENT;
  1354. }
  1355. lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
  1356. goto no_open;
  1357. }
  1358. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  1359. return -ENAMETOOLONG;
  1360. if (open_flags & O_CREAT) {
  1361. attr.ia_valid |= ATTR_MODE;
  1362. attr.ia_mode = mode & ~current_umask();
  1363. }
  1364. if (open_flags & O_TRUNC) {
  1365. attr.ia_valid |= ATTR_SIZE;
  1366. attr.ia_size = 0;
  1367. }
  1368. if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
  1369. d_drop(dentry);
  1370. switched = true;
  1371. dentry = d_alloc_parallel(dentry->d_parent,
  1372. &dentry->d_name, &wq);
  1373. if (IS_ERR(dentry))
  1374. return PTR_ERR(dentry);
  1375. if (unlikely(!d_in_lookup(dentry)))
  1376. return finish_no_open(file, dentry);
  1377. }
  1378. ctx = create_nfs_open_context(dentry, open_flags);
  1379. err = PTR_ERR(ctx);
  1380. if (IS_ERR(ctx))
  1381. goto out;
  1382. trace_nfs_atomic_open_enter(dir, ctx, open_flags);
  1383. inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
  1384. if (IS_ERR(inode)) {
  1385. err = PTR_ERR(inode);
  1386. trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
  1387. put_nfs_open_context(ctx);
  1388. d_drop(dentry);
  1389. switch (err) {
  1390. case -ENOENT:
  1391. d_add(dentry, NULL);
  1392. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1393. break;
  1394. case -EISDIR:
  1395. case -ENOTDIR:
  1396. goto no_open;
  1397. case -ELOOP:
  1398. if (!(open_flags & O_NOFOLLOW))
  1399. goto no_open;
  1400. break;
  1401. /* case -EINVAL: */
  1402. default:
  1403. break;
  1404. }
  1405. goto out;
  1406. }
  1407. err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
  1408. trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
  1409. put_nfs_open_context(ctx);
  1410. out:
  1411. if (unlikely(switched)) {
  1412. d_lookup_done(dentry);
  1413. dput(dentry);
  1414. }
  1415. return err;
  1416. no_open:
  1417. res = nfs_lookup(dir, dentry, lookup_flags);
  1418. if (switched) {
  1419. d_lookup_done(dentry);
  1420. if (!res)
  1421. res = dentry;
  1422. else
  1423. dput(dentry);
  1424. }
  1425. if (IS_ERR(res))
  1426. return PTR_ERR(res);
  1427. return finish_no_open(file, res);
  1428. }
  1429. EXPORT_SYMBOL_GPL(nfs_atomic_open);
  1430. static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  1431. {
  1432. struct inode *inode;
  1433. int ret = 0;
  1434. if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
  1435. goto no_open;
  1436. if (d_mountpoint(dentry))
  1437. goto no_open;
  1438. if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
  1439. goto no_open;
  1440. inode = d_inode(dentry);
  1441. /* We can't create new files in nfs_open_revalidate(), so we
  1442. * optimize away revalidation of negative dentries.
  1443. */
  1444. if (inode == NULL) {
  1445. struct dentry *parent;
  1446. struct inode *dir;
  1447. if (flags & LOOKUP_RCU) {
  1448. parent = ACCESS_ONCE(dentry->d_parent);
  1449. dir = d_inode_rcu(parent);
  1450. if (!dir)
  1451. return -ECHILD;
  1452. } else {
  1453. parent = dget_parent(dentry);
  1454. dir = d_inode(parent);
  1455. }
  1456. if (!nfs_neg_need_reval(dir, dentry, flags))
  1457. ret = 1;
  1458. else if (flags & LOOKUP_RCU)
  1459. ret = -ECHILD;
  1460. if (!(flags & LOOKUP_RCU))
  1461. dput(parent);
  1462. else if (parent != ACCESS_ONCE(dentry->d_parent))
  1463. return -ECHILD;
  1464. goto out;
  1465. }
  1466. /* NFS only supports OPEN on regular files */
  1467. if (!S_ISREG(inode->i_mode))
  1468. goto no_open;
  1469. /* We cannot do exclusive creation on a positive dentry */
  1470. if (flags & LOOKUP_EXCL)
  1471. goto no_open;
  1472. /* Let f_op->open() actually open (and revalidate) the file */
  1473. ret = 1;
  1474. out:
  1475. return ret;
  1476. no_open:
  1477. return nfs_lookup_revalidate(dentry, flags);
  1478. }
  1479. #endif /* CONFIG_NFSV4 */
  1480. /*
  1481. * Code common to create, mkdir, and mknod.
  1482. */
  1483. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1484. struct nfs_fattr *fattr,
  1485. struct nfs4_label *label)
  1486. {
  1487. struct dentry *parent = dget_parent(dentry);
  1488. struct inode *dir = d_inode(parent);
  1489. struct inode *inode;
  1490. int error = -EACCES;
  1491. d_drop(dentry);
  1492. /* We may have been initialized further down */
  1493. if (d_really_is_positive(dentry))
  1494. goto out;
  1495. if (fhandle->size == 0) {
  1496. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
  1497. if (error)
  1498. goto out_error;
  1499. }
  1500. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1501. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1502. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1503. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
  1504. if (error < 0)
  1505. goto out_error;
  1506. }
  1507. inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
  1508. error = PTR_ERR(inode);
  1509. if (IS_ERR(inode))
  1510. goto out_error;
  1511. d_add(dentry, inode);
  1512. out:
  1513. dput(parent);
  1514. return 0;
  1515. out_error:
  1516. nfs_mark_for_revalidate(dir);
  1517. dput(parent);
  1518. return error;
  1519. }
  1520. EXPORT_SYMBOL_GPL(nfs_instantiate);
  1521. /*
  1522. * Following a failed create operation, we drop the dentry rather
  1523. * than retain a negative dentry. This avoids a problem in the event
  1524. * that the operation succeeded on the server, but an error in the
  1525. * reply path made it appear to have failed.
  1526. */
  1527. int nfs_create(struct inode *dir, struct dentry *dentry,
  1528. umode_t mode, bool excl)
  1529. {
  1530. struct iattr attr;
  1531. int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
  1532. int error;
  1533. dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
  1534. dir->i_sb->s_id, dir->i_ino, dentry);
  1535. attr.ia_mode = mode;
  1536. attr.ia_valid = ATTR_MODE;
  1537. trace_nfs_create_enter(dir, dentry, open_flags);
  1538. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  1539. trace_nfs_create_exit(dir, dentry, open_flags, error);
  1540. if (error != 0)
  1541. goto out_err;
  1542. return 0;
  1543. out_err:
  1544. d_drop(dentry);
  1545. return error;
  1546. }
  1547. EXPORT_SYMBOL_GPL(nfs_create);
  1548. /*
  1549. * See comments for nfs_proc_create regarding failed operations.
  1550. */
  1551. int
  1552. nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1553. {
  1554. struct iattr attr;
  1555. int status;
  1556. dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
  1557. dir->i_sb->s_id, dir->i_ino, dentry);
  1558. attr.ia_mode = mode;
  1559. attr.ia_valid = ATTR_MODE;
  1560. trace_nfs_mknod_enter(dir, dentry);
  1561. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1562. trace_nfs_mknod_exit(dir, dentry, status);
  1563. if (status != 0)
  1564. goto out_err;
  1565. return 0;
  1566. out_err:
  1567. d_drop(dentry);
  1568. return status;
  1569. }
  1570. EXPORT_SYMBOL_GPL(nfs_mknod);
  1571. /*
  1572. * See comments for nfs_proc_create regarding failed operations.
  1573. */
  1574. int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1575. {
  1576. struct iattr attr;
  1577. int error;
  1578. dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
  1579. dir->i_sb->s_id, dir->i_ino, dentry);
  1580. attr.ia_valid = ATTR_MODE;
  1581. attr.ia_mode = mode | S_IFDIR;
  1582. trace_nfs_mkdir_enter(dir, dentry);
  1583. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1584. trace_nfs_mkdir_exit(dir, dentry, error);
  1585. if (error != 0)
  1586. goto out_err;
  1587. return 0;
  1588. out_err:
  1589. d_drop(dentry);
  1590. return error;
  1591. }
  1592. EXPORT_SYMBOL_GPL(nfs_mkdir);
  1593. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1594. {
  1595. if (simple_positive(dentry))
  1596. d_delete(dentry);
  1597. }
  1598. int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1599. {
  1600. int error;
  1601. dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
  1602. dir->i_sb->s_id, dir->i_ino, dentry);
  1603. trace_nfs_rmdir_enter(dir, dentry);
  1604. if (d_really_is_positive(dentry)) {
  1605. down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  1606. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1607. /* Ensure the VFS deletes this inode */
  1608. switch (error) {
  1609. case 0:
  1610. clear_nlink(d_inode(dentry));
  1611. break;
  1612. case -ENOENT:
  1613. nfs_dentry_handle_enoent(dentry);
  1614. }
  1615. up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  1616. } else
  1617. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1618. trace_nfs_rmdir_exit(dir, dentry, error);
  1619. return error;
  1620. }
  1621. EXPORT_SYMBOL_GPL(nfs_rmdir);
  1622. /*
  1623. * Remove a file after making sure there are no pending writes,
  1624. * and after checking that the file has only one user.
  1625. *
  1626. * We invalidate the attribute cache and free the inode prior to the operation
  1627. * to avoid possible races if the server reuses the inode.
  1628. */
  1629. static int nfs_safe_remove(struct dentry *dentry)
  1630. {
  1631. struct inode *dir = d_inode(dentry->d_parent);
  1632. struct inode *inode = d_inode(dentry);
  1633. int error = -EBUSY;
  1634. dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
  1635. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1636. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1637. error = 0;
  1638. goto out;
  1639. }
  1640. trace_nfs_remove_enter(dir, dentry);
  1641. if (inode != NULL) {
  1642. NFS_PROTO(inode)->return_delegation(inode);
  1643. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1644. if (error == 0)
  1645. nfs_drop_nlink(inode);
  1646. } else
  1647. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1648. if (error == -ENOENT)
  1649. nfs_dentry_handle_enoent(dentry);
  1650. trace_nfs_remove_exit(dir, dentry, error);
  1651. out:
  1652. return error;
  1653. }
  1654. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1655. * belongs to an active ".nfs..." file and we return -EBUSY.
  1656. *
  1657. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1658. */
  1659. int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1660. {
  1661. int error;
  1662. int need_rehash = 0;
  1663. dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
  1664. dir->i_ino, dentry);
  1665. trace_nfs_unlink_enter(dir, dentry);
  1666. spin_lock(&dentry->d_lock);
  1667. if (d_count(dentry) > 1) {
  1668. spin_unlock(&dentry->d_lock);
  1669. /* Start asynchronous writeout of the inode */
  1670. write_inode_now(d_inode(dentry), 0);
  1671. error = nfs_sillyrename(dir, dentry);
  1672. goto out;
  1673. }
  1674. if (!d_unhashed(dentry)) {
  1675. __d_drop(dentry);
  1676. need_rehash = 1;
  1677. }
  1678. spin_unlock(&dentry->d_lock);
  1679. error = nfs_safe_remove(dentry);
  1680. if (!error || error == -ENOENT) {
  1681. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1682. } else if (need_rehash)
  1683. d_rehash(dentry);
  1684. out:
  1685. trace_nfs_unlink_exit(dir, dentry, error);
  1686. return error;
  1687. }
  1688. EXPORT_SYMBOL_GPL(nfs_unlink);
  1689. /*
  1690. * To create a symbolic link, most file systems instantiate a new inode,
  1691. * add a page to it containing the path, then write it out to the disk
  1692. * using prepare_write/commit_write.
  1693. *
  1694. * Unfortunately the NFS client can't create the in-core inode first
  1695. * because it needs a file handle to create an in-core inode (see
  1696. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1697. * symlink request has completed on the server.
  1698. *
  1699. * So instead we allocate a raw page, copy the symname into it, then do
  1700. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1701. * now have a new file handle and can instantiate an in-core NFS inode
  1702. * and move the raw page into its mapping.
  1703. */
  1704. int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1705. {
  1706. struct page *page;
  1707. char *kaddr;
  1708. struct iattr attr;
  1709. unsigned int pathlen = strlen(symname);
  1710. int error;
  1711. dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
  1712. dir->i_ino, dentry, symname);
  1713. if (pathlen > PAGE_SIZE)
  1714. return -ENAMETOOLONG;
  1715. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1716. attr.ia_valid = ATTR_MODE;
  1717. page = alloc_page(GFP_USER);
  1718. if (!page)
  1719. return -ENOMEM;
  1720. kaddr = page_address(page);
  1721. memcpy(kaddr, symname, pathlen);
  1722. if (pathlen < PAGE_SIZE)
  1723. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1724. trace_nfs_symlink_enter(dir, dentry);
  1725. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1726. trace_nfs_symlink_exit(dir, dentry, error);
  1727. if (error != 0) {
  1728. dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
  1729. dir->i_sb->s_id, dir->i_ino,
  1730. dentry, symname, error);
  1731. d_drop(dentry);
  1732. __free_page(page);
  1733. return error;
  1734. }
  1735. /*
  1736. * No big deal if we can't add this page to the page cache here.
  1737. * READLINK will get the missing page from the server if needed.
  1738. */
  1739. if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
  1740. GFP_KERNEL)) {
  1741. SetPageUptodate(page);
  1742. unlock_page(page);
  1743. /*
  1744. * add_to_page_cache_lru() grabs an extra page refcount.
  1745. * Drop it here to avoid leaking this page later.
  1746. */
  1747. put_page(page);
  1748. } else
  1749. __free_page(page);
  1750. return 0;
  1751. }
  1752. EXPORT_SYMBOL_GPL(nfs_symlink);
  1753. int
  1754. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1755. {
  1756. struct inode *inode = d_inode(old_dentry);
  1757. int error;
  1758. dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
  1759. old_dentry, dentry);
  1760. trace_nfs_link_enter(inode, dir, dentry);
  1761. NFS_PROTO(inode)->return_delegation(inode);
  1762. d_drop(dentry);
  1763. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1764. if (error == 0) {
  1765. ihold(inode);
  1766. d_add(dentry, inode);
  1767. }
  1768. trace_nfs_link_exit(inode, dir, dentry, error);
  1769. return error;
  1770. }
  1771. EXPORT_SYMBOL_GPL(nfs_link);
  1772. /*
  1773. * RENAME
  1774. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1775. * different file handle for the same inode after a rename (e.g. when
  1776. * moving to a different directory). A fail-safe method to do so would
  1777. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1778. * rename the old file using the sillyrename stuff. This way, the original
  1779. * file in old_dir will go away when the last process iput()s the inode.
  1780. *
  1781. * FIXED.
  1782. *
  1783. * It actually works quite well. One needs to have the possibility for
  1784. * at least one ".nfs..." file in each directory the file ever gets
  1785. * moved or linked to which happens automagically with the new
  1786. * implementation that only depends on the dcache stuff instead of
  1787. * using the inode layer
  1788. *
  1789. * Unfortunately, things are a little more complicated than indicated
  1790. * above. For a cross-directory move, we want to make sure we can get
  1791. * rid of the old inode after the operation. This means there must be
  1792. * no pending writes (if it's a file), and the use count must be 1.
  1793. * If these conditions are met, we can drop the dentries before doing
  1794. * the rename.
  1795. */
  1796. int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1797. struct inode *new_dir, struct dentry *new_dentry,
  1798. unsigned int flags)
  1799. {
  1800. struct inode *old_inode = d_inode(old_dentry);
  1801. struct inode *new_inode = d_inode(new_dentry);
  1802. struct dentry *dentry = NULL, *rehash = NULL;
  1803. struct rpc_task *task;
  1804. int error = -EBUSY;
  1805. if (flags)
  1806. return -EINVAL;
  1807. dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
  1808. old_dentry, new_dentry,
  1809. d_count(new_dentry));
  1810. trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
  1811. /*
  1812. * For non-directories, check whether the target is busy and if so,
  1813. * make a copy of the dentry and then do a silly-rename. If the
  1814. * silly-rename succeeds, the copied dentry is hashed and becomes
  1815. * the new target.
  1816. */
  1817. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1818. /*
  1819. * To prevent any new references to the target during the
  1820. * rename, we unhash the dentry in advance.
  1821. */
  1822. if (!d_unhashed(new_dentry)) {
  1823. d_drop(new_dentry);
  1824. rehash = new_dentry;
  1825. }
  1826. if (d_count(new_dentry) > 2) {
  1827. int err;
  1828. /* copy the target dentry's name */
  1829. dentry = d_alloc(new_dentry->d_parent,
  1830. &new_dentry->d_name);
  1831. if (!dentry)
  1832. goto out;
  1833. /* silly-rename the existing target ... */
  1834. err = nfs_sillyrename(new_dir, new_dentry);
  1835. if (err)
  1836. goto out;
  1837. new_dentry = dentry;
  1838. rehash = NULL;
  1839. new_inode = NULL;
  1840. }
  1841. }
  1842. NFS_PROTO(old_inode)->return_delegation(old_inode);
  1843. if (new_inode != NULL)
  1844. NFS_PROTO(new_inode)->return_delegation(new_inode);
  1845. task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
  1846. if (IS_ERR(task)) {
  1847. error = PTR_ERR(task);
  1848. goto out;
  1849. }
  1850. error = rpc_wait_for_completion_task(task);
  1851. if (error == 0)
  1852. error = task->tk_status;
  1853. rpc_put_task(task);
  1854. nfs_mark_for_revalidate(old_inode);
  1855. out:
  1856. if (rehash)
  1857. d_rehash(rehash);
  1858. trace_nfs_rename_exit(old_dir, old_dentry,
  1859. new_dir, new_dentry, error);
  1860. if (!error) {
  1861. if (new_inode != NULL)
  1862. nfs_drop_nlink(new_inode);
  1863. d_move(old_dentry, new_dentry);
  1864. nfs_set_verifier(old_dentry,
  1865. nfs_save_change_attribute(new_dir));
  1866. } else if (error == -ENOENT)
  1867. nfs_dentry_handle_enoent(old_dentry);
  1868. /* new dentry created? */
  1869. if (dentry)
  1870. dput(dentry);
  1871. return error;
  1872. }
  1873. EXPORT_SYMBOL_GPL(nfs_rename);
  1874. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1875. static LIST_HEAD(nfs_access_lru_list);
  1876. static atomic_long_t nfs_access_nr_entries;
  1877. static unsigned long nfs_access_max_cachesize = ULONG_MAX;
  1878. module_param(nfs_access_max_cachesize, ulong, 0644);
  1879. MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
  1880. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1881. {
  1882. put_rpccred(entry->cred);
  1883. kfree_rcu(entry, rcu_head);
  1884. smp_mb__before_atomic();
  1885. atomic_long_dec(&nfs_access_nr_entries);
  1886. smp_mb__after_atomic();
  1887. }
  1888. static void nfs_access_free_list(struct list_head *head)
  1889. {
  1890. struct nfs_access_entry *cache;
  1891. while (!list_empty(head)) {
  1892. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1893. list_del(&cache->lru);
  1894. nfs_access_free_entry(cache);
  1895. }
  1896. }
  1897. static unsigned long
  1898. nfs_do_access_cache_scan(unsigned int nr_to_scan)
  1899. {
  1900. LIST_HEAD(head);
  1901. struct nfs_inode *nfsi, *next;
  1902. struct nfs_access_entry *cache;
  1903. long freed = 0;
  1904. spin_lock(&nfs_access_lru_lock);
  1905. list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
  1906. struct inode *inode;
  1907. if (nr_to_scan-- == 0)
  1908. break;
  1909. inode = &nfsi->vfs_inode;
  1910. spin_lock(&inode->i_lock);
  1911. if (list_empty(&nfsi->access_cache_entry_lru))
  1912. goto remove_lru_entry;
  1913. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1914. struct nfs_access_entry, lru);
  1915. list_move(&cache->lru, &head);
  1916. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1917. freed++;
  1918. if (!list_empty(&nfsi->access_cache_entry_lru))
  1919. list_move_tail(&nfsi->access_cache_inode_lru,
  1920. &nfs_access_lru_list);
  1921. else {
  1922. remove_lru_entry:
  1923. list_del_init(&nfsi->access_cache_inode_lru);
  1924. smp_mb__before_atomic();
  1925. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1926. smp_mb__after_atomic();
  1927. }
  1928. spin_unlock(&inode->i_lock);
  1929. }
  1930. spin_unlock(&nfs_access_lru_lock);
  1931. nfs_access_free_list(&head);
  1932. return freed;
  1933. }
  1934. unsigned long
  1935. nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
  1936. {
  1937. int nr_to_scan = sc->nr_to_scan;
  1938. gfp_t gfp_mask = sc->gfp_mask;
  1939. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1940. return SHRINK_STOP;
  1941. return nfs_do_access_cache_scan(nr_to_scan);
  1942. }
  1943. unsigned long
  1944. nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
  1945. {
  1946. return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
  1947. }
  1948. static void
  1949. nfs_access_cache_enforce_limit(void)
  1950. {
  1951. long nr_entries = atomic_long_read(&nfs_access_nr_entries);
  1952. unsigned long diff;
  1953. unsigned int nr_to_scan;
  1954. if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
  1955. return;
  1956. nr_to_scan = 100;
  1957. diff = nr_entries - nfs_access_max_cachesize;
  1958. if (diff < nr_to_scan)
  1959. nr_to_scan = diff;
  1960. nfs_do_access_cache_scan(nr_to_scan);
  1961. }
  1962. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1963. {
  1964. struct rb_root *root_node = &nfsi->access_cache;
  1965. struct rb_node *n;
  1966. struct nfs_access_entry *entry;
  1967. /* Unhook entries from the cache */
  1968. while ((n = rb_first(root_node)) != NULL) {
  1969. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1970. rb_erase(n, root_node);
  1971. list_move(&entry->lru, head);
  1972. }
  1973. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1974. }
  1975. void nfs_access_zap_cache(struct inode *inode)
  1976. {
  1977. LIST_HEAD(head);
  1978. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1979. return;
  1980. /* Remove from global LRU init */
  1981. spin_lock(&nfs_access_lru_lock);
  1982. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1983. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1984. spin_lock(&inode->i_lock);
  1985. __nfs_access_zap_cache(NFS_I(inode), &head);
  1986. spin_unlock(&inode->i_lock);
  1987. spin_unlock(&nfs_access_lru_lock);
  1988. nfs_access_free_list(&head);
  1989. }
  1990. EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
  1991. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1992. {
  1993. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1994. struct nfs_access_entry *entry;
  1995. while (n != NULL) {
  1996. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1997. if (cred < entry->cred)
  1998. n = n->rb_left;
  1999. else if (cred > entry->cred)
  2000. n = n->rb_right;
  2001. else
  2002. return entry;
  2003. }
  2004. return NULL;
  2005. }
  2006. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
  2007. {
  2008. struct nfs_inode *nfsi = NFS_I(inode);
  2009. struct nfs_access_entry *cache;
  2010. bool retry = true;
  2011. int err;
  2012. spin_lock(&inode->i_lock);
  2013. for(;;) {
  2014. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  2015. goto out_zap;
  2016. cache = nfs_access_search_rbtree(inode, cred);
  2017. err = -ENOENT;
  2018. if (cache == NULL)
  2019. goto out;
  2020. /* Found an entry, is our attribute cache valid? */
  2021. if (!nfs_attribute_cache_expired(inode) &&
  2022. !(nfsi->cache_validity & NFS_INO_INVALID_ATTR))
  2023. break;
  2024. err = -ECHILD;
  2025. if (!may_block)
  2026. goto out;
  2027. if (!retry)
  2028. goto out_zap;
  2029. spin_unlock(&inode->i_lock);
  2030. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2031. if (err)
  2032. return err;
  2033. spin_lock(&inode->i_lock);
  2034. retry = false;
  2035. }
  2036. res->jiffies = cache->jiffies;
  2037. res->cred = cache->cred;
  2038. res->mask = cache->mask;
  2039. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  2040. err = 0;
  2041. out:
  2042. spin_unlock(&inode->i_lock);
  2043. return err;
  2044. out_zap:
  2045. spin_unlock(&inode->i_lock);
  2046. nfs_access_zap_cache(inode);
  2047. return -ENOENT;
  2048. }
  2049. static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  2050. {
  2051. /* Only check the most recently returned cache entry,
  2052. * but do it without locking.
  2053. */
  2054. struct nfs_inode *nfsi = NFS_I(inode);
  2055. struct nfs_access_entry *cache;
  2056. int err = -ECHILD;
  2057. struct list_head *lh;
  2058. rcu_read_lock();
  2059. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  2060. goto out;
  2061. lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
  2062. cache = list_entry(lh, struct nfs_access_entry, lru);
  2063. if (lh == &nfsi->access_cache_entry_lru ||
  2064. cred != cache->cred)
  2065. cache = NULL;
  2066. if (cache == NULL)
  2067. goto out;
  2068. err = nfs_revalidate_inode_rcu(NFS_SERVER(inode), inode);
  2069. if (err)
  2070. goto out;
  2071. res->jiffies = cache->jiffies;
  2072. res->cred = cache->cred;
  2073. res->mask = cache->mask;
  2074. out:
  2075. rcu_read_unlock();
  2076. return err;
  2077. }
  2078. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  2079. {
  2080. struct nfs_inode *nfsi = NFS_I(inode);
  2081. struct rb_root *root_node = &nfsi->access_cache;
  2082. struct rb_node **p = &root_node->rb_node;
  2083. struct rb_node *parent = NULL;
  2084. struct nfs_access_entry *entry;
  2085. spin_lock(&inode->i_lock);
  2086. while (*p != NULL) {
  2087. parent = *p;
  2088. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  2089. if (set->cred < entry->cred)
  2090. p = &parent->rb_left;
  2091. else if (set->cred > entry->cred)
  2092. p = &parent->rb_right;
  2093. else
  2094. goto found;
  2095. }
  2096. rb_link_node(&set->rb_node, parent, p);
  2097. rb_insert_color(&set->rb_node, root_node);
  2098. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  2099. spin_unlock(&inode->i_lock);
  2100. return;
  2101. found:
  2102. rb_replace_node(parent, &set->rb_node, root_node);
  2103. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  2104. list_del(&entry->lru);
  2105. spin_unlock(&inode->i_lock);
  2106. nfs_access_free_entry(entry);
  2107. }
  2108. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  2109. {
  2110. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  2111. if (cache == NULL)
  2112. return;
  2113. RB_CLEAR_NODE(&cache->rb_node);
  2114. cache->jiffies = set->jiffies;
  2115. cache->cred = get_rpccred(set->cred);
  2116. cache->mask = set->mask;
  2117. /* The above field assignments must be visible
  2118. * before this item appears on the lru. We cannot easily
  2119. * use rcu_assign_pointer, so just force the memory barrier.
  2120. */
  2121. smp_wmb();
  2122. nfs_access_add_rbtree(inode, cache);
  2123. /* Update accounting */
  2124. smp_mb__before_atomic();
  2125. atomic_long_inc(&nfs_access_nr_entries);
  2126. smp_mb__after_atomic();
  2127. /* Add inode to global LRU list */
  2128. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  2129. spin_lock(&nfs_access_lru_lock);
  2130. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  2131. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  2132. &nfs_access_lru_list);
  2133. spin_unlock(&nfs_access_lru_lock);
  2134. }
  2135. nfs_access_cache_enforce_limit();
  2136. }
  2137. EXPORT_SYMBOL_GPL(nfs_access_add_cache);
  2138. void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
  2139. {
  2140. entry->mask = 0;
  2141. if (access_result & NFS4_ACCESS_READ)
  2142. entry->mask |= MAY_READ;
  2143. if (access_result &
  2144. (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2145. entry->mask |= MAY_WRITE;
  2146. if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2147. entry->mask |= MAY_EXEC;
  2148. }
  2149. EXPORT_SYMBOL_GPL(nfs_access_set_mask);
  2150. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  2151. {
  2152. struct nfs_access_entry cache;
  2153. bool may_block = (mask & MAY_NOT_BLOCK) == 0;
  2154. int status;
  2155. trace_nfs_access_enter(inode);
  2156. status = nfs_access_get_cached_rcu(inode, cred, &cache);
  2157. if (status != 0)
  2158. status = nfs_access_get_cached(inode, cred, &cache, may_block);
  2159. if (status == 0)
  2160. goto out_cached;
  2161. status = -ECHILD;
  2162. if (!may_block)
  2163. goto out;
  2164. /* Be clever: ask server to check for all possible rights */
  2165. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  2166. cache.cred = cred;
  2167. cache.jiffies = jiffies;
  2168. status = NFS_PROTO(inode)->access(inode, &cache);
  2169. if (status != 0) {
  2170. if (status == -ESTALE) {
  2171. nfs_zap_caches(inode);
  2172. if (!S_ISDIR(inode->i_mode))
  2173. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  2174. }
  2175. goto out;
  2176. }
  2177. nfs_access_add_cache(inode, &cache);
  2178. out_cached:
  2179. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
  2180. status = -EACCES;
  2181. out:
  2182. trace_nfs_access_exit(inode, status);
  2183. return status;
  2184. }
  2185. static int nfs_open_permission_mask(int openflags)
  2186. {
  2187. int mask = 0;
  2188. if (openflags & __FMODE_EXEC) {
  2189. /* ONLY check exec rights */
  2190. mask = MAY_EXEC;
  2191. } else {
  2192. if ((openflags & O_ACCMODE) != O_WRONLY)
  2193. mask |= MAY_READ;
  2194. if ((openflags & O_ACCMODE) != O_RDONLY)
  2195. mask |= MAY_WRITE;
  2196. }
  2197. return mask;
  2198. }
  2199. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  2200. {
  2201. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  2202. }
  2203. EXPORT_SYMBOL_GPL(nfs_may_open);
  2204. static int nfs_execute_ok(struct inode *inode, int mask)
  2205. {
  2206. struct nfs_server *server = NFS_SERVER(inode);
  2207. int ret;
  2208. if (mask & MAY_NOT_BLOCK)
  2209. ret = nfs_revalidate_inode_rcu(server, inode);
  2210. else
  2211. ret = nfs_revalidate_inode(server, inode);
  2212. if (ret == 0 && !execute_ok(inode))
  2213. ret = -EACCES;
  2214. return ret;
  2215. }
  2216. int nfs_permission(struct inode *inode, int mask)
  2217. {
  2218. struct rpc_cred *cred;
  2219. int res = 0;
  2220. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  2221. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  2222. goto out;
  2223. /* Is this sys_access() ? */
  2224. if (mask & (MAY_ACCESS | MAY_CHDIR))
  2225. goto force_lookup;
  2226. switch (inode->i_mode & S_IFMT) {
  2227. case S_IFLNK:
  2228. goto out;
  2229. case S_IFREG:
  2230. if ((mask & MAY_OPEN) &&
  2231. nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
  2232. return 0;
  2233. break;
  2234. case S_IFDIR:
  2235. /*
  2236. * Optimize away all write operations, since the server
  2237. * will check permissions when we perform the op.
  2238. */
  2239. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  2240. goto out;
  2241. }
  2242. force_lookup:
  2243. if (!NFS_PROTO(inode)->access)
  2244. goto out_notsup;
  2245. /* Always try fast lookups first */
  2246. rcu_read_lock();
  2247. cred = rpc_lookup_cred_nonblock();
  2248. if (!IS_ERR(cred))
  2249. res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
  2250. else
  2251. res = PTR_ERR(cred);
  2252. rcu_read_unlock();
  2253. if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
  2254. /* Fast lookup failed, try the slow way */
  2255. cred = rpc_lookup_cred();
  2256. if (!IS_ERR(cred)) {
  2257. res = nfs_do_access(inode, cred, mask);
  2258. put_rpccred(cred);
  2259. } else
  2260. res = PTR_ERR(cred);
  2261. }
  2262. out:
  2263. if (!res && (mask & MAY_EXEC))
  2264. res = nfs_execute_ok(inode, mask);
  2265. dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
  2266. inode->i_sb->s_id, inode->i_ino, mask, res);
  2267. return res;
  2268. out_notsup:
  2269. if (mask & MAY_NOT_BLOCK)
  2270. return -ECHILD;
  2271. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  2272. if (res == 0)
  2273. res = generic_permission(inode, mask);
  2274. goto out;
  2275. }
  2276. EXPORT_SYMBOL_GPL(nfs_permission);
  2277. /*
  2278. * Local variables:
  2279. * version-control: t
  2280. * kept-new-versions: 5
  2281. * End:
  2282. */