dir.c 63 KB

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