dir.c 57 KB

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