dir.c 59 KB

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