dir.c 56 KB

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