dir.c 57 KB

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