namei.c 71 KB

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