namei.c 69 KB

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