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