dir.c 64 KB

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