dir.c 60 KB

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