dir.c 56 KB

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