dir.c 57 KB

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