dir.c 59 KB

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