dir.c 63 KB

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