namei.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/namei.h>
  32. #include <asm/namei.h>
  33. #include <asm/uaccess.h>
  34. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  35. /* [Feb-1997 T. Schoebel-Theuer]
  36. * Fundamental changes in the pathname lookup mechanisms (namei)
  37. * were necessary because of omirr. The reason is that omirr needs
  38. * to know the _real_ pathname, not the user-supplied one, in case
  39. * of symlinks (and also when transname replacements occur).
  40. *
  41. * The new code replaces the old recursive symlink resolution with
  42. * an iterative one (in case of non-nested symlink chains). It does
  43. * this with calls to <fs>_follow_link().
  44. * As a side effect, dir_namei(), _namei() and follow_link() are now
  45. * replaced with a single function lookup_dentry() that can handle all
  46. * the special cases of the former code.
  47. *
  48. * With the new dcache, the pathname is stored at each inode, at least as
  49. * long as the refcount of the inode is positive. As a side effect, the
  50. * size of the dcache depends on the inode cache and thus is dynamic.
  51. *
  52. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  53. * resolution to correspond with current state of the code.
  54. *
  55. * Note that the symlink resolution is not *completely* iterative.
  56. * There is still a significant amount of tail- and mid- recursion in
  57. * the algorithm. Also, note that <fs>_readlink() is not used in
  58. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  59. * may return different results than <fs>_follow_link(). Many virtual
  60. * filesystems (including /proc) exhibit this behavior.
  61. */
  62. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  63. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  64. * and the name already exists in form of a symlink, try to create the new
  65. * name indicated by the symlink. The old code always complained that the
  66. * name already exists, due to not following the symlink even if its target
  67. * is nonexistent. The new semantics affects also mknod() and link() when
  68. * the name is a symlink pointing to a non-existant name.
  69. *
  70. * I don't know which semantics is the right one, since I have no access
  71. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  72. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  73. * "old" one. Personally, I think the new semantics is much more logical.
  74. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  75. * file does succeed in both HP-UX and SunOs, but not in Solaris
  76. * and in the old Linux semantics.
  77. */
  78. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  79. * semantics. See the comments in "open_namei" and "do_link" below.
  80. *
  81. * [10-Sep-98 Alan Modra] Another symlink change.
  82. */
  83. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  84. * inside the path - always follow.
  85. * in the last component in creation/removal/renaming - never follow.
  86. * if LOOKUP_FOLLOW passed - follow.
  87. * if the pathname has trailing slashes - follow.
  88. * otherwise - don't follow.
  89. * (applied in that order).
  90. *
  91. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  92. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  93. * During the 2.4 we need to fix the userland stuff depending on it -
  94. * hopefully we will be able to get rid of that wart in 2.5. So far only
  95. * XEmacs seems to be relying on it...
  96. */
  97. /*
  98. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  99. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  100. * any extra contention...
  101. */
  102. /* In order to reduce some races, while at the same time doing additional
  103. * checking and hopefully speeding things up, we copy filenames to the
  104. * kernel data space before using them..
  105. *
  106. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  107. * PATH_MAX includes the nul terminator --RR.
  108. */
  109. static int do_getname(const char __user *filename, char *page)
  110. {
  111. int retval;
  112. unsigned long len = PATH_MAX;
  113. if (!segment_eq(get_fs(), KERNEL_DS)) {
  114. if ((unsigned long) filename >= TASK_SIZE)
  115. return -EFAULT;
  116. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  117. len = TASK_SIZE - (unsigned long) filename;
  118. }
  119. retval = strncpy_from_user(page, filename, len);
  120. if (retval > 0) {
  121. if (retval < len)
  122. return 0;
  123. return -ENAMETOOLONG;
  124. } else if (!retval)
  125. retval = -ENOENT;
  126. return retval;
  127. }
  128. char * getname(const char __user * filename)
  129. {
  130. char *tmp, *result;
  131. result = ERR_PTR(-ENOMEM);
  132. tmp = __getname();
  133. if (tmp) {
  134. int retval = do_getname(filename, tmp);
  135. result = tmp;
  136. if (retval < 0) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. audit_getname(result);
  142. return result;
  143. }
  144. #ifdef CONFIG_AUDITSYSCALL
  145. void putname(const char *name)
  146. {
  147. if (unlikely(!audit_dummy_context()))
  148. audit_putname(name);
  149. else
  150. __putname(name);
  151. }
  152. EXPORT_SYMBOL(putname);
  153. #endif
  154. /**
  155. * generic_permission - check for access rights on a Posix-like filesystem
  156. * @inode: inode to check access rights for
  157. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  158. * @check_acl: optional callback to check for Posix ACLs
  159. *
  160. * Used to check for read/write/execute permissions on a file.
  161. * We use "fsuid" for this, letting us set arbitrary permissions
  162. * for filesystem access without changing the "normal" uids which
  163. * are used for other things..
  164. */
  165. int generic_permission(struct inode *inode, int mask,
  166. int (*check_acl)(struct inode *inode, int mask))
  167. {
  168. umode_t mode = inode->i_mode;
  169. if (current->fsuid == inode->i_uid)
  170. mode >>= 6;
  171. else {
  172. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  173. int error = check_acl(inode, mask);
  174. if (error == -EACCES)
  175. goto check_capabilities;
  176. else if (error != -EAGAIN)
  177. return error;
  178. }
  179. if (in_group_p(inode->i_gid))
  180. mode >>= 3;
  181. }
  182. /*
  183. * If the DACs are ok we don't need any capability check.
  184. */
  185. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  186. return 0;
  187. check_capabilities:
  188. /*
  189. * Read/write DACs are always overridable.
  190. * Executable DACs are overridable if at least one exec bit is set.
  191. */
  192. if (!(mask & MAY_EXEC) ||
  193. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  194. if (capable(CAP_DAC_OVERRIDE))
  195. return 0;
  196. /*
  197. * Searching includes executable on directories, else just read.
  198. */
  199. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  200. if (capable(CAP_DAC_READ_SEARCH))
  201. return 0;
  202. return -EACCES;
  203. }
  204. int permission(struct inode *inode, int mask, struct nameidata *nd)
  205. {
  206. umode_t mode = inode->i_mode;
  207. int retval, submask;
  208. if (mask & MAY_WRITE) {
  209. /*
  210. * Nobody gets write access to a read-only fs.
  211. */
  212. if (IS_RDONLY(inode) &&
  213. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  214. return -EROFS;
  215. /*
  216. * Nobody gets write access to an immutable file.
  217. */
  218. if (IS_IMMUTABLE(inode))
  219. return -EACCES;
  220. }
  221. /*
  222. * MAY_EXEC on regular files requires special handling: We override
  223. * filesystem execute permissions if the mode bits aren't set or
  224. * the fs is mounted with the "noexec" flag.
  225. */
  226. if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) ||
  227. (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC))))
  228. return -EACCES;
  229. /* Ordinary permission routines do not understand MAY_APPEND. */
  230. submask = mask & ~MAY_APPEND;
  231. if (inode->i_op && inode->i_op->permission)
  232. retval = inode->i_op->permission(inode, submask, nd);
  233. else
  234. retval = generic_permission(inode, submask, NULL);
  235. if (retval)
  236. return retval;
  237. return security_inode_permission(inode, mask, nd);
  238. }
  239. /**
  240. * vfs_permission - check for access rights to a given path
  241. * @nd: lookup result that describes the path
  242. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  243. *
  244. * Used to check for read/write/execute permissions on a path.
  245. * We use "fsuid" for this, letting us set arbitrary permissions
  246. * for filesystem access without changing the "normal" uids which
  247. * are used for other things.
  248. */
  249. int vfs_permission(struct nameidata *nd, int mask)
  250. {
  251. return permission(nd->dentry->d_inode, mask, nd);
  252. }
  253. /**
  254. * file_permission - check for additional access rights to a given file
  255. * @file: file to check access rights for
  256. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  257. *
  258. * Used to check for read/write/execute permissions on an already opened
  259. * file.
  260. *
  261. * Note:
  262. * Do not use this function in new code. All access checks should
  263. * be done using vfs_permission().
  264. */
  265. int file_permission(struct file *file, int mask)
  266. {
  267. return permission(file->f_dentry->d_inode, mask, NULL);
  268. }
  269. /*
  270. * get_write_access() gets write permission for a file.
  271. * put_write_access() releases this write permission.
  272. * This is used for regular files.
  273. * We cannot support write (and maybe mmap read-write shared) accesses and
  274. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  275. * can have the following values:
  276. * 0: no writers, no VM_DENYWRITE mappings
  277. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  278. * > 0: (i_writecount) users are writing to the file.
  279. *
  280. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  281. * except for the cases where we don't hold i_writecount yet. Then we need to
  282. * use {get,deny}_write_access() - these functions check the sign and refuse
  283. * to do the change if sign is wrong. Exclusion between them is provided by
  284. * the inode->i_lock spinlock.
  285. */
  286. int get_write_access(struct inode * inode)
  287. {
  288. spin_lock(&inode->i_lock);
  289. if (atomic_read(&inode->i_writecount) < 0) {
  290. spin_unlock(&inode->i_lock);
  291. return -ETXTBSY;
  292. }
  293. atomic_inc(&inode->i_writecount);
  294. spin_unlock(&inode->i_lock);
  295. return 0;
  296. }
  297. int deny_write_access(struct file * file)
  298. {
  299. struct inode *inode = file->f_dentry->d_inode;
  300. spin_lock(&inode->i_lock);
  301. if (atomic_read(&inode->i_writecount) > 0) {
  302. spin_unlock(&inode->i_lock);
  303. return -ETXTBSY;
  304. }
  305. atomic_dec(&inode->i_writecount);
  306. spin_unlock(&inode->i_lock);
  307. return 0;
  308. }
  309. void path_release(struct nameidata *nd)
  310. {
  311. dput(nd->dentry);
  312. mntput(nd->mnt);
  313. }
  314. /*
  315. * umount() mustn't call path_release()/mntput() as that would clear
  316. * mnt_expiry_mark
  317. */
  318. void path_release_on_umount(struct nameidata *nd)
  319. {
  320. dput(nd->dentry);
  321. mntput_no_expire(nd->mnt);
  322. }
  323. /**
  324. * release_open_intent - free up open intent resources
  325. * @nd: pointer to nameidata
  326. */
  327. void release_open_intent(struct nameidata *nd)
  328. {
  329. if (nd->intent.open.file->f_dentry == NULL)
  330. put_filp(nd->intent.open.file);
  331. else
  332. fput(nd->intent.open.file);
  333. }
  334. static inline struct dentry *
  335. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  336. {
  337. int status = dentry->d_op->d_revalidate(dentry, nd);
  338. if (unlikely(status <= 0)) {
  339. /*
  340. * The dentry failed validation.
  341. * If d_revalidate returned 0 attempt to invalidate
  342. * the dentry otherwise d_revalidate is asking us
  343. * to return a fail status.
  344. */
  345. if (!status) {
  346. if (!d_invalidate(dentry)) {
  347. dput(dentry);
  348. dentry = NULL;
  349. }
  350. } else {
  351. dput(dentry);
  352. dentry = ERR_PTR(status);
  353. }
  354. }
  355. return dentry;
  356. }
  357. /*
  358. * Internal lookup() using the new generic dcache.
  359. * SMP-safe
  360. */
  361. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  362. {
  363. struct dentry * dentry = __d_lookup(parent, name);
  364. /* lockess __d_lookup may fail due to concurrent d_move()
  365. * in some unrelated directory, so try with d_lookup
  366. */
  367. if (!dentry)
  368. dentry = d_lookup(parent, name);
  369. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  370. dentry = do_revalidate(dentry, nd);
  371. return dentry;
  372. }
  373. /*
  374. * Short-cut version of permission(), for calling by
  375. * path_walk(), when dcache lock is held. Combines parts
  376. * of permission() and generic_permission(), and tests ONLY for
  377. * MAY_EXEC permission.
  378. *
  379. * If appropriate, check DAC only. If not appropriate, or
  380. * short-cut DAC fails, then call permission() to do more
  381. * complete permission check.
  382. */
  383. static int exec_permission_lite(struct inode *inode,
  384. struct nameidata *nd)
  385. {
  386. umode_t mode = inode->i_mode;
  387. if (inode->i_op && inode->i_op->permission)
  388. return -EAGAIN;
  389. if (current->fsuid == inode->i_uid)
  390. mode >>= 6;
  391. else if (in_group_p(inode->i_gid))
  392. mode >>= 3;
  393. if (mode & MAY_EXEC)
  394. goto ok;
  395. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  396. goto ok;
  397. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  398. goto ok;
  399. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  400. goto ok;
  401. return -EACCES;
  402. ok:
  403. return security_inode_permission(inode, MAY_EXEC, nd);
  404. }
  405. /*
  406. * This is called when everything else fails, and we actually have
  407. * to go to the low-level filesystem to find out what we should do..
  408. *
  409. * We get the directory semaphore, and after getting that we also
  410. * make sure that nobody added the entry to the dcache in the meantime..
  411. * SMP-safe
  412. */
  413. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  414. {
  415. struct dentry * result;
  416. struct inode *dir = parent->d_inode;
  417. mutex_lock(&dir->i_mutex);
  418. /*
  419. * First re-do the cached lookup just in case it was created
  420. * while we waited for the directory semaphore..
  421. *
  422. * FIXME! This could use version numbering or similar to
  423. * avoid unnecessary cache lookups.
  424. *
  425. * The "dcache_lock" is purely to protect the RCU list walker
  426. * from concurrent renames at this point (we mustn't get false
  427. * negatives from the RCU list walk here, unlike the optimistic
  428. * fast walk).
  429. *
  430. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  431. */
  432. result = d_lookup(parent, name);
  433. if (!result) {
  434. struct dentry * dentry = d_alloc(parent, name);
  435. result = ERR_PTR(-ENOMEM);
  436. if (dentry) {
  437. result = dir->i_op->lookup(dir, dentry, nd);
  438. if (result)
  439. dput(dentry);
  440. else
  441. result = dentry;
  442. }
  443. mutex_unlock(&dir->i_mutex);
  444. return result;
  445. }
  446. /*
  447. * Uhhuh! Nasty case: the cache was re-populated while
  448. * we waited on the semaphore. Need to revalidate.
  449. */
  450. mutex_unlock(&dir->i_mutex);
  451. if (result->d_op && result->d_op->d_revalidate) {
  452. result = do_revalidate(result, nd);
  453. if (!result)
  454. result = ERR_PTR(-ENOENT);
  455. }
  456. return result;
  457. }
  458. static int __emul_lookup_dentry(const char *, struct nameidata *);
  459. /* SMP-safe */
  460. static __always_inline int
  461. walk_init_root(const char *name, struct nameidata *nd)
  462. {
  463. struct fs_struct *fs = current->fs;
  464. read_lock(&fs->lock);
  465. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  466. nd->mnt = mntget(fs->altrootmnt);
  467. nd->dentry = dget(fs->altroot);
  468. read_unlock(&fs->lock);
  469. if (__emul_lookup_dentry(name,nd))
  470. return 0;
  471. read_lock(&fs->lock);
  472. }
  473. nd->mnt = mntget(fs->rootmnt);
  474. nd->dentry = dget(fs->root);
  475. read_unlock(&fs->lock);
  476. return 1;
  477. }
  478. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  479. {
  480. int res = 0;
  481. char *name;
  482. if (IS_ERR(link))
  483. goto fail;
  484. if (*link == '/') {
  485. path_release(nd);
  486. if (!walk_init_root(link, nd))
  487. /* weird __emul_prefix() stuff did it */
  488. goto out;
  489. }
  490. res = link_path_walk(link, nd);
  491. out:
  492. if (nd->depth || res || nd->last_type!=LAST_NORM)
  493. return res;
  494. /*
  495. * If it is an iterative symlinks resolution in open_namei() we
  496. * have to copy the last component. And all that crap because of
  497. * bloody create() on broken symlinks. Furrfu...
  498. */
  499. name = __getname();
  500. if (unlikely(!name)) {
  501. path_release(nd);
  502. return -ENOMEM;
  503. }
  504. strcpy(name, nd->last.name);
  505. nd->last.name = name;
  506. return 0;
  507. fail:
  508. path_release(nd);
  509. return PTR_ERR(link);
  510. }
  511. struct path {
  512. struct vfsmount *mnt;
  513. struct dentry *dentry;
  514. };
  515. static inline void dput_path(struct path *path, struct nameidata *nd)
  516. {
  517. dput(path->dentry);
  518. if (path->mnt != nd->mnt)
  519. mntput(path->mnt);
  520. }
  521. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  522. {
  523. dput(nd->dentry);
  524. if (nd->mnt != path->mnt)
  525. mntput(nd->mnt);
  526. nd->mnt = path->mnt;
  527. nd->dentry = path->dentry;
  528. }
  529. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  530. {
  531. int error;
  532. void *cookie;
  533. struct dentry *dentry = path->dentry;
  534. touch_atime(path->mnt, dentry);
  535. nd_set_link(nd, NULL);
  536. if (path->mnt != nd->mnt) {
  537. path_to_nameidata(path, nd);
  538. dget(dentry);
  539. }
  540. mntget(path->mnt);
  541. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  542. error = PTR_ERR(cookie);
  543. if (!IS_ERR(cookie)) {
  544. char *s = nd_get_link(nd);
  545. error = 0;
  546. if (s)
  547. error = __vfs_follow_link(nd, s);
  548. if (dentry->d_inode->i_op->put_link)
  549. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  550. }
  551. dput(dentry);
  552. mntput(path->mnt);
  553. return error;
  554. }
  555. /*
  556. * This limits recursive symlink follows to 8, while
  557. * limiting consecutive symlinks to 40.
  558. *
  559. * Without that kind of total limit, nasty chains of consecutive
  560. * symlinks can cause almost arbitrarily long lookups.
  561. */
  562. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  563. {
  564. int err = -ELOOP;
  565. if (current->link_count >= MAX_NESTED_LINKS)
  566. goto loop;
  567. if (current->total_link_count >= 40)
  568. goto loop;
  569. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  570. cond_resched();
  571. err = security_inode_follow_link(path->dentry, nd);
  572. if (err)
  573. goto loop;
  574. current->link_count++;
  575. current->total_link_count++;
  576. nd->depth++;
  577. err = __do_follow_link(path, nd);
  578. current->link_count--;
  579. nd->depth--;
  580. return err;
  581. loop:
  582. dput_path(path, nd);
  583. path_release(nd);
  584. return err;
  585. }
  586. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  587. {
  588. struct vfsmount *parent;
  589. struct dentry *mountpoint;
  590. spin_lock(&vfsmount_lock);
  591. parent=(*mnt)->mnt_parent;
  592. if (parent == *mnt) {
  593. spin_unlock(&vfsmount_lock);
  594. return 0;
  595. }
  596. mntget(parent);
  597. mountpoint=dget((*mnt)->mnt_mountpoint);
  598. spin_unlock(&vfsmount_lock);
  599. dput(*dentry);
  600. *dentry = mountpoint;
  601. mntput(*mnt);
  602. *mnt = parent;
  603. return 1;
  604. }
  605. /* no need for dcache_lock, as serialization is taken care in
  606. * namespace.c
  607. */
  608. static int __follow_mount(struct path *path)
  609. {
  610. int res = 0;
  611. while (d_mountpoint(path->dentry)) {
  612. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  613. if (!mounted)
  614. break;
  615. dput(path->dentry);
  616. if (res)
  617. mntput(path->mnt);
  618. path->mnt = mounted;
  619. path->dentry = dget(mounted->mnt_root);
  620. res = 1;
  621. }
  622. return res;
  623. }
  624. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  625. {
  626. while (d_mountpoint(*dentry)) {
  627. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  628. if (!mounted)
  629. break;
  630. dput(*dentry);
  631. mntput(*mnt);
  632. *mnt = mounted;
  633. *dentry = dget(mounted->mnt_root);
  634. }
  635. }
  636. /* no need for dcache_lock, as serialization is taken care in
  637. * namespace.c
  638. */
  639. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  640. {
  641. struct vfsmount *mounted;
  642. mounted = lookup_mnt(*mnt, *dentry);
  643. if (mounted) {
  644. dput(*dentry);
  645. mntput(*mnt);
  646. *mnt = mounted;
  647. *dentry = dget(mounted->mnt_root);
  648. return 1;
  649. }
  650. return 0;
  651. }
  652. static __always_inline void follow_dotdot(struct nameidata *nd)
  653. {
  654. struct fs_struct *fs = current->fs;
  655. while(1) {
  656. struct vfsmount *parent;
  657. struct dentry *old = nd->dentry;
  658. read_lock(&fs->lock);
  659. if (nd->dentry == fs->root &&
  660. nd->mnt == fs->rootmnt) {
  661. read_unlock(&fs->lock);
  662. break;
  663. }
  664. read_unlock(&fs->lock);
  665. spin_lock(&dcache_lock);
  666. if (nd->dentry != nd->mnt->mnt_root) {
  667. nd->dentry = dget(nd->dentry->d_parent);
  668. spin_unlock(&dcache_lock);
  669. dput(old);
  670. break;
  671. }
  672. spin_unlock(&dcache_lock);
  673. spin_lock(&vfsmount_lock);
  674. parent = nd->mnt->mnt_parent;
  675. if (parent == nd->mnt) {
  676. spin_unlock(&vfsmount_lock);
  677. break;
  678. }
  679. mntget(parent);
  680. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  681. spin_unlock(&vfsmount_lock);
  682. dput(old);
  683. mntput(nd->mnt);
  684. nd->mnt = parent;
  685. }
  686. follow_mount(&nd->mnt, &nd->dentry);
  687. }
  688. /*
  689. * It's more convoluted than I'd like it to be, but... it's still fairly
  690. * small and for now I'd prefer to have fast path as straight as possible.
  691. * It _is_ time-critical.
  692. */
  693. static int do_lookup(struct nameidata *nd, struct qstr *name,
  694. struct path *path)
  695. {
  696. struct vfsmount *mnt = nd->mnt;
  697. struct dentry *dentry = __d_lookup(nd->dentry, name);
  698. if (!dentry)
  699. goto need_lookup;
  700. if (dentry->d_op && dentry->d_op->d_revalidate)
  701. goto need_revalidate;
  702. done:
  703. path->mnt = mnt;
  704. path->dentry = dentry;
  705. __follow_mount(path);
  706. return 0;
  707. need_lookup:
  708. dentry = real_lookup(nd->dentry, name, nd);
  709. if (IS_ERR(dentry))
  710. goto fail;
  711. goto done;
  712. need_revalidate:
  713. dentry = do_revalidate(dentry, nd);
  714. if (!dentry)
  715. goto need_lookup;
  716. if (IS_ERR(dentry))
  717. goto fail;
  718. goto done;
  719. fail:
  720. return PTR_ERR(dentry);
  721. }
  722. /*
  723. * Name resolution.
  724. * This is the basic name resolution function, turning a pathname into
  725. * the final dentry. We expect 'base' to be positive and a directory.
  726. *
  727. * Returns 0 and nd will have valid dentry and mnt on success.
  728. * Returns error and drops reference to input namei data on failure.
  729. */
  730. static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
  731. {
  732. struct path next;
  733. struct inode *inode;
  734. int err;
  735. unsigned int lookup_flags = nd->flags;
  736. while (*name=='/')
  737. name++;
  738. if (!*name)
  739. goto return_reval;
  740. inode = nd->dentry->d_inode;
  741. if (nd->depth)
  742. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  743. /* At this point we know we have a real path component. */
  744. for(;;) {
  745. unsigned long hash;
  746. struct qstr this;
  747. unsigned int c;
  748. nd->flags |= LOOKUP_CONTINUE;
  749. err = exec_permission_lite(inode, nd);
  750. if (err == -EAGAIN)
  751. err = vfs_permission(nd, MAY_EXEC);
  752. if (err)
  753. break;
  754. this.name = name;
  755. c = *(const unsigned char *)name;
  756. hash = init_name_hash();
  757. do {
  758. name++;
  759. hash = partial_name_hash(c, hash);
  760. c = *(const unsigned char *)name;
  761. } while (c && (c != '/'));
  762. this.len = name - (const char *) this.name;
  763. this.hash = end_name_hash(hash);
  764. /* remove trailing slashes? */
  765. if (!c)
  766. goto last_component;
  767. while (*++name == '/');
  768. if (!*name)
  769. goto last_with_slashes;
  770. /*
  771. * "." and ".." are special - ".." especially so because it has
  772. * to be able to know about the current root directory and
  773. * parent relationships.
  774. */
  775. if (this.name[0] == '.') switch (this.len) {
  776. default:
  777. break;
  778. case 2:
  779. if (this.name[1] != '.')
  780. break;
  781. follow_dotdot(nd);
  782. inode = nd->dentry->d_inode;
  783. /* fallthrough */
  784. case 1:
  785. continue;
  786. }
  787. /*
  788. * See if the low-level filesystem might want
  789. * to use its own hash..
  790. */
  791. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  792. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  793. if (err < 0)
  794. break;
  795. }
  796. /* This does the actual lookups.. */
  797. err = do_lookup(nd, &this, &next);
  798. if (err)
  799. break;
  800. err = -ENOENT;
  801. inode = next.dentry->d_inode;
  802. if (!inode)
  803. goto out_dput;
  804. err = -ENOTDIR;
  805. if (!inode->i_op)
  806. goto out_dput;
  807. if (inode->i_op->follow_link) {
  808. err = do_follow_link(&next, nd);
  809. if (err)
  810. goto return_err;
  811. err = -ENOENT;
  812. inode = nd->dentry->d_inode;
  813. if (!inode)
  814. break;
  815. err = -ENOTDIR;
  816. if (!inode->i_op)
  817. break;
  818. } else
  819. path_to_nameidata(&next, nd);
  820. err = -ENOTDIR;
  821. if (!inode->i_op->lookup)
  822. break;
  823. continue;
  824. /* here ends the main loop */
  825. last_with_slashes:
  826. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  827. last_component:
  828. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  829. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  830. if (lookup_flags & LOOKUP_PARENT)
  831. goto lookup_parent;
  832. if (this.name[0] == '.') switch (this.len) {
  833. default:
  834. break;
  835. case 2:
  836. if (this.name[1] != '.')
  837. break;
  838. follow_dotdot(nd);
  839. inode = nd->dentry->d_inode;
  840. /* fallthrough */
  841. case 1:
  842. goto return_reval;
  843. }
  844. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  845. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  846. if (err < 0)
  847. break;
  848. }
  849. err = do_lookup(nd, &this, &next);
  850. if (err)
  851. break;
  852. inode = next.dentry->d_inode;
  853. if ((lookup_flags & LOOKUP_FOLLOW)
  854. && inode && inode->i_op && inode->i_op->follow_link) {
  855. err = do_follow_link(&next, nd);
  856. if (err)
  857. goto return_err;
  858. inode = nd->dentry->d_inode;
  859. } else
  860. path_to_nameidata(&next, nd);
  861. err = -ENOENT;
  862. if (!inode)
  863. break;
  864. if (lookup_flags & LOOKUP_DIRECTORY) {
  865. err = -ENOTDIR;
  866. if (!inode->i_op || !inode->i_op->lookup)
  867. break;
  868. }
  869. goto return_base;
  870. lookup_parent:
  871. nd->last = this;
  872. nd->last_type = LAST_NORM;
  873. if (this.name[0] != '.')
  874. goto return_base;
  875. if (this.len == 1)
  876. nd->last_type = LAST_DOT;
  877. else if (this.len == 2 && this.name[1] == '.')
  878. nd->last_type = LAST_DOTDOT;
  879. else
  880. goto return_base;
  881. return_reval:
  882. /*
  883. * We bypassed the ordinary revalidation routines.
  884. * We may need to check the cached dentry for staleness.
  885. */
  886. if (nd->dentry && nd->dentry->d_sb &&
  887. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  888. err = -ESTALE;
  889. /* Note: we do not d_invalidate() */
  890. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  891. break;
  892. }
  893. return_base:
  894. return 0;
  895. out_dput:
  896. dput_path(&next, nd);
  897. break;
  898. }
  899. path_release(nd);
  900. return_err:
  901. return err;
  902. }
  903. /*
  904. * Wrapper to retry pathname resolution whenever the underlying
  905. * file system returns an ESTALE.
  906. *
  907. * Retry the whole path once, forcing real lookup requests
  908. * instead of relying on the dcache.
  909. */
  910. int fastcall link_path_walk(const char *name, struct nameidata *nd)
  911. {
  912. struct nameidata save = *nd;
  913. int result;
  914. /* make sure the stuff we saved doesn't go away */
  915. dget(save.dentry);
  916. mntget(save.mnt);
  917. result = __link_path_walk(name, nd);
  918. if (result == -ESTALE) {
  919. *nd = save;
  920. dget(nd->dentry);
  921. mntget(nd->mnt);
  922. nd->flags |= LOOKUP_REVAL;
  923. result = __link_path_walk(name, nd);
  924. }
  925. dput(save.dentry);
  926. mntput(save.mnt);
  927. return result;
  928. }
  929. int fastcall path_walk(const char * name, struct nameidata *nd)
  930. {
  931. current->total_link_count = 0;
  932. return link_path_walk(name, nd);
  933. }
  934. /*
  935. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  936. * everything is done. Returns 0 and drops input nd, if lookup failed;
  937. */
  938. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  939. {
  940. if (path_walk(name, nd))
  941. return 0; /* something went wrong... */
  942. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  943. struct dentry *old_dentry = nd->dentry;
  944. struct vfsmount *old_mnt = nd->mnt;
  945. struct qstr last = nd->last;
  946. int last_type = nd->last_type;
  947. struct fs_struct *fs = current->fs;
  948. /*
  949. * NAME was not found in alternate root or it's a directory.
  950. * Try to find it in the normal root:
  951. */
  952. nd->last_type = LAST_ROOT;
  953. read_lock(&fs->lock);
  954. nd->mnt = mntget(fs->rootmnt);
  955. nd->dentry = dget(fs->root);
  956. read_unlock(&fs->lock);
  957. if (path_walk(name, nd) == 0) {
  958. if (nd->dentry->d_inode) {
  959. dput(old_dentry);
  960. mntput(old_mnt);
  961. return 1;
  962. }
  963. path_release(nd);
  964. }
  965. nd->dentry = old_dentry;
  966. nd->mnt = old_mnt;
  967. nd->last = last;
  968. nd->last_type = last_type;
  969. }
  970. return 1;
  971. }
  972. void set_fs_altroot(void)
  973. {
  974. char *emul = __emul_prefix();
  975. struct nameidata nd;
  976. struct vfsmount *mnt = NULL, *oldmnt;
  977. struct dentry *dentry = NULL, *olddentry;
  978. int err;
  979. struct fs_struct *fs = current->fs;
  980. if (!emul)
  981. goto set_it;
  982. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  983. if (!err) {
  984. mnt = nd.mnt;
  985. dentry = nd.dentry;
  986. }
  987. set_it:
  988. write_lock(&fs->lock);
  989. oldmnt = fs->altrootmnt;
  990. olddentry = fs->altroot;
  991. fs->altrootmnt = mnt;
  992. fs->altroot = dentry;
  993. write_unlock(&fs->lock);
  994. if (olddentry) {
  995. dput(olddentry);
  996. mntput(oldmnt);
  997. }
  998. }
  999. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1000. static int fastcall do_path_lookup(int dfd, const char *name,
  1001. unsigned int flags, struct nameidata *nd)
  1002. {
  1003. int retval = 0;
  1004. int fput_needed;
  1005. struct file *file;
  1006. struct fs_struct *fs = current->fs;
  1007. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1008. nd->flags = flags;
  1009. nd->depth = 0;
  1010. if (*name=='/') {
  1011. read_lock(&fs->lock);
  1012. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  1013. nd->mnt = mntget(fs->altrootmnt);
  1014. nd->dentry = dget(fs->altroot);
  1015. read_unlock(&fs->lock);
  1016. if (__emul_lookup_dentry(name,nd))
  1017. goto out; /* found in altroot */
  1018. read_lock(&fs->lock);
  1019. }
  1020. nd->mnt = mntget(fs->rootmnt);
  1021. nd->dentry = dget(fs->root);
  1022. read_unlock(&fs->lock);
  1023. } else if (dfd == AT_FDCWD) {
  1024. read_lock(&fs->lock);
  1025. nd->mnt = mntget(fs->pwdmnt);
  1026. nd->dentry = dget(fs->pwd);
  1027. read_unlock(&fs->lock);
  1028. } else {
  1029. struct dentry *dentry;
  1030. file = fget_light(dfd, &fput_needed);
  1031. retval = -EBADF;
  1032. if (!file)
  1033. goto out_fail;
  1034. dentry = file->f_dentry;
  1035. retval = -ENOTDIR;
  1036. if (!S_ISDIR(dentry->d_inode->i_mode))
  1037. goto fput_fail;
  1038. retval = file_permission(file, MAY_EXEC);
  1039. if (retval)
  1040. goto fput_fail;
  1041. nd->mnt = mntget(file->f_vfsmnt);
  1042. nd->dentry = dget(dentry);
  1043. fput_light(file, fput_needed);
  1044. }
  1045. current->total_link_count = 0;
  1046. retval = link_path_walk(name, nd);
  1047. out:
  1048. if (likely(retval == 0)) {
  1049. if (unlikely(!audit_dummy_context() && nd && nd->dentry &&
  1050. nd->dentry->d_inode))
  1051. audit_inode(name, nd->dentry->d_inode);
  1052. }
  1053. out_fail:
  1054. return retval;
  1055. fput_fail:
  1056. fput_light(file, fput_needed);
  1057. goto out_fail;
  1058. }
  1059. int fastcall path_lookup(const char *name, unsigned int flags,
  1060. struct nameidata *nd)
  1061. {
  1062. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1063. }
  1064. static int __path_lookup_intent_open(int dfd, const char *name,
  1065. unsigned int lookup_flags, struct nameidata *nd,
  1066. int open_flags, int create_mode)
  1067. {
  1068. struct file *filp = get_empty_filp();
  1069. int err;
  1070. if (filp == NULL)
  1071. return -ENFILE;
  1072. nd->intent.open.file = filp;
  1073. nd->intent.open.flags = open_flags;
  1074. nd->intent.open.create_mode = create_mode;
  1075. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1076. if (IS_ERR(nd->intent.open.file)) {
  1077. if (err == 0) {
  1078. err = PTR_ERR(nd->intent.open.file);
  1079. path_release(nd);
  1080. }
  1081. } else if (err != 0)
  1082. release_open_intent(nd);
  1083. return err;
  1084. }
  1085. /**
  1086. * path_lookup_open - lookup a file path with open intent
  1087. * @dfd: the directory to use as base, or AT_FDCWD
  1088. * @name: pointer to file name
  1089. * @lookup_flags: lookup intent flags
  1090. * @nd: pointer to nameidata
  1091. * @open_flags: open intent flags
  1092. */
  1093. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1094. struct nameidata *nd, int open_flags)
  1095. {
  1096. return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
  1097. open_flags, 0);
  1098. }
  1099. /**
  1100. * path_lookup_create - lookup a file path with open + create intent
  1101. * @dfd: the directory to use as base, or AT_FDCWD
  1102. * @name: pointer to file name
  1103. * @lookup_flags: lookup intent flags
  1104. * @nd: pointer to nameidata
  1105. * @open_flags: open intent flags
  1106. * @create_mode: create intent flags
  1107. */
  1108. static int path_lookup_create(int dfd, const char *name,
  1109. unsigned int lookup_flags, struct nameidata *nd,
  1110. int open_flags, int create_mode)
  1111. {
  1112. return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
  1113. nd, open_flags, create_mode);
  1114. }
  1115. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1116. struct nameidata *nd, int open_flags)
  1117. {
  1118. char *tmp = getname(name);
  1119. int err = PTR_ERR(tmp);
  1120. if (!IS_ERR(tmp)) {
  1121. err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
  1122. putname(tmp);
  1123. }
  1124. return err;
  1125. }
  1126. /*
  1127. * Restricted form of lookup. Doesn't follow links, single-component only,
  1128. * needs parent already locked. Doesn't follow mounts.
  1129. * SMP-safe.
  1130. */
  1131. static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
  1132. {
  1133. struct dentry * dentry;
  1134. struct inode *inode;
  1135. int err;
  1136. inode = base->d_inode;
  1137. err = permission(inode, MAY_EXEC, nd);
  1138. dentry = ERR_PTR(err);
  1139. if (err)
  1140. goto out;
  1141. /*
  1142. * See if the low-level filesystem might want
  1143. * to use its own hash..
  1144. */
  1145. if (base->d_op && base->d_op->d_hash) {
  1146. err = base->d_op->d_hash(base, name);
  1147. dentry = ERR_PTR(err);
  1148. if (err < 0)
  1149. goto out;
  1150. }
  1151. dentry = cached_lookup(base, name, nd);
  1152. if (!dentry) {
  1153. struct dentry *new = d_alloc(base, name);
  1154. dentry = ERR_PTR(-ENOMEM);
  1155. if (!new)
  1156. goto out;
  1157. dentry = inode->i_op->lookup(inode, new, nd);
  1158. if (!dentry)
  1159. dentry = new;
  1160. else
  1161. dput(new);
  1162. }
  1163. out:
  1164. return dentry;
  1165. }
  1166. static struct dentry *lookup_hash(struct nameidata *nd)
  1167. {
  1168. return __lookup_hash(&nd->last, nd->dentry, nd);
  1169. }
  1170. /* SMP-safe */
  1171. struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
  1172. {
  1173. unsigned long hash;
  1174. struct qstr this;
  1175. unsigned int c;
  1176. this.name = name;
  1177. this.len = len;
  1178. if (!len)
  1179. goto access;
  1180. hash = init_name_hash();
  1181. while (len--) {
  1182. c = *(const unsigned char *)name++;
  1183. if (c == '/' || c == '\0')
  1184. goto access;
  1185. hash = partial_name_hash(c, hash);
  1186. }
  1187. this.hash = end_name_hash(hash);
  1188. return __lookup_hash(&this, base, NULL);
  1189. access:
  1190. return ERR_PTR(-EACCES);
  1191. }
  1192. /*
  1193. * namei()
  1194. *
  1195. * is used by most simple commands to get the inode of a specified name.
  1196. * Open, link etc use their own routines, but this is enough for things
  1197. * like 'chmod' etc.
  1198. *
  1199. * namei exists in two versions: namei/lnamei. The only difference is
  1200. * that namei follows links, while lnamei does not.
  1201. * SMP-safe
  1202. */
  1203. int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
  1204. struct nameidata *nd)
  1205. {
  1206. char *tmp = getname(name);
  1207. int err = PTR_ERR(tmp);
  1208. if (!IS_ERR(tmp)) {
  1209. err = do_path_lookup(dfd, tmp, flags, nd);
  1210. putname(tmp);
  1211. }
  1212. return err;
  1213. }
  1214. int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1215. {
  1216. return __user_walk_fd(AT_FDCWD, name, flags, nd);
  1217. }
  1218. /*
  1219. * It's inline, so penalty for filesystems that don't use sticky bit is
  1220. * minimal.
  1221. */
  1222. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1223. {
  1224. if (!(dir->i_mode & S_ISVTX))
  1225. return 0;
  1226. if (inode->i_uid == current->fsuid)
  1227. return 0;
  1228. if (dir->i_uid == current->fsuid)
  1229. return 0;
  1230. return !capable(CAP_FOWNER);
  1231. }
  1232. /*
  1233. * Check whether we can remove a link victim from directory dir, check
  1234. * whether the type of victim is right.
  1235. * 1. We can't do it if dir is read-only (done in permission())
  1236. * 2. We should have write and exec permissions on dir
  1237. * 3. We can't remove anything from append-only dir
  1238. * 4. We can't do anything with immutable dir (done in permission())
  1239. * 5. If the sticky bit on dir is set we should either
  1240. * a. be owner of dir, or
  1241. * b. be owner of victim, or
  1242. * c. have CAP_FOWNER capability
  1243. * 6. If the victim is append-only or immutable we can't do antyhing with
  1244. * links pointing to it.
  1245. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1246. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1247. * 9. We can't remove a root or mountpoint.
  1248. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1249. * nfs_async_unlink().
  1250. */
  1251. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1252. {
  1253. int error;
  1254. if (!victim->d_inode)
  1255. return -ENOENT;
  1256. BUG_ON(victim->d_parent->d_inode != dir);
  1257. audit_inode_child(victim->d_name.name, victim->d_inode, dir);
  1258. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1259. if (error)
  1260. return error;
  1261. if (IS_APPEND(dir))
  1262. return -EPERM;
  1263. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1264. IS_IMMUTABLE(victim->d_inode))
  1265. return -EPERM;
  1266. if (isdir) {
  1267. if (!S_ISDIR(victim->d_inode->i_mode))
  1268. return -ENOTDIR;
  1269. if (IS_ROOT(victim))
  1270. return -EBUSY;
  1271. } else if (S_ISDIR(victim->d_inode->i_mode))
  1272. return -EISDIR;
  1273. if (IS_DEADDIR(dir))
  1274. return -ENOENT;
  1275. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1276. return -EBUSY;
  1277. return 0;
  1278. }
  1279. /* Check whether we can create an object with dentry child in directory
  1280. * dir.
  1281. * 1. We can't do it if child already exists (open has special treatment for
  1282. * this case, but since we are inlined it's OK)
  1283. * 2. We can't do it if dir is read-only (done in permission())
  1284. * 3. We should have write and exec permissions on dir
  1285. * 4. We can't do it if dir is immutable (done in permission())
  1286. */
  1287. static inline int may_create(struct inode *dir, struct dentry *child,
  1288. struct nameidata *nd)
  1289. {
  1290. if (child->d_inode)
  1291. return -EEXIST;
  1292. if (IS_DEADDIR(dir))
  1293. return -ENOENT;
  1294. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1295. }
  1296. /*
  1297. * O_DIRECTORY translates into forcing a directory lookup.
  1298. */
  1299. static inline int lookup_flags(unsigned int f)
  1300. {
  1301. unsigned long retval = LOOKUP_FOLLOW;
  1302. if (f & O_NOFOLLOW)
  1303. retval &= ~LOOKUP_FOLLOW;
  1304. if (f & O_DIRECTORY)
  1305. retval |= LOOKUP_DIRECTORY;
  1306. return retval;
  1307. }
  1308. /*
  1309. * p1 and p2 should be directories on the same fs.
  1310. */
  1311. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1312. {
  1313. struct dentry *p;
  1314. if (p1 == p2) {
  1315. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1316. return NULL;
  1317. }
  1318. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1319. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1320. if (p->d_parent == p2) {
  1321. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1322. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1323. return p;
  1324. }
  1325. }
  1326. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1327. if (p->d_parent == p1) {
  1328. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1329. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1330. return p;
  1331. }
  1332. }
  1333. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1334. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1335. return NULL;
  1336. }
  1337. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1338. {
  1339. mutex_unlock(&p1->d_inode->i_mutex);
  1340. if (p1 != p2) {
  1341. mutex_unlock(&p2->d_inode->i_mutex);
  1342. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1343. }
  1344. }
  1345. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1346. struct nameidata *nd)
  1347. {
  1348. int error = may_create(dir, dentry, nd);
  1349. if (error)
  1350. return error;
  1351. if (!dir->i_op || !dir->i_op->create)
  1352. return -EACCES; /* shouldn't it be ENOSYS? */
  1353. mode &= S_IALLUGO;
  1354. mode |= S_IFREG;
  1355. error = security_inode_create(dir, dentry, mode);
  1356. if (error)
  1357. return error;
  1358. DQUOT_INIT(dir);
  1359. error = dir->i_op->create(dir, dentry, mode, nd);
  1360. if (!error)
  1361. fsnotify_create(dir, dentry);
  1362. return error;
  1363. }
  1364. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1365. {
  1366. struct dentry *dentry = nd->dentry;
  1367. struct inode *inode = dentry->d_inode;
  1368. int error;
  1369. if (!inode)
  1370. return -ENOENT;
  1371. if (S_ISLNK(inode->i_mode))
  1372. return -ELOOP;
  1373. if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
  1374. return -EISDIR;
  1375. error = vfs_permission(nd, acc_mode);
  1376. if (error)
  1377. return error;
  1378. /*
  1379. * FIFO's, sockets and device files are special: they don't
  1380. * actually live on the filesystem itself, and as such you
  1381. * can write to them even if the filesystem is read-only.
  1382. */
  1383. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1384. flag &= ~O_TRUNC;
  1385. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1386. if (nd->mnt->mnt_flags & MNT_NODEV)
  1387. return -EACCES;
  1388. flag &= ~O_TRUNC;
  1389. } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
  1390. return -EROFS;
  1391. /*
  1392. * An append-only file must be opened in append mode for writing.
  1393. */
  1394. if (IS_APPEND(inode)) {
  1395. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1396. return -EPERM;
  1397. if (flag & O_TRUNC)
  1398. return -EPERM;
  1399. }
  1400. /* O_NOATIME can only be set by the owner or superuser */
  1401. if (flag & O_NOATIME)
  1402. if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
  1403. return -EPERM;
  1404. /*
  1405. * Ensure there are no outstanding leases on the file.
  1406. */
  1407. error = break_lease(inode, flag);
  1408. if (error)
  1409. return error;
  1410. if (flag & O_TRUNC) {
  1411. error = get_write_access(inode);
  1412. if (error)
  1413. return error;
  1414. /*
  1415. * Refuse to truncate files with mandatory locks held on them.
  1416. */
  1417. error = locks_verify_locked(inode);
  1418. if (!error) {
  1419. DQUOT_INIT(inode);
  1420. error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
  1421. }
  1422. put_write_access(inode);
  1423. if (error)
  1424. return error;
  1425. } else
  1426. if (flag & FMODE_WRITE)
  1427. DQUOT_INIT(inode);
  1428. return 0;
  1429. }
  1430. static int open_namei_create(struct nameidata *nd, struct path *path,
  1431. int flag, int mode)
  1432. {
  1433. int error;
  1434. struct dentry *dir = nd->dentry;
  1435. if (!IS_POSIXACL(dir->d_inode))
  1436. mode &= ~current->fs->umask;
  1437. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1438. mutex_unlock(&dir->d_inode->i_mutex);
  1439. dput(nd->dentry);
  1440. nd->dentry = path->dentry;
  1441. if (error)
  1442. return error;
  1443. /* Don't check for write permission, don't truncate */
  1444. return may_open(nd, 0, flag & ~O_TRUNC);
  1445. }
  1446. /*
  1447. * open_namei()
  1448. *
  1449. * namei for open - this is in fact almost the whole open-routine.
  1450. *
  1451. * Note that the low bits of "flag" aren't the same as in the open
  1452. * system call - they are 00 - no permissions needed
  1453. * 01 - read permission needed
  1454. * 10 - write permission needed
  1455. * 11 - read/write permissions needed
  1456. * which is a lot more logical, and also allows the "no perm" needed
  1457. * for symlinks (where the permissions are checked later).
  1458. * SMP-safe
  1459. */
  1460. int open_namei(int dfd, const char *pathname, int flag,
  1461. int mode, struct nameidata *nd)
  1462. {
  1463. int acc_mode, error;
  1464. struct path path;
  1465. struct dentry *dir;
  1466. int count = 0;
  1467. acc_mode = ACC_MODE(flag);
  1468. /* O_TRUNC implies we need access checks for write permissions */
  1469. if (flag & O_TRUNC)
  1470. acc_mode |= MAY_WRITE;
  1471. /* Allow the LSM permission hook to distinguish append
  1472. access from general write access. */
  1473. if (flag & O_APPEND)
  1474. acc_mode |= MAY_APPEND;
  1475. /*
  1476. * The simplest case - just a plain lookup.
  1477. */
  1478. if (!(flag & O_CREAT)) {
  1479. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1480. nd, flag);
  1481. if (error)
  1482. return error;
  1483. goto ok;
  1484. }
  1485. /*
  1486. * Create - we need to know the parent.
  1487. */
  1488. error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
  1489. if (error)
  1490. return error;
  1491. /*
  1492. * We have the parent and last component. First of all, check
  1493. * that we are not asked to creat(2) an obvious directory - that
  1494. * will not do.
  1495. */
  1496. error = -EISDIR;
  1497. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1498. goto exit;
  1499. dir = nd->dentry;
  1500. nd->flags &= ~LOOKUP_PARENT;
  1501. mutex_lock(&dir->d_inode->i_mutex);
  1502. path.dentry = lookup_hash(nd);
  1503. path.mnt = nd->mnt;
  1504. do_last:
  1505. error = PTR_ERR(path.dentry);
  1506. if (IS_ERR(path.dentry)) {
  1507. mutex_unlock(&dir->d_inode->i_mutex);
  1508. goto exit;
  1509. }
  1510. if (IS_ERR(nd->intent.open.file)) {
  1511. mutex_unlock(&dir->d_inode->i_mutex);
  1512. error = PTR_ERR(nd->intent.open.file);
  1513. goto exit_dput;
  1514. }
  1515. /* Negative dentry, just create the file */
  1516. if (!path.dentry->d_inode) {
  1517. error = open_namei_create(nd, &path, flag, mode);
  1518. if (error)
  1519. goto exit;
  1520. return 0;
  1521. }
  1522. /*
  1523. * It already exists.
  1524. */
  1525. mutex_unlock(&dir->d_inode->i_mutex);
  1526. audit_inode_update(path.dentry->d_inode);
  1527. error = -EEXIST;
  1528. if (flag & O_EXCL)
  1529. goto exit_dput;
  1530. if (__follow_mount(&path)) {
  1531. error = -ELOOP;
  1532. if (flag & O_NOFOLLOW)
  1533. goto exit_dput;
  1534. }
  1535. error = -ENOENT;
  1536. if (!path.dentry->d_inode)
  1537. goto exit_dput;
  1538. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1539. goto do_link;
  1540. path_to_nameidata(&path, nd);
  1541. error = -EISDIR;
  1542. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1543. goto exit;
  1544. ok:
  1545. error = may_open(nd, acc_mode, flag);
  1546. if (error)
  1547. goto exit;
  1548. return 0;
  1549. exit_dput:
  1550. dput_path(&path, nd);
  1551. exit:
  1552. if (!IS_ERR(nd->intent.open.file))
  1553. release_open_intent(nd);
  1554. path_release(nd);
  1555. return error;
  1556. do_link:
  1557. error = -ELOOP;
  1558. if (flag & O_NOFOLLOW)
  1559. goto exit_dput;
  1560. /*
  1561. * This is subtle. Instead of calling do_follow_link() we do the
  1562. * thing by hands. The reason is that this way we have zero link_count
  1563. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1564. * After that we have the parent and last component, i.e.
  1565. * we are in the same situation as after the first path_walk().
  1566. * Well, almost - if the last component is normal we get its copy
  1567. * stored in nd->last.name and we will have to putname() it when we
  1568. * are done. Procfs-like symlinks just set LAST_BIND.
  1569. */
  1570. nd->flags |= LOOKUP_PARENT;
  1571. error = security_inode_follow_link(path.dentry, nd);
  1572. if (error)
  1573. goto exit_dput;
  1574. error = __do_follow_link(&path, nd);
  1575. if (error) {
  1576. /* Does someone understand code flow here? Or it is only
  1577. * me so stupid? Anathema to whoever designed this non-sense
  1578. * with "intent.open".
  1579. */
  1580. release_open_intent(nd);
  1581. return error;
  1582. }
  1583. nd->flags &= ~LOOKUP_PARENT;
  1584. if (nd->last_type == LAST_BIND)
  1585. goto ok;
  1586. error = -EISDIR;
  1587. if (nd->last_type != LAST_NORM)
  1588. goto exit;
  1589. if (nd->last.name[nd->last.len]) {
  1590. __putname(nd->last.name);
  1591. goto exit;
  1592. }
  1593. error = -ELOOP;
  1594. if (count++==32) {
  1595. __putname(nd->last.name);
  1596. goto exit;
  1597. }
  1598. dir = nd->dentry;
  1599. mutex_lock(&dir->d_inode->i_mutex);
  1600. path.dentry = lookup_hash(nd);
  1601. path.mnt = nd->mnt;
  1602. __putname(nd->last.name);
  1603. goto do_last;
  1604. }
  1605. /**
  1606. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1607. * @nd: nameidata info
  1608. * @is_dir: directory flag
  1609. *
  1610. * Simple function to lookup and return a dentry and create it
  1611. * if it doesn't exist. Is SMP-safe.
  1612. *
  1613. * Returns with nd->dentry->d_inode->i_mutex locked.
  1614. */
  1615. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1616. {
  1617. struct dentry *dentry = ERR_PTR(-EEXIST);
  1618. mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1619. /*
  1620. * Yucky last component or no last component at all?
  1621. * (foo/., foo/.., /////)
  1622. */
  1623. if (nd->last_type != LAST_NORM)
  1624. goto fail;
  1625. nd->flags &= ~LOOKUP_PARENT;
  1626. nd->flags |= LOOKUP_CREATE;
  1627. nd->intent.open.flags = O_EXCL;
  1628. /*
  1629. * Do the final lookup.
  1630. */
  1631. dentry = lookup_hash(nd);
  1632. if (IS_ERR(dentry))
  1633. goto fail;
  1634. /*
  1635. * Special case - lookup gave negative, but... we had foo/bar/
  1636. * From the vfs_mknod() POV we just have a negative dentry -
  1637. * all is fine. Let's be bastards - you had / on the end, you've
  1638. * been asking for (non-existent) directory. -ENOENT for you.
  1639. */
  1640. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1641. goto enoent;
  1642. return dentry;
  1643. enoent:
  1644. dput(dentry);
  1645. dentry = ERR_PTR(-ENOENT);
  1646. fail:
  1647. return dentry;
  1648. }
  1649. EXPORT_SYMBOL_GPL(lookup_create);
  1650. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1651. {
  1652. int error = may_create(dir, dentry, NULL);
  1653. if (error)
  1654. return error;
  1655. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1656. return -EPERM;
  1657. if (!dir->i_op || !dir->i_op->mknod)
  1658. return -EPERM;
  1659. error = security_inode_mknod(dir, dentry, mode, dev);
  1660. if (error)
  1661. return error;
  1662. DQUOT_INIT(dir);
  1663. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1664. if (!error)
  1665. fsnotify_create(dir, dentry);
  1666. return error;
  1667. }
  1668. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1669. unsigned dev)
  1670. {
  1671. int error = 0;
  1672. char * tmp;
  1673. struct dentry * dentry;
  1674. struct nameidata nd;
  1675. if (S_ISDIR(mode))
  1676. return -EPERM;
  1677. tmp = getname(filename);
  1678. if (IS_ERR(tmp))
  1679. return PTR_ERR(tmp);
  1680. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1681. if (error)
  1682. goto out;
  1683. dentry = lookup_create(&nd, 0);
  1684. error = PTR_ERR(dentry);
  1685. if (!IS_POSIXACL(nd.dentry->d_inode))
  1686. mode &= ~current->fs->umask;
  1687. if (!IS_ERR(dentry)) {
  1688. switch (mode & S_IFMT) {
  1689. case 0: case S_IFREG:
  1690. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1691. break;
  1692. case S_IFCHR: case S_IFBLK:
  1693. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1694. new_decode_dev(dev));
  1695. break;
  1696. case S_IFIFO: case S_IFSOCK:
  1697. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1698. break;
  1699. case S_IFDIR:
  1700. error = -EPERM;
  1701. break;
  1702. default:
  1703. error = -EINVAL;
  1704. }
  1705. dput(dentry);
  1706. }
  1707. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1708. path_release(&nd);
  1709. out:
  1710. putname(tmp);
  1711. return error;
  1712. }
  1713. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1714. {
  1715. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1716. }
  1717. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1718. {
  1719. int error = may_create(dir, dentry, NULL);
  1720. if (error)
  1721. return error;
  1722. if (!dir->i_op || !dir->i_op->mkdir)
  1723. return -EPERM;
  1724. mode &= (S_IRWXUGO|S_ISVTX);
  1725. error = security_inode_mkdir(dir, dentry, mode);
  1726. if (error)
  1727. return error;
  1728. DQUOT_INIT(dir);
  1729. error = dir->i_op->mkdir(dir, dentry, mode);
  1730. if (!error)
  1731. fsnotify_mkdir(dir, dentry);
  1732. return error;
  1733. }
  1734. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1735. {
  1736. int error = 0;
  1737. char * tmp;
  1738. struct dentry *dentry;
  1739. struct nameidata nd;
  1740. tmp = getname(pathname);
  1741. error = PTR_ERR(tmp);
  1742. if (IS_ERR(tmp))
  1743. goto out_err;
  1744. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1745. if (error)
  1746. goto out;
  1747. dentry = lookup_create(&nd, 1);
  1748. error = PTR_ERR(dentry);
  1749. if (IS_ERR(dentry))
  1750. goto out_unlock;
  1751. if (!IS_POSIXACL(nd.dentry->d_inode))
  1752. mode &= ~current->fs->umask;
  1753. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1754. dput(dentry);
  1755. out_unlock:
  1756. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1757. path_release(&nd);
  1758. out:
  1759. putname(tmp);
  1760. out_err:
  1761. return error;
  1762. }
  1763. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1764. {
  1765. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1766. }
  1767. /*
  1768. * We try to drop the dentry early: we should have
  1769. * a usage count of 2 if we're the only user of this
  1770. * dentry, and if that is true (possibly after pruning
  1771. * the dcache), then we drop the dentry now.
  1772. *
  1773. * A low-level filesystem can, if it choses, legally
  1774. * do a
  1775. *
  1776. * if (!d_unhashed(dentry))
  1777. * return -EBUSY;
  1778. *
  1779. * if it cannot handle the case of removing a directory
  1780. * that is still in use by something else..
  1781. */
  1782. void dentry_unhash(struct dentry *dentry)
  1783. {
  1784. dget(dentry);
  1785. shrink_dcache_parent(dentry);
  1786. spin_lock(&dcache_lock);
  1787. spin_lock(&dentry->d_lock);
  1788. if (atomic_read(&dentry->d_count) == 2)
  1789. __d_drop(dentry);
  1790. spin_unlock(&dentry->d_lock);
  1791. spin_unlock(&dcache_lock);
  1792. }
  1793. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1794. {
  1795. int error = may_delete(dir, dentry, 1);
  1796. if (error)
  1797. return error;
  1798. if (!dir->i_op || !dir->i_op->rmdir)
  1799. return -EPERM;
  1800. DQUOT_INIT(dir);
  1801. mutex_lock(&dentry->d_inode->i_mutex);
  1802. dentry_unhash(dentry);
  1803. if (d_mountpoint(dentry))
  1804. error = -EBUSY;
  1805. else {
  1806. error = security_inode_rmdir(dir, dentry);
  1807. if (!error) {
  1808. error = dir->i_op->rmdir(dir, dentry);
  1809. if (!error)
  1810. dentry->d_inode->i_flags |= S_DEAD;
  1811. }
  1812. }
  1813. mutex_unlock(&dentry->d_inode->i_mutex);
  1814. if (!error) {
  1815. d_delete(dentry);
  1816. }
  1817. dput(dentry);
  1818. return error;
  1819. }
  1820. static long do_rmdir(int dfd, const char __user *pathname)
  1821. {
  1822. int error = 0;
  1823. char * name;
  1824. struct dentry *dentry;
  1825. struct nameidata nd;
  1826. name = getname(pathname);
  1827. if(IS_ERR(name))
  1828. return PTR_ERR(name);
  1829. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1830. if (error)
  1831. goto exit;
  1832. switch(nd.last_type) {
  1833. case LAST_DOTDOT:
  1834. error = -ENOTEMPTY;
  1835. goto exit1;
  1836. case LAST_DOT:
  1837. error = -EINVAL;
  1838. goto exit1;
  1839. case LAST_ROOT:
  1840. error = -EBUSY;
  1841. goto exit1;
  1842. }
  1843. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1844. dentry = lookup_hash(&nd);
  1845. error = PTR_ERR(dentry);
  1846. if (IS_ERR(dentry))
  1847. goto exit2;
  1848. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1849. dput(dentry);
  1850. exit2:
  1851. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1852. exit1:
  1853. path_release(&nd);
  1854. exit:
  1855. putname(name);
  1856. return error;
  1857. }
  1858. asmlinkage long sys_rmdir(const char __user *pathname)
  1859. {
  1860. return do_rmdir(AT_FDCWD, pathname);
  1861. }
  1862. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1863. {
  1864. int error = may_delete(dir, dentry, 0);
  1865. if (error)
  1866. return error;
  1867. if (!dir->i_op || !dir->i_op->unlink)
  1868. return -EPERM;
  1869. DQUOT_INIT(dir);
  1870. mutex_lock(&dentry->d_inode->i_mutex);
  1871. if (d_mountpoint(dentry))
  1872. error = -EBUSY;
  1873. else {
  1874. error = security_inode_unlink(dir, dentry);
  1875. if (!error)
  1876. error = dir->i_op->unlink(dir, dentry);
  1877. }
  1878. mutex_unlock(&dentry->d_inode->i_mutex);
  1879. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1880. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1881. d_delete(dentry);
  1882. }
  1883. return error;
  1884. }
  1885. /*
  1886. * Make sure that the actual truncation of the file will occur outside its
  1887. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1888. * writeout happening, and we don't want to prevent access to the directory
  1889. * while waiting on the I/O.
  1890. */
  1891. static long do_unlinkat(int dfd, const char __user *pathname)
  1892. {
  1893. int error = 0;
  1894. char * name;
  1895. struct dentry *dentry;
  1896. struct nameidata nd;
  1897. struct inode *inode = NULL;
  1898. name = getname(pathname);
  1899. if(IS_ERR(name))
  1900. return PTR_ERR(name);
  1901. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1902. if (error)
  1903. goto exit;
  1904. error = -EISDIR;
  1905. if (nd.last_type != LAST_NORM)
  1906. goto exit1;
  1907. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1908. dentry = lookup_hash(&nd);
  1909. error = PTR_ERR(dentry);
  1910. if (!IS_ERR(dentry)) {
  1911. /* Why not before? Because we want correct error value */
  1912. if (nd.last.name[nd.last.len])
  1913. goto slashes;
  1914. inode = dentry->d_inode;
  1915. if (inode)
  1916. atomic_inc(&inode->i_count);
  1917. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1918. exit2:
  1919. dput(dentry);
  1920. }
  1921. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1922. if (inode)
  1923. iput(inode); /* truncate the inode here */
  1924. exit1:
  1925. path_release(&nd);
  1926. exit:
  1927. putname(name);
  1928. return error;
  1929. slashes:
  1930. error = !dentry->d_inode ? -ENOENT :
  1931. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1932. goto exit2;
  1933. }
  1934. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  1935. {
  1936. if ((flag & ~AT_REMOVEDIR) != 0)
  1937. return -EINVAL;
  1938. if (flag & AT_REMOVEDIR)
  1939. return do_rmdir(dfd, pathname);
  1940. return do_unlinkat(dfd, pathname);
  1941. }
  1942. asmlinkage long sys_unlink(const char __user *pathname)
  1943. {
  1944. return do_unlinkat(AT_FDCWD, pathname);
  1945. }
  1946. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  1947. {
  1948. int error = may_create(dir, dentry, NULL);
  1949. if (error)
  1950. return error;
  1951. if (!dir->i_op || !dir->i_op->symlink)
  1952. return -EPERM;
  1953. error = security_inode_symlink(dir, dentry, oldname);
  1954. if (error)
  1955. return error;
  1956. DQUOT_INIT(dir);
  1957. error = dir->i_op->symlink(dir, dentry, oldname);
  1958. if (!error)
  1959. fsnotify_create(dir, dentry);
  1960. return error;
  1961. }
  1962. asmlinkage long sys_symlinkat(const char __user *oldname,
  1963. int newdfd, const char __user *newname)
  1964. {
  1965. int error = 0;
  1966. char * from;
  1967. char * to;
  1968. struct dentry *dentry;
  1969. struct nameidata nd;
  1970. from = getname(oldname);
  1971. if(IS_ERR(from))
  1972. return PTR_ERR(from);
  1973. to = getname(newname);
  1974. error = PTR_ERR(to);
  1975. if (IS_ERR(to))
  1976. goto out_putname;
  1977. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  1978. if (error)
  1979. goto out;
  1980. dentry = lookup_create(&nd, 0);
  1981. error = PTR_ERR(dentry);
  1982. if (IS_ERR(dentry))
  1983. goto out_unlock;
  1984. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  1985. dput(dentry);
  1986. out_unlock:
  1987. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1988. path_release(&nd);
  1989. out:
  1990. putname(to);
  1991. out_putname:
  1992. putname(from);
  1993. return error;
  1994. }
  1995. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  1996. {
  1997. return sys_symlinkat(oldname, AT_FDCWD, newname);
  1998. }
  1999. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2000. {
  2001. struct inode *inode = old_dentry->d_inode;
  2002. int error;
  2003. if (!inode)
  2004. return -ENOENT;
  2005. error = may_create(dir, new_dentry, NULL);
  2006. if (error)
  2007. return error;
  2008. if (dir->i_sb != inode->i_sb)
  2009. return -EXDEV;
  2010. /*
  2011. * A link to an append-only or immutable file cannot be created.
  2012. */
  2013. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2014. return -EPERM;
  2015. if (!dir->i_op || !dir->i_op->link)
  2016. return -EPERM;
  2017. if (S_ISDIR(old_dentry->d_inode->i_mode))
  2018. return -EPERM;
  2019. error = security_inode_link(old_dentry, dir, new_dentry);
  2020. if (error)
  2021. return error;
  2022. mutex_lock(&old_dentry->d_inode->i_mutex);
  2023. DQUOT_INIT(dir);
  2024. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2025. mutex_unlock(&old_dentry->d_inode->i_mutex);
  2026. if (!error)
  2027. fsnotify_create(dir, new_dentry);
  2028. return error;
  2029. }
  2030. /*
  2031. * Hardlinks are often used in delicate situations. We avoid
  2032. * security-related surprises by not following symlinks on the
  2033. * newname. --KAB
  2034. *
  2035. * We don't follow them on the oldname either to be compatible
  2036. * with linux 2.0, and to avoid hard-linking to directories
  2037. * and other special files. --ADM
  2038. */
  2039. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2040. int newdfd, const char __user *newname,
  2041. int flags)
  2042. {
  2043. struct dentry *new_dentry;
  2044. struct nameidata nd, old_nd;
  2045. int error;
  2046. char * to;
  2047. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2048. return -EINVAL;
  2049. to = getname(newname);
  2050. if (IS_ERR(to))
  2051. return PTR_ERR(to);
  2052. error = __user_walk_fd(olddfd, oldname,
  2053. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2054. &old_nd);
  2055. if (error)
  2056. goto exit;
  2057. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2058. if (error)
  2059. goto out;
  2060. error = -EXDEV;
  2061. if (old_nd.mnt != nd.mnt)
  2062. goto out_release;
  2063. new_dentry = lookup_create(&nd, 0);
  2064. error = PTR_ERR(new_dentry);
  2065. if (IS_ERR(new_dentry))
  2066. goto out_unlock;
  2067. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  2068. dput(new_dentry);
  2069. out_unlock:
  2070. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  2071. out_release:
  2072. path_release(&nd);
  2073. out:
  2074. path_release(&old_nd);
  2075. exit:
  2076. putname(to);
  2077. return error;
  2078. }
  2079. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2080. {
  2081. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2082. }
  2083. /*
  2084. * The worst of all namespace operations - renaming directory. "Perverted"
  2085. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2086. * Problems:
  2087. * a) we can get into loop creation. Check is done in is_subdir().
  2088. * b) race potential - two innocent renames can create a loop together.
  2089. * That's where 4.4 screws up. Current fix: serialization on
  2090. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2091. * story.
  2092. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2093. * And that - after we got ->i_mutex on parents (until then we don't know
  2094. * whether the target exists). Solution: try to be smart with locking
  2095. * order for inodes. We rely on the fact that tree topology may change
  2096. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2097. * move will be locked. Thus we can rank directories by the tree
  2098. * (ancestors first) and rank all non-directories after them.
  2099. * That works since everybody except rename does "lock parent, lookup,
  2100. * lock child" and rename is under ->s_vfs_rename_mutex.
  2101. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2102. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2103. * we'd better make sure that there's no link(2) for them.
  2104. * d) some filesystems don't support opened-but-unlinked directories,
  2105. * either because of layout or because they are not ready to deal with
  2106. * all cases correctly. The latter will be fixed (taking this sort of
  2107. * stuff into VFS), but the former is not going away. Solution: the same
  2108. * trick as in rmdir().
  2109. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2110. * we are removing the target. Solution: we will have to grab ->i_mutex
  2111. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2112. * ->i_mutex on parents, which works but leads to some truely excessive
  2113. * locking].
  2114. */
  2115. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2116. struct inode *new_dir, struct dentry *new_dentry)
  2117. {
  2118. int error = 0;
  2119. struct inode *target;
  2120. /*
  2121. * If we are going to change the parent - check write permissions,
  2122. * we'll need to flip '..'.
  2123. */
  2124. if (new_dir != old_dir) {
  2125. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  2126. if (error)
  2127. return error;
  2128. }
  2129. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2130. if (error)
  2131. return error;
  2132. target = new_dentry->d_inode;
  2133. if (target) {
  2134. mutex_lock(&target->i_mutex);
  2135. dentry_unhash(new_dentry);
  2136. }
  2137. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2138. error = -EBUSY;
  2139. else
  2140. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2141. if (target) {
  2142. if (!error)
  2143. target->i_flags |= S_DEAD;
  2144. mutex_unlock(&target->i_mutex);
  2145. if (d_unhashed(new_dentry))
  2146. d_rehash(new_dentry);
  2147. dput(new_dentry);
  2148. }
  2149. if (!error)
  2150. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2151. d_move(old_dentry,new_dentry);
  2152. return error;
  2153. }
  2154. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2155. struct inode *new_dir, struct dentry *new_dentry)
  2156. {
  2157. struct inode *target;
  2158. int error;
  2159. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2160. if (error)
  2161. return error;
  2162. dget(new_dentry);
  2163. target = new_dentry->d_inode;
  2164. if (target)
  2165. mutex_lock(&target->i_mutex);
  2166. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2167. error = -EBUSY;
  2168. else
  2169. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2170. if (!error) {
  2171. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2172. d_move(old_dentry, new_dentry);
  2173. }
  2174. if (target)
  2175. mutex_unlock(&target->i_mutex);
  2176. dput(new_dentry);
  2177. return error;
  2178. }
  2179. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2180. struct inode *new_dir, struct dentry *new_dentry)
  2181. {
  2182. int error;
  2183. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2184. const char *old_name;
  2185. if (old_dentry->d_inode == new_dentry->d_inode)
  2186. return 0;
  2187. error = may_delete(old_dir, old_dentry, is_dir);
  2188. if (error)
  2189. return error;
  2190. if (!new_dentry->d_inode)
  2191. error = may_create(new_dir, new_dentry, NULL);
  2192. else
  2193. error = may_delete(new_dir, new_dentry, is_dir);
  2194. if (error)
  2195. return error;
  2196. if (!old_dir->i_op || !old_dir->i_op->rename)
  2197. return -EPERM;
  2198. DQUOT_INIT(old_dir);
  2199. DQUOT_INIT(new_dir);
  2200. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2201. if (is_dir)
  2202. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2203. else
  2204. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2205. if (!error) {
  2206. const char *new_name = old_dentry->d_name.name;
  2207. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2208. new_dentry->d_inode, old_dentry->d_inode);
  2209. }
  2210. fsnotify_oldname_free(old_name);
  2211. return error;
  2212. }
  2213. static int do_rename(int olddfd, const char *oldname,
  2214. int newdfd, const char *newname)
  2215. {
  2216. int error = 0;
  2217. struct dentry * old_dir, * new_dir;
  2218. struct dentry * old_dentry, *new_dentry;
  2219. struct dentry * trap;
  2220. struct nameidata oldnd, newnd;
  2221. error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
  2222. if (error)
  2223. goto exit;
  2224. error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
  2225. if (error)
  2226. goto exit1;
  2227. error = -EXDEV;
  2228. if (oldnd.mnt != newnd.mnt)
  2229. goto exit2;
  2230. old_dir = oldnd.dentry;
  2231. error = -EBUSY;
  2232. if (oldnd.last_type != LAST_NORM)
  2233. goto exit2;
  2234. new_dir = newnd.dentry;
  2235. if (newnd.last_type != LAST_NORM)
  2236. goto exit2;
  2237. trap = lock_rename(new_dir, old_dir);
  2238. old_dentry = lookup_hash(&oldnd);
  2239. error = PTR_ERR(old_dentry);
  2240. if (IS_ERR(old_dentry))
  2241. goto exit3;
  2242. /* source must exist */
  2243. error = -ENOENT;
  2244. if (!old_dentry->d_inode)
  2245. goto exit4;
  2246. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2247. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2248. error = -ENOTDIR;
  2249. if (oldnd.last.name[oldnd.last.len])
  2250. goto exit4;
  2251. if (newnd.last.name[newnd.last.len])
  2252. goto exit4;
  2253. }
  2254. /* source should not be ancestor of target */
  2255. error = -EINVAL;
  2256. if (old_dentry == trap)
  2257. goto exit4;
  2258. new_dentry = lookup_hash(&newnd);
  2259. error = PTR_ERR(new_dentry);
  2260. if (IS_ERR(new_dentry))
  2261. goto exit4;
  2262. /* target should not be an ancestor of source */
  2263. error = -ENOTEMPTY;
  2264. if (new_dentry == trap)
  2265. goto exit5;
  2266. error = vfs_rename(old_dir->d_inode, old_dentry,
  2267. new_dir->d_inode, new_dentry);
  2268. exit5:
  2269. dput(new_dentry);
  2270. exit4:
  2271. dput(old_dentry);
  2272. exit3:
  2273. unlock_rename(new_dir, old_dir);
  2274. exit2:
  2275. path_release(&newnd);
  2276. exit1:
  2277. path_release(&oldnd);
  2278. exit:
  2279. return error;
  2280. }
  2281. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2282. int newdfd, const char __user *newname)
  2283. {
  2284. int error;
  2285. char * from;
  2286. char * to;
  2287. from = getname(oldname);
  2288. if(IS_ERR(from))
  2289. return PTR_ERR(from);
  2290. to = getname(newname);
  2291. error = PTR_ERR(to);
  2292. if (!IS_ERR(to)) {
  2293. error = do_rename(olddfd, from, newdfd, to);
  2294. putname(to);
  2295. }
  2296. putname(from);
  2297. return error;
  2298. }
  2299. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2300. {
  2301. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2302. }
  2303. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2304. {
  2305. int len;
  2306. len = PTR_ERR(link);
  2307. if (IS_ERR(link))
  2308. goto out;
  2309. len = strlen(link);
  2310. if (len > (unsigned) buflen)
  2311. len = buflen;
  2312. if (copy_to_user(buffer, link, len))
  2313. len = -EFAULT;
  2314. out:
  2315. return len;
  2316. }
  2317. /*
  2318. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2319. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2320. * using) it for any given inode is up to filesystem.
  2321. */
  2322. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2323. {
  2324. struct nameidata nd;
  2325. void *cookie;
  2326. nd.depth = 0;
  2327. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2328. if (!IS_ERR(cookie)) {
  2329. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2330. if (dentry->d_inode->i_op->put_link)
  2331. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2332. cookie = ERR_PTR(res);
  2333. }
  2334. return PTR_ERR(cookie);
  2335. }
  2336. int vfs_follow_link(struct nameidata *nd, const char *link)
  2337. {
  2338. return __vfs_follow_link(nd, link);
  2339. }
  2340. /* get the link contents into pagecache */
  2341. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2342. {
  2343. struct page * page;
  2344. struct address_space *mapping = dentry->d_inode->i_mapping;
  2345. page = read_mapping_page(mapping, 0, NULL);
  2346. if (IS_ERR(page))
  2347. goto sync_fail;
  2348. wait_on_page_locked(page);
  2349. if (!PageUptodate(page))
  2350. goto async_fail;
  2351. *ppage = page;
  2352. return kmap(page);
  2353. async_fail:
  2354. page_cache_release(page);
  2355. return ERR_PTR(-EIO);
  2356. sync_fail:
  2357. return (char*)page;
  2358. }
  2359. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2360. {
  2361. struct page *page = NULL;
  2362. char *s = page_getlink(dentry, &page);
  2363. int res = vfs_readlink(dentry,buffer,buflen,s);
  2364. if (page) {
  2365. kunmap(page);
  2366. page_cache_release(page);
  2367. }
  2368. return res;
  2369. }
  2370. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2371. {
  2372. struct page *page = NULL;
  2373. nd_set_link(nd, page_getlink(dentry, &page));
  2374. return page;
  2375. }
  2376. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2377. {
  2378. struct page *page = cookie;
  2379. if (page) {
  2380. kunmap(page);
  2381. page_cache_release(page);
  2382. }
  2383. }
  2384. int __page_symlink(struct inode *inode, const char *symname, int len,
  2385. gfp_t gfp_mask)
  2386. {
  2387. struct address_space *mapping = inode->i_mapping;
  2388. struct page *page;
  2389. int err = -ENOMEM;
  2390. char *kaddr;
  2391. retry:
  2392. page = find_or_create_page(mapping, 0, gfp_mask);
  2393. if (!page)
  2394. goto fail;
  2395. err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
  2396. if (err == AOP_TRUNCATED_PAGE) {
  2397. page_cache_release(page);
  2398. goto retry;
  2399. }
  2400. if (err)
  2401. goto fail_map;
  2402. kaddr = kmap_atomic(page, KM_USER0);
  2403. memcpy(kaddr, symname, len-1);
  2404. kunmap_atomic(kaddr, KM_USER0);
  2405. err = mapping->a_ops->commit_write(NULL, page, 0, len-1);
  2406. if (err == AOP_TRUNCATED_PAGE) {
  2407. page_cache_release(page);
  2408. goto retry;
  2409. }
  2410. if (err)
  2411. goto fail_map;
  2412. /*
  2413. * Notice that we are _not_ going to block here - end of page is
  2414. * unmapped, so this will only try to map the rest of page, see
  2415. * that it is unmapped (typically even will not look into inode -
  2416. * ->i_size will be enough for everything) and zero it out.
  2417. * OTOH it's obviously correct and should make the page up-to-date.
  2418. */
  2419. if (!PageUptodate(page)) {
  2420. err = mapping->a_ops->readpage(NULL, page);
  2421. if (err != AOP_TRUNCATED_PAGE)
  2422. wait_on_page_locked(page);
  2423. } else {
  2424. unlock_page(page);
  2425. }
  2426. page_cache_release(page);
  2427. if (err < 0)
  2428. goto fail;
  2429. mark_inode_dirty(inode);
  2430. return 0;
  2431. fail_map:
  2432. unlock_page(page);
  2433. page_cache_release(page);
  2434. fail:
  2435. return err;
  2436. }
  2437. int page_symlink(struct inode *inode, const char *symname, int len)
  2438. {
  2439. return __page_symlink(inode, symname, len,
  2440. mapping_gfp_mask(inode->i_mapping));
  2441. }
  2442. struct inode_operations page_symlink_inode_operations = {
  2443. .readlink = generic_readlink,
  2444. .follow_link = page_follow_link_light,
  2445. .put_link = page_put_link,
  2446. };
  2447. EXPORT_SYMBOL(__user_walk);
  2448. EXPORT_SYMBOL(__user_walk_fd);
  2449. EXPORT_SYMBOL(follow_down);
  2450. EXPORT_SYMBOL(follow_up);
  2451. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2452. EXPORT_SYMBOL(getname);
  2453. EXPORT_SYMBOL(lock_rename);
  2454. EXPORT_SYMBOL(lookup_one_len);
  2455. EXPORT_SYMBOL(page_follow_link_light);
  2456. EXPORT_SYMBOL(page_put_link);
  2457. EXPORT_SYMBOL(page_readlink);
  2458. EXPORT_SYMBOL(__page_symlink);
  2459. EXPORT_SYMBOL(page_symlink);
  2460. EXPORT_SYMBOL(page_symlink_inode_operations);
  2461. EXPORT_SYMBOL(path_lookup);
  2462. EXPORT_SYMBOL(path_release);
  2463. EXPORT_SYMBOL(path_walk);
  2464. EXPORT_SYMBOL(permission);
  2465. EXPORT_SYMBOL(vfs_permission);
  2466. EXPORT_SYMBOL(file_permission);
  2467. EXPORT_SYMBOL(unlock_rename);
  2468. EXPORT_SYMBOL(vfs_create);
  2469. EXPORT_SYMBOL(vfs_follow_link);
  2470. EXPORT_SYMBOL(vfs_link);
  2471. EXPORT_SYMBOL(vfs_mkdir);
  2472. EXPORT_SYMBOL(vfs_mknod);
  2473. EXPORT_SYMBOL(generic_permission);
  2474. EXPORT_SYMBOL(vfs_readlink);
  2475. EXPORT_SYMBOL(vfs_rename);
  2476. EXPORT_SYMBOL(vfs_rmdir);
  2477. EXPORT_SYMBOL(vfs_symlink);
  2478. EXPORT_SYMBOL(vfs_unlink);
  2479. EXPORT_SYMBOL(dentry_unhash);
  2480. EXPORT_SYMBOL(generic_readlink);