namei.c 69 KB

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