namei.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442
  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/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.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 <linux/fs_struct.h>
  32. #include <asm/uaccess.h>
  33. #include "internal.h"
  34. /* [Feb-1997 T. Schoebel-Theuer]
  35. * Fundamental changes in the pathname lookup mechanisms (namei)
  36. * were necessary because of omirr. The reason is that omirr needs
  37. * to know the _real_ pathname, not the user-supplied one, in case
  38. * of symlinks (and also when transname replacements occur).
  39. *
  40. * The new code replaces the old recursive symlink resolution with
  41. * an iterative one (in case of non-nested symlink chains). It does
  42. * this with calls to <fs>_follow_link().
  43. * As a side effect, dir_namei(), _namei() and follow_link() are now
  44. * replaced with a single function lookup_dentry() that can handle all
  45. * the special cases of the former code.
  46. *
  47. * With the new dcache, the pathname is stored at each inode, at least as
  48. * long as the refcount of the inode is positive. As a side effect, the
  49. * size of the dcache depends on the inode cache and thus is dynamic.
  50. *
  51. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  52. * resolution to correspond with current state of the code.
  53. *
  54. * Note that the symlink resolution is not *completely* iterative.
  55. * There is still a significant amount of tail- and mid- recursion in
  56. * the algorithm. Also, note that <fs>_readlink() is not used in
  57. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  58. * may return different results than <fs>_follow_link(). Many virtual
  59. * filesystems (including /proc) exhibit this behavior.
  60. */
  61. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63. * and the name already exists in form of a symlink, try to create the new
  64. * name indicated by the symlink. The old code always complained that the
  65. * name already exists, due to not following the symlink even if its target
  66. * is nonexistent. The new semantics affects also mknod() and link() when
  67. * the name is a symlink pointing to a non-existant name.
  68. *
  69. * I don't know which semantics is the right one, since I have no access
  70. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72. * "old" one. Personally, I think the new semantics is much more logical.
  73. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74. * file does succeed in both HP-UX and SunOs, but not in Solaris
  75. * and in the old Linux semantics.
  76. */
  77. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  78. * semantics. See the comments in "open_namei" and "do_link" below.
  79. *
  80. * [10-Sep-98 Alan Modra] Another symlink change.
  81. */
  82. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  83. * inside the path - always follow.
  84. * in the last component in creation/removal/renaming - never follow.
  85. * if LOOKUP_FOLLOW passed - follow.
  86. * if the pathname has trailing slashes - follow.
  87. * otherwise - don't follow.
  88. * (applied in that order).
  89. *
  90. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  91. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  92. * During the 2.4 we need to fix the userland stuff depending on it -
  93. * hopefully we will be able to get rid of that wart in 2.5. So far only
  94. * XEmacs seems to be relying on it...
  95. */
  96. /*
  97. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  98. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  99. * any extra contention...
  100. */
  101. /* 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. static char *getname_flags(const char __user * filename, int flags)
  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. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. }
  142. audit_getname(result);
  143. return result;
  144. }
  145. char *getname(const char __user * filename)
  146. {
  147. return getname_flags(filename, 0);
  148. }
  149. #ifdef CONFIG_AUDITSYSCALL
  150. void putname(const char *name)
  151. {
  152. if (unlikely(!audit_dummy_context()))
  153. audit_putname(name);
  154. else
  155. __putname(name);
  156. }
  157. EXPORT_SYMBOL(putname);
  158. #endif
  159. /*
  160. * This does basic POSIX ACL permission checking
  161. */
  162. static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
  163. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  164. {
  165. umode_t mode = inode->i_mode;
  166. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  167. if (current_fsuid() == inode->i_uid)
  168. mode >>= 6;
  169. else {
  170. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  171. int error = check_acl(inode, mask, flags);
  172. if (error != -EAGAIN)
  173. return error;
  174. }
  175. if (in_group_p(inode->i_gid))
  176. mode >>= 3;
  177. }
  178. /*
  179. * If the DACs are ok we don't need any capability check.
  180. */
  181. if ((mask & ~mode) == 0)
  182. return 0;
  183. return -EACCES;
  184. }
  185. /**
  186. * generic_permission - check for access rights on a Posix-like filesystem
  187. * @inode: inode to check access rights for
  188. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  189. * @check_acl: optional callback to check for Posix ACLs
  190. * @flags: IPERM_FLAG_ flags.
  191. *
  192. * Used to check for read/write/execute permissions on a file.
  193. * We use "fsuid" for this, letting us set arbitrary permissions
  194. * for filesystem access without changing the "normal" uids which
  195. * are used for other things.
  196. *
  197. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  198. * request cannot be satisfied (eg. requires blocking or too much complexity).
  199. * It would then be called again in ref-walk mode.
  200. */
  201. int generic_permission(struct inode *inode, int mask, unsigned int flags,
  202. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  203. {
  204. int ret;
  205. /*
  206. * Do the basic POSIX ACL permission checks.
  207. */
  208. ret = acl_permission_check(inode, mask, flags, check_acl);
  209. if (ret != -EACCES)
  210. return ret;
  211. /*
  212. * Read/write DACs are always overridable.
  213. * Executable DACs are overridable if at least one exec bit is set.
  214. */
  215. if (!(mask & MAY_EXEC) || execute_ok(inode))
  216. if (capable(CAP_DAC_OVERRIDE))
  217. return 0;
  218. /*
  219. * Searching includes executable on directories, else just read.
  220. */
  221. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  222. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  223. if (capable(CAP_DAC_READ_SEARCH))
  224. return 0;
  225. return -EACCES;
  226. }
  227. /**
  228. * inode_permission - check for access rights to a given inode
  229. * @inode: inode to check permission on
  230. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  231. *
  232. * Used to check for read/write/execute permissions on an inode.
  233. * We use "fsuid" for this, letting us set arbitrary permissions
  234. * for filesystem access without changing the "normal" uids which
  235. * are used for other things.
  236. */
  237. int inode_permission(struct inode *inode, int mask)
  238. {
  239. int retval;
  240. if (mask & MAY_WRITE) {
  241. umode_t mode = inode->i_mode;
  242. /*
  243. * Nobody gets write access to a read-only fs.
  244. */
  245. if (IS_RDONLY(inode) &&
  246. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  247. return -EROFS;
  248. /*
  249. * Nobody gets write access to an immutable file.
  250. */
  251. if (IS_IMMUTABLE(inode))
  252. return -EACCES;
  253. }
  254. if (inode->i_op->permission)
  255. retval = inode->i_op->permission(inode, mask, 0);
  256. else
  257. retval = generic_permission(inode, mask, 0,
  258. inode->i_op->check_acl);
  259. if (retval)
  260. return retval;
  261. retval = devcgroup_inode_permission(inode, mask);
  262. if (retval)
  263. return retval;
  264. return security_inode_permission(inode, mask);
  265. }
  266. /**
  267. * file_permission - check for additional access rights to a given file
  268. * @file: file to check access rights for
  269. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  270. *
  271. * Used to check for read/write/execute permissions on an already opened
  272. * file.
  273. *
  274. * Note:
  275. * Do not use this function in new code. All access checks should
  276. * be done using inode_permission().
  277. */
  278. int file_permission(struct file *file, int mask)
  279. {
  280. return inode_permission(file->f_path.dentry->d_inode, mask);
  281. }
  282. /*
  283. * get_write_access() gets write permission for a file.
  284. * put_write_access() releases this write permission.
  285. * This is used for regular files.
  286. * We cannot support write (and maybe mmap read-write shared) accesses and
  287. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  288. * can have the following values:
  289. * 0: no writers, no VM_DENYWRITE mappings
  290. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  291. * > 0: (i_writecount) users are writing to the file.
  292. *
  293. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  294. * except for the cases where we don't hold i_writecount yet. Then we need to
  295. * use {get,deny}_write_access() - these functions check the sign and refuse
  296. * to do the change if sign is wrong. Exclusion between them is provided by
  297. * the inode->i_lock spinlock.
  298. */
  299. int get_write_access(struct inode * inode)
  300. {
  301. spin_lock(&inode->i_lock);
  302. if (atomic_read(&inode->i_writecount) < 0) {
  303. spin_unlock(&inode->i_lock);
  304. return -ETXTBSY;
  305. }
  306. atomic_inc(&inode->i_writecount);
  307. spin_unlock(&inode->i_lock);
  308. return 0;
  309. }
  310. int deny_write_access(struct file * file)
  311. {
  312. struct inode *inode = file->f_path.dentry->d_inode;
  313. spin_lock(&inode->i_lock);
  314. if (atomic_read(&inode->i_writecount) > 0) {
  315. spin_unlock(&inode->i_lock);
  316. return -ETXTBSY;
  317. }
  318. atomic_dec(&inode->i_writecount);
  319. spin_unlock(&inode->i_lock);
  320. return 0;
  321. }
  322. /**
  323. * path_get - get a reference to a path
  324. * @path: path to get the reference to
  325. *
  326. * Given a path increment the reference count to the dentry and the vfsmount.
  327. */
  328. void path_get(struct path *path)
  329. {
  330. mntget(path->mnt);
  331. dget(path->dentry);
  332. }
  333. EXPORT_SYMBOL(path_get);
  334. /**
  335. * path_put - put a reference to a path
  336. * @path: path to put the reference to
  337. *
  338. * Given a path decrement the reference count to the dentry and the vfsmount.
  339. */
  340. void path_put(struct path *path)
  341. {
  342. dput(path->dentry);
  343. mntput(path->mnt);
  344. }
  345. EXPORT_SYMBOL(path_put);
  346. /**
  347. * nameidata_drop_rcu - drop this nameidata out of rcu-walk
  348. * @nd: nameidata pathwalk data to drop
  349. * Returns: 0 on success, -ECHILD on failure
  350. *
  351. * Path walking has 2 modes, rcu-walk and ref-walk (see
  352. * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
  353. * to drop out of rcu-walk mode and take normal reference counts on dentries
  354. * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
  355. * refcounts at the last known good point before rcu-walk got stuck, so
  356. * ref-walk may continue from there. If this is not successful (eg. a seqcount
  357. * has changed), then failure is returned and path walk restarts from the
  358. * beginning in ref-walk mode.
  359. *
  360. * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
  361. * ref-walk. Must be called from rcu-walk context.
  362. */
  363. static int nameidata_drop_rcu(struct nameidata *nd)
  364. {
  365. struct fs_struct *fs = current->fs;
  366. struct dentry *dentry = nd->path.dentry;
  367. int want_root = 0;
  368. BUG_ON(!(nd->flags & LOOKUP_RCU));
  369. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  370. want_root = 1;
  371. spin_lock(&fs->lock);
  372. if (nd->root.mnt != fs->root.mnt ||
  373. nd->root.dentry != fs->root.dentry)
  374. goto err_root;
  375. }
  376. spin_lock(&dentry->d_lock);
  377. if (!__d_rcu_to_refcount(dentry, nd->seq))
  378. goto err;
  379. BUG_ON(nd->inode != dentry->d_inode);
  380. spin_unlock(&dentry->d_lock);
  381. if (want_root) {
  382. path_get(&nd->root);
  383. spin_unlock(&fs->lock);
  384. }
  385. mntget(nd->path.mnt);
  386. rcu_read_unlock();
  387. br_read_unlock(vfsmount_lock);
  388. nd->flags &= ~LOOKUP_RCU;
  389. return 0;
  390. err:
  391. spin_unlock(&dentry->d_lock);
  392. err_root:
  393. if (want_root)
  394. spin_unlock(&fs->lock);
  395. return -ECHILD;
  396. }
  397. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  398. static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
  399. {
  400. if (nd->flags & LOOKUP_RCU)
  401. return nameidata_drop_rcu(nd);
  402. return 0;
  403. }
  404. /**
  405. * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
  406. * @nd: nameidata pathwalk data to drop
  407. * @dentry: dentry to drop
  408. * Returns: 0 on success, -ECHILD on failure
  409. *
  410. * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
  411. * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
  412. * @nd. Must be called from rcu-walk context.
  413. */
  414. static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
  415. {
  416. struct fs_struct *fs = current->fs;
  417. struct dentry *parent = nd->path.dentry;
  418. int want_root = 0;
  419. BUG_ON(!(nd->flags & LOOKUP_RCU));
  420. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  421. want_root = 1;
  422. spin_lock(&fs->lock);
  423. if (nd->root.mnt != fs->root.mnt ||
  424. nd->root.dentry != fs->root.dentry)
  425. goto err_root;
  426. }
  427. spin_lock(&parent->d_lock);
  428. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  429. if (!__d_rcu_to_refcount(dentry, nd->seq))
  430. goto err;
  431. /*
  432. * If the sequence check on the child dentry passed, then the child has
  433. * not been removed from its parent. This means the parent dentry must
  434. * be valid and able to take a reference at this point.
  435. */
  436. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  437. BUG_ON(!parent->d_count);
  438. parent->d_count++;
  439. spin_unlock(&dentry->d_lock);
  440. spin_unlock(&parent->d_lock);
  441. if (want_root) {
  442. path_get(&nd->root);
  443. spin_unlock(&fs->lock);
  444. }
  445. mntget(nd->path.mnt);
  446. rcu_read_unlock();
  447. br_read_unlock(vfsmount_lock);
  448. nd->flags &= ~LOOKUP_RCU;
  449. return 0;
  450. err:
  451. spin_unlock(&dentry->d_lock);
  452. spin_unlock(&parent->d_lock);
  453. err_root:
  454. if (want_root)
  455. spin_unlock(&fs->lock);
  456. return -ECHILD;
  457. }
  458. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  459. static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
  460. {
  461. if (nd->flags & LOOKUP_RCU) {
  462. if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
  463. nd->flags &= ~LOOKUP_RCU;
  464. if (!(nd->flags & LOOKUP_ROOT))
  465. nd->root.mnt = NULL;
  466. rcu_read_unlock();
  467. br_read_unlock(vfsmount_lock);
  468. return -ECHILD;
  469. }
  470. }
  471. return 0;
  472. }
  473. /**
  474. * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
  475. * @nd: nameidata pathwalk data to drop
  476. * Returns: 0 on success, -ECHILD on failure
  477. *
  478. * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
  479. * nd->path should be the final element of the lookup, so nd->root is discarded.
  480. * Must be called from rcu-walk context.
  481. */
  482. static int nameidata_drop_rcu_last(struct nameidata *nd)
  483. {
  484. struct dentry *dentry = nd->path.dentry;
  485. BUG_ON(!(nd->flags & LOOKUP_RCU));
  486. nd->flags &= ~LOOKUP_RCU;
  487. if (!(nd->flags & LOOKUP_ROOT))
  488. nd->root.mnt = NULL;
  489. spin_lock(&dentry->d_lock);
  490. if (!__d_rcu_to_refcount(dentry, nd->seq))
  491. goto err_unlock;
  492. BUG_ON(nd->inode != dentry->d_inode);
  493. spin_unlock(&dentry->d_lock);
  494. mntget(nd->path.mnt);
  495. rcu_read_unlock();
  496. br_read_unlock(vfsmount_lock);
  497. return 0;
  498. err_unlock:
  499. spin_unlock(&dentry->d_lock);
  500. rcu_read_unlock();
  501. br_read_unlock(vfsmount_lock);
  502. return -ECHILD;
  503. }
  504. /**
  505. * release_open_intent - free up open intent resources
  506. * @nd: pointer to nameidata
  507. */
  508. void release_open_intent(struct nameidata *nd)
  509. {
  510. struct file *file = nd->intent.open.file;
  511. if (file && !IS_ERR(file)) {
  512. if (file->f_path.dentry == NULL)
  513. put_filp(file);
  514. else
  515. fput(file);
  516. }
  517. }
  518. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  519. {
  520. return dentry->d_op->d_revalidate(dentry, nd);
  521. }
  522. static struct dentry *
  523. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  524. {
  525. int status = d_revalidate(dentry, nd);
  526. if (unlikely(status <= 0)) {
  527. /*
  528. * The dentry failed validation.
  529. * If d_revalidate returned 0 attempt to invalidate
  530. * the dentry otherwise d_revalidate is asking us
  531. * to return a fail status.
  532. */
  533. if (status < 0) {
  534. dput(dentry);
  535. dentry = ERR_PTR(status);
  536. } else if (!d_invalidate(dentry)) {
  537. dput(dentry);
  538. dentry = NULL;
  539. }
  540. }
  541. return dentry;
  542. }
  543. /*
  544. * handle_reval_path - force revalidation of a dentry
  545. *
  546. * In some situations the path walking code will trust dentries without
  547. * revalidating them. This causes problems for filesystems that depend on
  548. * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
  549. * (which indicates that it's possible for the dentry to go stale), force
  550. * a d_revalidate call before proceeding.
  551. *
  552. * Returns 0 if the revalidation was successful. If the revalidation fails,
  553. * either return the error returned by d_revalidate or -ESTALE if the
  554. * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
  555. * invalidate the dentry. It's up to the caller to handle putting references
  556. * to the path if necessary.
  557. */
  558. static inline int handle_reval_path(struct nameidata *nd)
  559. {
  560. struct dentry *dentry = nd->path.dentry;
  561. int status;
  562. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  563. return 0;
  564. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  565. return 0;
  566. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  567. return 0;
  568. /* Note: we do not d_invalidate() */
  569. status = d_revalidate(dentry, nd);
  570. if (status > 0)
  571. return 0;
  572. if (!status)
  573. status = -ESTALE;
  574. return status;
  575. }
  576. /*
  577. * Short-cut version of permission(), for calling on directories
  578. * during pathname resolution. Combines parts of permission()
  579. * and generic_permission(), and tests ONLY for MAY_EXEC permission.
  580. *
  581. * If appropriate, check DAC only. If not appropriate, or
  582. * short-cut DAC fails, then call ->permission() to do more
  583. * complete permission check.
  584. */
  585. static inline int exec_permission(struct inode *inode, unsigned int flags)
  586. {
  587. int ret;
  588. if (inode->i_op->permission) {
  589. ret = inode->i_op->permission(inode, MAY_EXEC, flags);
  590. } else {
  591. ret = acl_permission_check(inode, MAY_EXEC, flags,
  592. inode->i_op->check_acl);
  593. }
  594. if (likely(!ret))
  595. goto ok;
  596. if (ret == -ECHILD)
  597. return ret;
  598. if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
  599. goto ok;
  600. return ret;
  601. ok:
  602. return security_inode_exec_permission(inode, flags);
  603. }
  604. static __always_inline void set_root(struct nameidata *nd)
  605. {
  606. if (!nd->root.mnt)
  607. get_fs_root(current->fs, &nd->root);
  608. }
  609. static int link_path_walk(const char *, struct nameidata *);
  610. static __always_inline void set_root_rcu(struct nameidata *nd)
  611. {
  612. if (!nd->root.mnt) {
  613. struct fs_struct *fs = current->fs;
  614. unsigned seq;
  615. do {
  616. seq = read_seqcount_begin(&fs->seq);
  617. nd->root = fs->root;
  618. } while (read_seqcount_retry(&fs->seq, seq));
  619. }
  620. }
  621. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  622. {
  623. int ret;
  624. if (IS_ERR(link))
  625. goto fail;
  626. if (*link == '/') {
  627. set_root(nd);
  628. path_put(&nd->path);
  629. nd->path = nd->root;
  630. path_get(&nd->root);
  631. nd->flags |= LOOKUP_JUMPED;
  632. }
  633. nd->inode = nd->path.dentry->d_inode;
  634. ret = link_path_walk(link, nd);
  635. return ret;
  636. fail:
  637. path_put(&nd->path);
  638. return PTR_ERR(link);
  639. }
  640. static void path_put_conditional(struct path *path, struct nameidata *nd)
  641. {
  642. dput(path->dentry);
  643. if (path->mnt != nd->path.mnt)
  644. mntput(path->mnt);
  645. }
  646. static inline void path_to_nameidata(const struct path *path,
  647. struct nameidata *nd)
  648. {
  649. if (!(nd->flags & LOOKUP_RCU)) {
  650. dput(nd->path.dentry);
  651. if (nd->path.mnt != path->mnt)
  652. mntput(nd->path.mnt);
  653. }
  654. nd->path.mnt = path->mnt;
  655. nd->path.dentry = path->dentry;
  656. }
  657. static __always_inline int
  658. __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
  659. {
  660. int error;
  661. struct dentry *dentry = link->dentry;
  662. BUG_ON(nd->flags & LOOKUP_RCU);
  663. touch_atime(link->mnt, dentry);
  664. nd_set_link(nd, NULL);
  665. if (link->mnt == nd->path.mnt)
  666. mntget(link->mnt);
  667. error = security_inode_follow_link(link->dentry, nd);
  668. if (error) {
  669. *p = ERR_PTR(error); /* no ->put_link(), please */
  670. path_put(&nd->path);
  671. return error;
  672. }
  673. nd->last_type = LAST_BIND;
  674. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  675. error = PTR_ERR(*p);
  676. if (!IS_ERR(*p)) {
  677. char *s = nd_get_link(nd);
  678. error = 0;
  679. if (s)
  680. error = __vfs_follow_link(nd, s);
  681. else if (nd->last_type == LAST_BIND) {
  682. nd->flags |= LOOKUP_JUMPED;
  683. nd->inode = nd->path.dentry->d_inode;
  684. if (nd->inode->i_op->follow_link) {
  685. /* stepped on a _really_ weird one */
  686. path_put(&nd->path);
  687. error = -ELOOP;
  688. }
  689. }
  690. }
  691. return error;
  692. }
  693. static int follow_up_rcu(struct path *path)
  694. {
  695. struct vfsmount *parent;
  696. struct dentry *mountpoint;
  697. parent = path->mnt->mnt_parent;
  698. if (parent == path->mnt)
  699. return 0;
  700. mountpoint = path->mnt->mnt_mountpoint;
  701. path->dentry = mountpoint;
  702. path->mnt = parent;
  703. return 1;
  704. }
  705. int follow_up(struct path *path)
  706. {
  707. struct vfsmount *parent;
  708. struct dentry *mountpoint;
  709. br_read_lock(vfsmount_lock);
  710. parent = path->mnt->mnt_parent;
  711. if (parent == path->mnt) {
  712. br_read_unlock(vfsmount_lock);
  713. return 0;
  714. }
  715. mntget(parent);
  716. mountpoint = dget(path->mnt->mnt_mountpoint);
  717. br_read_unlock(vfsmount_lock);
  718. dput(path->dentry);
  719. path->dentry = mountpoint;
  720. mntput(path->mnt);
  721. path->mnt = parent;
  722. return 1;
  723. }
  724. /*
  725. * Perform an automount
  726. * - return -EISDIR to tell follow_managed() to stop and return the path we
  727. * were called with.
  728. */
  729. static int follow_automount(struct path *path, unsigned flags,
  730. bool *need_mntput)
  731. {
  732. struct vfsmount *mnt;
  733. int err;
  734. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  735. return -EREMOTE;
  736. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  737. * and this is the terminal part of the path.
  738. */
  739. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
  740. return -EISDIR; /* we actually want to stop here */
  741. /* We want to mount if someone is trying to open/create a file of any
  742. * type under the mountpoint, wants to traverse through the mountpoint
  743. * or wants to open the mounted directory.
  744. *
  745. * We don't want to mount if someone's just doing a stat and they've
  746. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  747. * appended a '/' to the name.
  748. */
  749. if (!(flags & LOOKUP_FOLLOW) &&
  750. !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
  751. LOOKUP_OPEN | LOOKUP_CREATE)))
  752. return -EISDIR;
  753. current->total_link_count++;
  754. if (current->total_link_count >= 40)
  755. return -ELOOP;
  756. mnt = path->dentry->d_op->d_automount(path);
  757. if (IS_ERR(mnt)) {
  758. /*
  759. * The filesystem is allowed to return -EISDIR here to indicate
  760. * it doesn't want to automount. For instance, autofs would do
  761. * this so that its userspace daemon can mount on this dentry.
  762. *
  763. * However, we can only permit this if it's a terminal point in
  764. * the path being looked up; if it wasn't then the remainder of
  765. * the path is inaccessible and we should say so.
  766. */
  767. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
  768. return -EREMOTE;
  769. return PTR_ERR(mnt);
  770. }
  771. if (!mnt) /* mount collision */
  772. return 0;
  773. err = finish_automount(mnt, path);
  774. switch (err) {
  775. case -EBUSY:
  776. /* Someone else made a mount here whilst we were busy */
  777. return 0;
  778. case 0:
  779. dput(path->dentry);
  780. if (*need_mntput)
  781. mntput(path->mnt);
  782. path->mnt = mnt;
  783. path->dentry = dget(mnt->mnt_root);
  784. *need_mntput = true;
  785. return 0;
  786. default:
  787. return err;
  788. }
  789. }
  790. /*
  791. * Handle a dentry that is managed in some way.
  792. * - Flagged for transit management (autofs)
  793. * - Flagged as mountpoint
  794. * - Flagged as automount point
  795. *
  796. * This may only be called in refwalk mode.
  797. *
  798. * Serialization is taken care of in namespace.c
  799. */
  800. static int follow_managed(struct path *path, unsigned flags)
  801. {
  802. unsigned managed;
  803. bool need_mntput = false;
  804. int ret;
  805. /* Given that we're not holding a lock here, we retain the value in a
  806. * local variable for each dentry as we look at it so that we don't see
  807. * the components of that value change under us */
  808. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  809. managed &= DCACHE_MANAGED_DENTRY,
  810. unlikely(managed != 0)) {
  811. /* Allow the filesystem to manage the transit without i_mutex
  812. * being held. */
  813. if (managed & DCACHE_MANAGE_TRANSIT) {
  814. BUG_ON(!path->dentry->d_op);
  815. BUG_ON(!path->dentry->d_op->d_manage);
  816. ret = path->dentry->d_op->d_manage(path->dentry,
  817. false, false);
  818. if (ret < 0)
  819. return ret == -EISDIR ? 0 : ret;
  820. }
  821. /* Transit to a mounted filesystem. */
  822. if (managed & DCACHE_MOUNTED) {
  823. struct vfsmount *mounted = lookup_mnt(path);
  824. if (mounted) {
  825. dput(path->dentry);
  826. if (need_mntput)
  827. mntput(path->mnt);
  828. path->mnt = mounted;
  829. path->dentry = dget(mounted->mnt_root);
  830. need_mntput = true;
  831. continue;
  832. }
  833. /* Something is mounted on this dentry in another
  834. * namespace and/or whatever was mounted there in this
  835. * namespace got unmounted before we managed to get the
  836. * vfsmount_lock */
  837. }
  838. /* Handle an automount point */
  839. if (managed & DCACHE_NEED_AUTOMOUNT) {
  840. ret = follow_automount(path, flags, &need_mntput);
  841. if (ret < 0)
  842. return ret == -EISDIR ? 0 : ret;
  843. continue;
  844. }
  845. /* We didn't change the current path point */
  846. break;
  847. }
  848. return 0;
  849. }
  850. int follow_down_one(struct path *path)
  851. {
  852. struct vfsmount *mounted;
  853. mounted = lookup_mnt(path);
  854. if (mounted) {
  855. dput(path->dentry);
  856. mntput(path->mnt);
  857. path->mnt = mounted;
  858. path->dentry = dget(mounted->mnt_root);
  859. return 1;
  860. }
  861. return 0;
  862. }
  863. /*
  864. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  865. * meet a managed dentry and we're not walking to "..". True is returned to
  866. * continue, false to abort.
  867. */
  868. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  869. struct inode **inode, bool reverse_transit)
  870. {
  871. while (d_mountpoint(path->dentry)) {
  872. struct vfsmount *mounted;
  873. if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  874. !reverse_transit &&
  875. path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
  876. return false;
  877. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  878. if (!mounted)
  879. break;
  880. path->mnt = mounted;
  881. path->dentry = mounted->mnt_root;
  882. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  883. *inode = path->dentry->d_inode;
  884. }
  885. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  886. return reverse_transit;
  887. return true;
  888. }
  889. static int follow_dotdot_rcu(struct nameidata *nd)
  890. {
  891. struct inode *inode = nd->inode;
  892. set_root_rcu(nd);
  893. while (1) {
  894. if (nd->path.dentry == nd->root.dentry &&
  895. nd->path.mnt == nd->root.mnt) {
  896. break;
  897. }
  898. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  899. struct dentry *old = nd->path.dentry;
  900. struct dentry *parent = old->d_parent;
  901. unsigned seq;
  902. seq = read_seqcount_begin(&parent->d_seq);
  903. if (read_seqcount_retry(&old->d_seq, nd->seq))
  904. goto failed;
  905. inode = parent->d_inode;
  906. nd->path.dentry = parent;
  907. nd->seq = seq;
  908. break;
  909. }
  910. if (!follow_up_rcu(&nd->path))
  911. break;
  912. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  913. inode = nd->path.dentry->d_inode;
  914. }
  915. __follow_mount_rcu(nd, &nd->path, &inode, true);
  916. nd->inode = inode;
  917. return 0;
  918. failed:
  919. nd->flags &= ~LOOKUP_RCU;
  920. if (!(nd->flags & LOOKUP_ROOT))
  921. nd->root.mnt = NULL;
  922. rcu_read_unlock();
  923. br_read_unlock(vfsmount_lock);
  924. return -ECHILD;
  925. }
  926. /*
  927. * Follow down to the covering mount currently visible to userspace. At each
  928. * point, the filesystem owning that dentry may be queried as to whether the
  929. * caller is permitted to proceed or not.
  930. *
  931. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  932. * being true).
  933. */
  934. int follow_down(struct path *path, bool mounting_here)
  935. {
  936. unsigned managed;
  937. int ret;
  938. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  939. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  940. /* Allow the filesystem to manage the transit without i_mutex
  941. * being held.
  942. *
  943. * We indicate to the filesystem if someone is trying to mount
  944. * something here. This gives autofs the chance to deny anyone
  945. * other than its daemon the right to mount on its
  946. * superstructure.
  947. *
  948. * The filesystem may sleep at this point.
  949. */
  950. if (managed & DCACHE_MANAGE_TRANSIT) {
  951. BUG_ON(!path->dentry->d_op);
  952. BUG_ON(!path->dentry->d_op->d_manage);
  953. ret = path->dentry->d_op->d_manage(
  954. path->dentry, mounting_here, false);
  955. if (ret < 0)
  956. return ret == -EISDIR ? 0 : ret;
  957. }
  958. /* Transit to a mounted filesystem. */
  959. if (managed & DCACHE_MOUNTED) {
  960. struct vfsmount *mounted = lookup_mnt(path);
  961. if (!mounted)
  962. break;
  963. dput(path->dentry);
  964. mntput(path->mnt);
  965. path->mnt = mounted;
  966. path->dentry = dget(mounted->mnt_root);
  967. continue;
  968. }
  969. /* Don't handle automount points here */
  970. break;
  971. }
  972. return 0;
  973. }
  974. /*
  975. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  976. */
  977. static void follow_mount(struct path *path)
  978. {
  979. while (d_mountpoint(path->dentry)) {
  980. struct vfsmount *mounted = lookup_mnt(path);
  981. if (!mounted)
  982. break;
  983. dput(path->dentry);
  984. mntput(path->mnt);
  985. path->mnt = mounted;
  986. path->dentry = dget(mounted->mnt_root);
  987. }
  988. }
  989. static void follow_dotdot(struct nameidata *nd)
  990. {
  991. set_root(nd);
  992. while(1) {
  993. struct dentry *old = nd->path.dentry;
  994. if (nd->path.dentry == nd->root.dentry &&
  995. nd->path.mnt == nd->root.mnt) {
  996. break;
  997. }
  998. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  999. /* rare case of legitimate dget_parent()... */
  1000. nd->path.dentry = dget_parent(nd->path.dentry);
  1001. dput(old);
  1002. break;
  1003. }
  1004. if (!follow_up(&nd->path))
  1005. break;
  1006. }
  1007. follow_mount(&nd->path);
  1008. nd->inode = nd->path.dentry->d_inode;
  1009. }
  1010. /*
  1011. * Allocate a dentry with name and parent, and perform a parent
  1012. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  1013. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  1014. * have verified that no child exists while under i_mutex.
  1015. */
  1016. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  1017. struct qstr *name, struct nameidata *nd)
  1018. {
  1019. struct inode *inode = parent->d_inode;
  1020. struct dentry *dentry;
  1021. struct dentry *old;
  1022. /* Don't create child dentry for a dead directory. */
  1023. if (unlikely(IS_DEADDIR(inode)))
  1024. return ERR_PTR(-ENOENT);
  1025. dentry = d_alloc(parent, name);
  1026. if (unlikely(!dentry))
  1027. return ERR_PTR(-ENOMEM);
  1028. old = inode->i_op->lookup(inode, dentry, nd);
  1029. if (unlikely(old)) {
  1030. dput(dentry);
  1031. dentry = old;
  1032. }
  1033. return dentry;
  1034. }
  1035. /*
  1036. * It's more convoluted than I'd like it to be, but... it's still fairly
  1037. * small and for now I'd prefer to have fast path as straight as possible.
  1038. * It _is_ time-critical.
  1039. */
  1040. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1041. struct path *path, struct inode **inode)
  1042. {
  1043. struct vfsmount *mnt = nd->path.mnt;
  1044. struct dentry *dentry, *parent = nd->path.dentry;
  1045. int need_reval = 1;
  1046. int status = 1;
  1047. int err;
  1048. /*
  1049. * Rename seqlock is not required here because in the off chance
  1050. * of a false negative due to a concurrent rename, we're going to
  1051. * do the non-racy lookup, below.
  1052. */
  1053. if (nd->flags & LOOKUP_RCU) {
  1054. unsigned seq;
  1055. *inode = nd->inode;
  1056. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1057. if (!dentry)
  1058. goto unlazy;
  1059. /* Memory barrier in read_seqcount_begin of child is enough */
  1060. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1061. return -ECHILD;
  1062. nd->seq = seq;
  1063. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1064. status = d_revalidate(dentry, nd);
  1065. if (unlikely(status <= 0)) {
  1066. if (status != -ECHILD)
  1067. need_reval = 0;
  1068. goto unlazy;
  1069. }
  1070. }
  1071. path->mnt = mnt;
  1072. path->dentry = dentry;
  1073. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1074. return 0;
  1075. unlazy:
  1076. if (dentry) {
  1077. if (nameidata_dentry_drop_rcu(nd, dentry))
  1078. return -ECHILD;
  1079. } else {
  1080. if (nameidata_drop_rcu(nd))
  1081. return -ECHILD;
  1082. }
  1083. } else {
  1084. dentry = __d_lookup(parent, name);
  1085. }
  1086. retry:
  1087. if (unlikely(!dentry)) {
  1088. struct inode *dir = parent->d_inode;
  1089. BUG_ON(nd->inode != dir);
  1090. mutex_lock(&dir->i_mutex);
  1091. dentry = d_lookup(parent, name);
  1092. if (likely(!dentry)) {
  1093. dentry = d_alloc_and_lookup(parent, name, nd);
  1094. if (IS_ERR(dentry)) {
  1095. mutex_unlock(&dir->i_mutex);
  1096. return PTR_ERR(dentry);
  1097. }
  1098. /* known good */
  1099. need_reval = 0;
  1100. status = 1;
  1101. }
  1102. mutex_unlock(&dir->i_mutex);
  1103. }
  1104. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1105. status = d_revalidate(dentry, nd);
  1106. if (unlikely(status <= 0)) {
  1107. if (status < 0) {
  1108. dput(dentry);
  1109. return status;
  1110. }
  1111. if (!d_invalidate(dentry)) {
  1112. dput(dentry);
  1113. dentry = NULL;
  1114. need_reval = 1;
  1115. goto retry;
  1116. }
  1117. }
  1118. path->mnt = mnt;
  1119. path->dentry = dentry;
  1120. err = follow_managed(path, nd->flags);
  1121. if (unlikely(err < 0)) {
  1122. path_put_conditional(path, nd);
  1123. return err;
  1124. }
  1125. *inode = path->dentry->d_inode;
  1126. return 0;
  1127. }
  1128. static inline int may_lookup(struct nameidata *nd)
  1129. {
  1130. if (nd->flags & LOOKUP_RCU) {
  1131. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1132. if (err != -ECHILD)
  1133. return err;
  1134. if (nameidata_drop_rcu(nd))
  1135. return -ECHILD;
  1136. }
  1137. return exec_permission(nd->inode, 0);
  1138. }
  1139. static inline int handle_dots(struct nameidata *nd, int type)
  1140. {
  1141. if (type == LAST_DOTDOT) {
  1142. if (nd->flags & LOOKUP_RCU) {
  1143. if (follow_dotdot_rcu(nd))
  1144. return -ECHILD;
  1145. } else
  1146. follow_dotdot(nd);
  1147. }
  1148. return 0;
  1149. }
  1150. static void terminate_walk(struct nameidata *nd)
  1151. {
  1152. if (!(nd->flags & LOOKUP_RCU)) {
  1153. path_put(&nd->path);
  1154. } else {
  1155. nd->flags &= ~LOOKUP_RCU;
  1156. if (!(nd->flags & LOOKUP_ROOT))
  1157. nd->root.mnt = NULL;
  1158. rcu_read_unlock();
  1159. br_read_unlock(vfsmount_lock);
  1160. }
  1161. }
  1162. static inline int walk_component(struct nameidata *nd, struct path *path,
  1163. struct qstr *name, int type, int follow)
  1164. {
  1165. struct inode *inode;
  1166. int err;
  1167. /*
  1168. * "." and ".." are special - ".." especially so because it has
  1169. * to be able to know about the current root directory and
  1170. * parent relationships.
  1171. */
  1172. if (unlikely(type != LAST_NORM))
  1173. return handle_dots(nd, type);
  1174. err = do_lookup(nd, name, path, &inode);
  1175. if (unlikely(err)) {
  1176. terminate_walk(nd);
  1177. return err;
  1178. }
  1179. if (!inode) {
  1180. path_to_nameidata(path, nd);
  1181. terminate_walk(nd);
  1182. return -ENOENT;
  1183. }
  1184. if (unlikely(inode->i_op->follow_link) && follow) {
  1185. if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
  1186. return -ECHILD;
  1187. BUG_ON(inode != path->dentry->d_inode);
  1188. return 1;
  1189. }
  1190. path_to_nameidata(path, nd);
  1191. nd->inode = inode;
  1192. return 0;
  1193. }
  1194. /*
  1195. * This limits recursive symlink follows to 8, while
  1196. * limiting consecutive symlinks to 40.
  1197. *
  1198. * Without that kind of total limit, nasty chains of consecutive
  1199. * symlinks can cause almost arbitrarily long lookups.
  1200. */
  1201. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1202. {
  1203. int res;
  1204. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1205. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1206. path_put_conditional(path, nd);
  1207. path_put(&nd->path);
  1208. return -ELOOP;
  1209. }
  1210. nd->depth++;
  1211. current->link_count++;
  1212. do {
  1213. struct path link = *path;
  1214. void *cookie;
  1215. if (unlikely(current->total_link_count >= 40)) {
  1216. path_put_conditional(path, nd);
  1217. path_put(&nd->path);
  1218. res = -ELOOP;
  1219. break;
  1220. }
  1221. cond_resched();
  1222. current->total_link_count++;
  1223. res = __do_follow_link(&link, nd, &cookie);
  1224. if (!res)
  1225. res = walk_component(nd, path, &nd->last,
  1226. nd->last_type, LOOKUP_FOLLOW);
  1227. if (!IS_ERR(cookie) && link.dentry->d_inode->i_op->put_link)
  1228. link.dentry->d_inode->i_op->put_link(link.dentry, nd, cookie);
  1229. path_put(&link);
  1230. } while (res > 0);
  1231. current->link_count--;
  1232. nd->depth--;
  1233. return res;
  1234. }
  1235. /*
  1236. * Name resolution.
  1237. * This is the basic name resolution function, turning a pathname into
  1238. * the final dentry. We expect 'base' to be positive and a directory.
  1239. *
  1240. * Returns 0 and nd will have valid dentry and mnt on success.
  1241. * Returns error and drops reference to input namei data on failure.
  1242. */
  1243. static int link_path_walk(const char *name, struct nameidata *nd)
  1244. {
  1245. struct path next;
  1246. int err;
  1247. unsigned int lookup_flags = nd->flags;
  1248. while (*name=='/')
  1249. name++;
  1250. if (!*name)
  1251. return 0;
  1252. /* At this point we know we have a real path component. */
  1253. for(;;) {
  1254. unsigned long hash;
  1255. struct qstr this;
  1256. unsigned int c;
  1257. int type;
  1258. nd->flags |= LOOKUP_CONTINUE;
  1259. err = may_lookup(nd);
  1260. if (err)
  1261. break;
  1262. this.name = name;
  1263. c = *(const unsigned char *)name;
  1264. hash = init_name_hash();
  1265. do {
  1266. name++;
  1267. hash = partial_name_hash(c, hash);
  1268. c = *(const unsigned char *)name;
  1269. } while (c && (c != '/'));
  1270. this.len = name - (const char *) this.name;
  1271. this.hash = end_name_hash(hash);
  1272. type = LAST_NORM;
  1273. if (this.name[0] == '.') switch (this.len) {
  1274. case 2:
  1275. if (this.name[1] == '.') {
  1276. type = LAST_DOTDOT;
  1277. nd->flags |= LOOKUP_JUMPED;
  1278. }
  1279. break;
  1280. case 1:
  1281. type = LAST_DOT;
  1282. }
  1283. if (likely(type == LAST_NORM)) {
  1284. struct dentry *parent = nd->path.dentry;
  1285. nd->flags &= ~LOOKUP_JUMPED;
  1286. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1287. err = parent->d_op->d_hash(parent, nd->inode,
  1288. &this);
  1289. if (err < 0)
  1290. break;
  1291. }
  1292. }
  1293. /* remove trailing slashes? */
  1294. if (!c)
  1295. goto last_component;
  1296. while (*++name == '/');
  1297. if (!*name)
  1298. goto last_component;
  1299. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1300. if (err < 0)
  1301. return err;
  1302. if (err) {
  1303. err = nested_symlink(&next, nd);
  1304. if (err)
  1305. return err;
  1306. }
  1307. err = -ENOTDIR;
  1308. if (!nd->inode->i_op->lookup)
  1309. break;
  1310. continue;
  1311. /* here ends the main loop */
  1312. last_component:
  1313. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1314. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1315. nd->last = this;
  1316. nd->last_type = type;
  1317. return 0;
  1318. }
  1319. terminate_walk(nd);
  1320. return err;
  1321. }
  1322. static int path_init(int dfd, const char *name, unsigned int flags,
  1323. struct nameidata *nd, struct file **fp)
  1324. {
  1325. int retval = 0;
  1326. int fput_needed;
  1327. struct file *file;
  1328. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1329. nd->flags = flags | LOOKUP_JUMPED;
  1330. nd->depth = 0;
  1331. if (flags & LOOKUP_ROOT) {
  1332. struct inode *inode = nd->root.dentry->d_inode;
  1333. if (*name) {
  1334. if (!inode->i_op->lookup)
  1335. return -ENOTDIR;
  1336. retval = inode_permission(inode, MAY_EXEC);
  1337. if (retval)
  1338. return retval;
  1339. }
  1340. nd->path = nd->root;
  1341. nd->inode = inode;
  1342. if (flags & LOOKUP_RCU) {
  1343. br_read_lock(vfsmount_lock);
  1344. rcu_read_lock();
  1345. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1346. } else {
  1347. path_get(&nd->path);
  1348. }
  1349. return 0;
  1350. }
  1351. nd->root.mnt = NULL;
  1352. if (*name=='/') {
  1353. if (flags & LOOKUP_RCU) {
  1354. br_read_lock(vfsmount_lock);
  1355. rcu_read_lock();
  1356. set_root_rcu(nd);
  1357. } else {
  1358. set_root(nd);
  1359. path_get(&nd->root);
  1360. }
  1361. nd->path = nd->root;
  1362. } else if (dfd == AT_FDCWD) {
  1363. if (flags & LOOKUP_RCU) {
  1364. struct fs_struct *fs = current->fs;
  1365. unsigned seq;
  1366. br_read_lock(vfsmount_lock);
  1367. rcu_read_lock();
  1368. do {
  1369. seq = read_seqcount_begin(&fs->seq);
  1370. nd->path = fs->pwd;
  1371. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1372. } while (read_seqcount_retry(&fs->seq, seq));
  1373. } else {
  1374. get_fs_pwd(current->fs, &nd->path);
  1375. }
  1376. } else {
  1377. struct dentry *dentry;
  1378. file = fget_raw_light(dfd, &fput_needed);
  1379. retval = -EBADF;
  1380. if (!file)
  1381. goto out_fail;
  1382. dentry = file->f_path.dentry;
  1383. if (*name) {
  1384. retval = -ENOTDIR;
  1385. if (!S_ISDIR(dentry->d_inode->i_mode))
  1386. goto fput_fail;
  1387. retval = file_permission(file, MAY_EXEC);
  1388. if (retval)
  1389. goto fput_fail;
  1390. }
  1391. nd->path = file->f_path;
  1392. if (flags & LOOKUP_RCU) {
  1393. if (fput_needed)
  1394. *fp = file;
  1395. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1396. br_read_lock(vfsmount_lock);
  1397. rcu_read_lock();
  1398. } else {
  1399. path_get(&file->f_path);
  1400. fput_light(file, fput_needed);
  1401. }
  1402. }
  1403. nd->inode = nd->path.dentry->d_inode;
  1404. return 0;
  1405. fput_fail:
  1406. fput_light(file, fput_needed);
  1407. out_fail:
  1408. return retval;
  1409. }
  1410. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1411. {
  1412. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1413. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1414. nd->flags &= ~LOOKUP_PARENT;
  1415. return walk_component(nd, path, &nd->last, nd->last_type,
  1416. nd->flags & LOOKUP_FOLLOW);
  1417. }
  1418. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1419. static int path_lookupat(int dfd, const char *name,
  1420. unsigned int flags, struct nameidata *nd)
  1421. {
  1422. struct file *base = NULL;
  1423. struct path path;
  1424. int err;
  1425. /*
  1426. * Path walking is largely split up into 2 different synchronisation
  1427. * schemes, rcu-walk and ref-walk (explained in
  1428. * Documentation/filesystems/path-lookup.txt). These share much of the
  1429. * path walk code, but some things particularly setup, cleanup, and
  1430. * following mounts are sufficiently divergent that functions are
  1431. * duplicated. Typically there is a function foo(), and its RCU
  1432. * analogue, foo_rcu().
  1433. *
  1434. * -ECHILD is the error number of choice (just to avoid clashes) that
  1435. * is returned if some aspect of an rcu-walk fails. Such an error must
  1436. * be handled by restarting a traditional ref-walk (which will always
  1437. * be able to complete).
  1438. */
  1439. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1440. if (unlikely(err))
  1441. return err;
  1442. current->total_link_count = 0;
  1443. err = link_path_walk(name, nd);
  1444. if (!err && !(flags & LOOKUP_PARENT)) {
  1445. int count = 0;
  1446. err = lookup_last(nd, &path);
  1447. while (err > 0) {
  1448. void *cookie;
  1449. struct path link = path;
  1450. struct inode *inode = link.dentry->d_inode;
  1451. if (count++ > 32) {
  1452. path_put_conditional(&path, nd);
  1453. path_put(&nd->path);
  1454. err = -ELOOP;
  1455. break;
  1456. }
  1457. cond_resched();
  1458. nd->flags |= LOOKUP_PARENT;
  1459. err = __do_follow_link(&link, nd, &cookie);
  1460. if (!err)
  1461. err = lookup_last(nd, &path);
  1462. if (!IS_ERR(cookie) && inode->i_op->put_link)
  1463. inode->i_op->put_link(link.dentry, nd, cookie);
  1464. path_put(&link);
  1465. }
  1466. }
  1467. if (nd->flags & LOOKUP_RCU) {
  1468. /* went all way through without dropping RCU */
  1469. BUG_ON(err);
  1470. if (nameidata_drop_rcu_last(nd))
  1471. err = -ECHILD;
  1472. }
  1473. if (!err)
  1474. err = handle_reval_path(nd);
  1475. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1476. if (!nd->inode->i_op->lookup) {
  1477. path_put(&nd->path);
  1478. return -ENOTDIR;
  1479. }
  1480. }
  1481. if (base)
  1482. fput(base);
  1483. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1484. path_put(&nd->root);
  1485. nd->root.mnt = NULL;
  1486. }
  1487. return err;
  1488. }
  1489. static int do_path_lookup(int dfd, const char *name,
  1490. unsigned int flags, struct nameidata *nd)
  1491. {
  1492. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1493. if (unlikely(retval == -ECHILD))
  1494. retval = path_lookupat(dfd, name, flags, nd);
  1495. if (unlikely(retval == -ESTALE))
  1496. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1497. if (likely(!retval)) {
  1498. if (unlikely(!audit_dummy_context())) {
  1499. if (nd->path.dentry && nd->inode)
  1500. audit_inode(name, nd->path.dentry);
  1501. }
  1502. }
  1503. return retval;
  1504. }
  1505. int kern_path_parent(const char *name, struct nameidata *nd)
  1506. {
  1507. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1508. }
  1509. int kern_path(const char *name, unsigned int flags, struct path *path)
  1510. {
  1511. struct nameidata nd;
  1512. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1513. if (!res)
  1514. *path = nd.path;
  1515. return res;
  1516. }
  1517. /**
  1518. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1519. * @dentry: pointer to dentry of the base directory
  1520. * @mnt: pointer to vfs mount of the base directory
  1521. * @name: pointer to file name
  1522. * @flags: lookup flags
  1523. * @nd: pointer to nameidata
  1524. */
  1525. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1526. const char *name, unsigned int flags,
  1527. struct nameidata *nd)
  1528. {
  1529. nd->root.dentry = dentry;
  1530. nd->root.mnt = mnt;
  1531. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1532. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1533. }
  1534. static struct dentry *__lookup_hash(struct qstr *name,
  1535. struct dentry *base, struct nameidata *nd)
  1536. {
  1537. struct inode *inode = base->d_inode;
  1538. struct dentry *dentry;
  1539. int err;
  1540. err = exec_permission(inode, 0);
  1541. if (err)
  1542. return ERR_PTR(err);
  1543. /*
  1544. * Don't bother with __d_lookup: callers are for creat as
  1545. * well as unlink, so a lot of the time it would cost
  1546. * a double lookup.
  1547. */
  1548. dentry = d_lookup(base, name);
  1549. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1550. dentry = do_revalidate(dentry, nd);
  1551. if (!dentry)
  1552. dentry = d_alloc_and_lookup(base, name, nd);
  1553. return dentry;
  1554. }
  1555. /*
  1556. * Restricted form of lookup. Doesn't follow links, single-component only,
  1557. * needs parent already locked. Doesn't follow mounts.
  1558. * SMP-safe.
  1559. */
  1560. static struct dentry *lookup_hash(struct nameidata *nd)
  1561. {
  1562. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1563. }
  1564. /**
  1565. * lookup_one_len - filesystem helper to lookup single pathname component
  1566. * @name: pathname component to lookup
  1567. * @base: base directory to lookup from
  1568. * @len: maximum length @len should be interpreted to
  1569. *
  1570. * Note that this routine is purely a helper for filesystem usage and should
  1571. * not be called by generic code. Also note that by using this function the
  1572. * nameidata argument is passed to the filesystem methods and a filesystem
  1573. * using this helper needs to be prepared for that.
  1574. */
  1575. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1576. {
  1577. struct qstr this;
  1578. unsigned long hash;
  1579. unsigned int c;
  1580. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1581. this.name = name;
  1582. this.len = len;
  1583. if (!len)
  1584. return ERR_PTR(-EACCES);
  1585. hash = init_name_hash();
  1586. while (len--) {
  1587. c = *(const unsigned char *)name++;
  1588. if (c == '/' || c == '\0')
  1589. return ERR_PTR(-EACCES);
  1590. hash = partial_name_hash(c, hash);
  1591. }
  1592. this.hash = end_name_hash(hash);
  1593. /*
  1594. * See if the low-level filesystem might want
  1595. * to use its own hash..
  1596. */
  1597. if (base->d_flags & DCACHE_OP_HASH) {
  1598. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1599. if (err < 0)
  1600. return ERR_PTR(err);
  1601. }
  1602. return __lookup_hash(&this, base, NULL);
  1603. }
  1604. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1605. struct path *path)
  1606. {
  1607. struct nameidata nd;
  1608. char *tmp = getname_flags(name, flags);
  1609. int err = PTR_ERR(tmp);
  1610. if (!IS_ERR(tmp)) {
  1611. BUG_ON(flags & LOOKUP_PARENT);
  1612. err = do_path_lookup(dfd, tmp, flags, &nd);
  1613. putname(tmp);
  1614. if (!err)
  1615. *path = nd.path;
  1616. }
  1617. return err;
  1618. }
  1619. static int user_path_parent(int dfd, const char __user *path,
  1620. struct nameidata *nd, char **name)
  1621. {
  1622. char *s = getname(path);
  1623. int error;
  1624. if (IS_ERR(s))
  1625. return PTR_ERR(s);
  1626. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1627. if (error)
  1628. putname(s);
  1629. else
  1630. *name = s;
  1631. return error;
  1632. }
  1633. /*
  1634. * It's inline, so penalty for filesystems that don't use sticky bit is
  1635. * minimal.
  1636. */
  1637. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1638. {
  1639. uid_t fsuid = current_fsuid();
  1640. if (!(dir->i_mode & S_ISVTX))
  1641. return 0;
  1642. if (inode->i_uid == fsuid)
  1643. return 0;
  1644. if (dir->i_uid == fsuid)
  1645. return 0;
  1646. return !capable(CAP_FOWNER);
  1647. }
  1648. /*
  1649. * Check whether we can remove a link victim from directory dir, check
  1650. * whether the type of victim is right.
  1651. * 1. We can't do it if dir is read-only (done in permission())
  1652. * 2. We should have write and exec permissions on dir
  1653. * 3. We can't remove anything from append-only dir
  1654. * 4. We can't do anything with immutable dir (done in permission())
  1655. * 5. If the sticky bit on dir is set we should either
  1656. * a. be owner of dir, or
  1657. * b. be owner of victim, or
  1658. * c. have CAP_FOWNER capability
  1659. * 6. If the victim is append-only or immutable we can't do antyhing with
  1660. * links pointing to it.
  1661. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1662. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1663. * 9. We can't remove a root or mountpoint.
  1664. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1665. * nfs_async_unlink().
  1666. */
  1667. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1668. {
  1669. int error;
  1670. if (!victim->d_inode)
  1671. return -ENOENT;
  1672. BUG_ON(victim->d_parent->d_inode != dir);
  1673. audit_inode_child(victim, dir);
  1674. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1675. if (error)
  1676. return error;
  1677. if (IS_APPEND(dir))
  1678. return -EPERM;
  1679. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1680. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1681. return -EPERM;
  1682. if (isdir) {
  1683. if (!S_ISDIR(victim->d_inode->i_mode))
  1684. return -ENOTDIR;
  1685. if (IS_ROOT(victim))
  1686. return -EBUSY;
  1687. } else if (S_ISDIR(victim->d_inode->i_mode))
  1688. return -EISDIR;
  1689. if (IS_DEADDIR(dir))
  1690. return -ENOENT;
  1691. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1692. return -EBUSY;
  1693. return 0;
  1694. }
  1695. /* Check whether we can create an object with dentry child in directory
  1696. * dir.
  1697. * 1. We can't do it if child already exists (open has special treatment for
  1698. * this case, but since we are inlined it's OK)
  1699. * 2. We can't do it if dir is read-only (done in permission())
  1700. * 3. We should have write and exec permissions on dir
  1701. * 4. We can't do it if dir is immutable (done in permission())
  1702. */
  1703. static inline int may_create(struct inode *dir, struct dentry *child)
  1704. {
  1705. if (child->d_inode)
  1706. return -EEXIST;
  1707. if (IS_DEADDIR(dir))
  1708. return -ENOENT;
  1709. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1710. }
  1711. /*
  1712. * p1 and p2 should be directories on the same fs.
  1713. */
  1714. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1715. {
  1716. struct dentry *p;
  1717. if (p1 == p2) {
  1718. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1719. return NULL;
  1720. }
  1721. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1722. p = d_ancestor(p2, p1);
  1723. if (p) {
  1724. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1725. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1726. return p;
  1727. }
  1728. p = d_ancestor(p1, p2);
  1729. if (p) {
  1730. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1731. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1732. return p;
  1733. }
  1734. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1735. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1736. return NULL;
  1737. }
  1738. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1739. {
  1740. mutex_unlock(&p1->d_inode->i_mutex);
  1741. if (p1 != p2) {
  1742. mutex_unlock(&p2->d_inode->i_mutex);
  1743. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1744. }
  1745. }
  1746. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1747. struct nameidata *nd)
  1748. {
  1749. int error = may_create(dir, dentry);
  1750. if (error)
  1751. return error;
  1752. if (!dir->i_op->create)
  1753. return -EACCES; /* shouldn't it be ENOSYS? */
  1754. mode &= S_IALLUGO;
  1755. mode |= S_IFREG;
  1756. error = security_inode_create(dir, dentry, mode);
  1757. if (error)
  1758. return error;
  1759. error = dir->i_op->create(dir, dentry, mode, nd);
  1760. if (!error)
  1761. fsnotify_create(dir, dentry);
  1762. return error;
  1763. }
  1764. static int may_open(struct path *path, int acc_mode, int flag)
  1765. {
  1766. struct dentry *dentry = path->dentry;
  1767. struct inode *inode = dentry->d_inode;
  1768. int error;
  1769. /* O_PATH? */
  1770. if (!acc_mode)
  1771. return 0;
  1772. if (!inode)
  1773. return -ENOENT;
  1774. switch (inode->i_mode & S_IFMT) {
  1775. case S_IFLNK:
  1776. return -ELOOP;
  1777. case S_IFDIR:
  1778. if (acc_mode & MAY_WRITE)
  1779. return -EISDIR;
  1780. break;
  1781. case S_IFBLK:
  1782. case S_IFCHR:
  1783. if (path->mnt->mnt_flags & MNT_NODEV)
  1784. return -EACCES;
  1785. /*FALLTHRU*/
  1786. case S_IFIFO:
  1787. case S_IFSOCK:
  1788. flag &= ~O_TRUNC;
  1789. break;
  1790. }
  1791. error = inode_permission(inode, acc_mode);
  1792. if (error)
  1793. return error;
  1794. /*
  1795. * An append-only file must be opened in append mode for writing.
  1796. */
  1797. if (IS_APPEND(inode)) {
  1798. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1799. return -EPERM;
  1800. if (flag & O_TRUNC)
  1801. return -EPERM;
  1802. }
  1803. /* O_NOATIME can only be set by the owner or superuser */
  1804. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1805. return -EPERM;
  1806. /*
  1807. * Ensure there are no outstanding leases on the file.
  1808. */
  1809. return break_lease(inode, flag);
  1810. }
  1811. static int handle_truncate(struct file *filp)
  1812. {
  1813. struct path *path = &filp->f_path;
  1814. struct inode *inode = path->dentry->d_inode;
  1815. int error = get_write_access(inode);
  1816. if (error)
  1817. return error;
  1818. /*
  1819. * Refuse to truncate files with mandatory locks held on them.
  1820. */
  1821. error = locks_verify_locked(inode);
  1822. if (!error)
  1823. error = security_path_truncate(path);
  1824. if (!error) {
  1825. error = do_truncate(path->dentry, 0,
  1826. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1827. filp);
  1828. }
  1829. put_write_access(inode);
  1830. return error;
  1831. }
  1832. /*
  1833. * Note that while the flag value (low two bits) for sys_open means:
  1834. * 00 - read-only
  1835. * 01 - write-only
  1836. * 10 - read-write
  1837. * 11 - special
  1838. * it is changed into
  1839. * 00 - no permissions needed
  1840. * 01 - read-permission
  1841. * 10 - write-permission
  1842. * 11 - read-write
  1843. * for the internal routines (ie open_namei()/follow_link() etc)
  1844. * This is more logical, and also allows the 00 "no perm needed"
  1845. * to be used for symlinks (where the permissions are checked
  1846. * later).
  1847. *
  1848. */
  1849. static inline int open_to_namei_flags(int flag)
  1850. {
  1851. if ((flag+1) & O_ACCMODE)
  1852. flag++;
  1853. return flag;
  1854. }
  1855. /*
  1856. * Handle the last step of open()
  1857. */
  1858. static struct file *do_last(struct nameidata *nd, struct path *path,
  1859. const struct open_flags *op, const char *pathname)
  1860. {
  1861. struct dentry *dir = nd->path.dentry;
  1862. struct dentry *dentry;
  1863. int open_flag = op->open_flag;
  1864. int will_truncate = open_flag & O_TRUNC;
  1865. int want_write = 0;
  1866. int acc_mode = op->acc_mode;
  1867. struct file *filp;
  1868. int error;
  1869. nd->flags &= ~LOOKUP_PARENT;
  1870. nd->flags |= op->intent;
  1871. switch (nd->last_type) {
  1872. case LAST_DOTDOT:
  1873. case LAST_DOT:
  1874. error = handle_dots(nd, nd->last_type);
  1875. if (error)
  1876. return ERR_PTR(error);
  1877. /* fallthrough */
  1878. case LAST_ROOT:
  1879. if (nd->flags & LOOKUP_RCU) {
  1880. if (nameidata_drop_rcu_last(nd))
  1881. return ERR_PTR(-ECHILD);
  1882. }
  1883. error = handle_reval_path(nd);
  1884. if (error)
  1885. goto exit;
  1886. audit_inode(pathname, nd->path.dentry);
  1887. if (open_flag & O_CREAT) {
  1888. error = -EISDIR;
  1889. goto exit;
  1890. }
  1891. goto ok;
  1892. case LAST_BIND:
  1893. /* can't be RCU mode here */
  1894. error = handle_reval_path(nd);
  1895. if (error)
  1896. goto exit;
  1897. audit_inode(pathname, dir);
  1898. goto ok;
  1899. }
  1900. if (!(open_flag & O_CREAT)) {
  1901. int symlink_ok = 0;
  1902. if (nd->last.name[nd->last.len])
  1903. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1904. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1905. symlink_ok = 1;
  1906. /* we _can_ be in RCU mode here */
  1907. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1908. !symlink_ok);
  1909. if (error < 0)
  1910. return ERR_PTR(error);
  1911. if (error) /* symlink */
  1912. return NULL;
  1913. /* sayonara */
  1914. if (nd->flags & LOOKUP_RCU) {
  1915. if (nameidata_drop_rcu_last(nd))
  1916. return ERR_PTR(-ECHILD);
  1917. }
  1918. error = -ENOTDIR;
  1919. if (nd->flags & LOOKUP_DIRECTORY) {
  1920. if (!nd->inode->i_op->lookup)
  1921. goto exit;
  1922. }
  1923. audit_inode(pathname, nd->path.dentry);
  1924. goto ok;
  1925. }
  1926. /* create side of things */
  1927. if (nd->flags & LOOKUP_RCU) {
  1928. if (nameidata_drop_rcu_last(nd))
  1929. return ERR_PTR(-ECHILD);
  1930. }
  1931. audit_inode(pathname, dir);
  1932. error = -EISDIR;
  1933. /* trailing slashes? */
  1934. if (nd->last.name[nd->last.len])
  1935. goto exit;
  1936. mutex_lock(&dir->d_inode->i_mutex);
  1937. dentry = lookup_hash(nd);
  1938. error = PTR_ERR(dentry);
  1939. if (IS_ERR(dentry)) {
  1940. mutex_unlock(&dir->d_inode->i_mutex);
  1941. goto exit;
  1942. }
  1943. path->dentry = dentry;
  1944. path->mnt = nd->path.mnt;
  1945. /* Negative dentry, just create the file */
  1946. if (!dentry->d_inode) {
  1947. int mode = op->mode;
  1948. if (!IS_POSIXACL(dir->d_inode))
  1949. mode &= ~current_umask();
  1950. /*
  1951. * This write is needed to ensure that a
  1952. * rw->ro transition does not occur between
  1953. * the time when the file is created and when
  1954. * a permanent write count is taken through
  1955. * the 'struct file' in nameidata_to_filp().
  1956. */
  1957. error = mnt_want_write(nd->path.mnt);
  1958. if (error)
  1959. goto exit_mutex_unlock;
  1960. want_write = 1;
  1961. /* Don't check for write permission, don't truncate */
  1962. open_flag &= ~O_TRUNC;
  1963. will_truncate = 0;
  1964. acc_mode = MAY_OPEN;
  1965. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1966. if (error)
  1967. goto exit_mutex_unlock;
  1968. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1969. if (error)
  1970. goto exit_mutex_unlock;
  1971. mutex_unlock(&dir->d_inode->i_mutex);
  1972. dput(nd->path.dentry);
  1973. nd->path.dentry = dentry;
  1974. goto common;
  1975. }
  1976. /*
  1977. * It already exists.
  1978. */
  1979. mutex_unlock(&dir->d_inode->i_mutex);
  1980. audit_inode(pathname, path->dentry);
  1981. error = -EEXIST;
  1982. if (open_flag & O_EXCL)
  1983. goto exit_dput;
  1984. error = follow_managed(path, nd->flags);
  1985. if (error < 0)
  1986. goto exit_dput;
  1987. error = -ENOENT;
  1988. if (!path->dentry->d_inode)
  1989. goto exit_dput;
  1990. if (path->dentry->d_inode->i_op->follow_link)
  1991. return NULL;
  1992. path_to_nameidata(path, nd);
  1993. nd->inode = path->dentry->d_inode;
  1994. error = -EISDIR;
  1995. if (S_ISDIR(nd->inode->i_mode))
  1996. goto exit;
  1997. ok:
  1998. if (!S_ISREG(nd->inode->i_mode))
  1999. will_truncate = 0;
  2000. if (will_truncate) {
  2001. error = mnt_want_write(nd->path.mnt);
  2002. if (error)
  2003. goto exit;
  2004. want_write = 1;
  2005. }
  2006. common:
  2007. error = may_open(&nd->path, acc_mode, open_flag);
  2008. if (error)
  2009. goto exit;
  2010. filp = nameidata_to_filp(nd);
  2011. if (!IS_ERR(filp)) {
  2012. error = ima_file_check(filp, op->acc_mode);
  2013. if (error) {
  2014. fput(filp);
  2015. filp = ERR_PTR(error);
  2016. }
  2017. }
  2018. if (!IS_ERR(filp)) {
  2019. if (will_truncate) {
  2020. error = handle_truncate(filp);
  2021. if (error) {
  2022. fput(filp);
  2023. filp = ERR_PTR(error);
  2024. }
  2025. }
  2026. }
  2027. out:
  2028. if (want_write)
  2029. mnt_drop_write(nd->path.mnt);
  2030. path_put(&nd->path);
  2031. return filp;
  2032. exit_mutex_unlock:
  2033. mutex_unlock(&dir->d_inode->i_mutex);
  2034. exit_dput:
  2035. path_put_conditional(path, nd);
  2036. exit:
  2037. filp = ERR_PTR(error);
  2038. goto out;
  2039. }
  2040. static struct file *path_openat(int dfd, const char *pathname,
  2041. struct nameidata *nd, const struct open_flags *op, int flags)
  2042. {
  2043. struct file *base = NULL;
  2044. struct file *filp;
  2045. struct path path;
  2046. int count = 0;
  2047. int error;
  2048. filp = get_empty_filp();
  2049. if (!filp)
  2050. return ERR_PTR(-ENFILE);
  2051. filp->f_flags = op->open_flag;
  2052. nd->intent.open.file = filp;
  2053. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2054. nd->intent.open.create_mode = op->mode;
  2055. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2056. if (unlikely(error))
  2057. goto out_filp;
  2058. current->total_link_count = 0;
  2059. error = link_path_walk(pathname, nd);
  2060. if (unlikely(error))
  2061. goto out_filp;
  2062. filp = do_last(nd, &path, op, pathname);
  2063. while (unlikely(!filp)) { /* trailing symlink */
  2064. struct path link = path;
  2065. struct inode *linki = link.dentry->d_inode;
  2066. void *cookie;
  2067. if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
  2068. path_put_conditional(&path, nd);
  2069. path_put(&nd->path);
  2070. filp = ERR_PTR(-ELOOP);
  2071. break;
  2072. }
  2073. /*
  2074. * This is subtle. Instead of calling do_follow_link() we do
  2075. * the thing by hands. The reason is that this way we have zero
  2076. * link_count and path_walk() (called from ->follow_link)
  2077. * honoring LOOKUP_PARENT. After that we have the parent and
  2078. * last component, i.e. we are in the same situation as after
  2079. * the first path_walk(). Well, almost - if the last component
  2080. * is normal we get its copy stored in nd->last.name and we will
  2081. * have to putname() it when we are done. Procfs-like symlinks
  2082. * just set LAST_BIND.
  2083. */
  2084. nd->flags |= LOOKUP_PARENT;
  2085. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2086. error = __do_follow_link(&link, nd, &cookie);
  2087. if (unlikely(error))
  2088. filp = ERR_PTR(error);
  2089. else
  2090. filp = do_last(nd, &path, op, pathname);
  2091. if (!IS_ERR(cookie) && linki->i_op->put_link)
  2092. linki->i_op->put_link(link.dentry, nd, cookie);
  2093. path_put(&link);
  2094. }
  2095. out:
  2096. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2097. path_put(&nd->root);
  2098. if (base)
  2099. fput(base);
  2100. release_open_intent(nd);
  2101. return filp;
  2102. out_filp:
  2103. filp = ERR_PTR(error);
  2104. goto out;
  2105. }
  2106. struct file *do_filp_open(int dfd, const char *pathname,
  2107. const struct open_flags *op, int flags)
  2108. {
  2109. struct nameidata nd;
  2110. struct file *filp;
  2111. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2112. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2113. filp = path_openat(dfd, pathname, &nd, op, flags);
  2114. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2115. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2116. return filp;
  2117. }
  2118. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2119. const char *name, const struct open_flags *op, int flags)
  2120. {
  2121. struct nameidata nd;
  2122. struct file *file;
  2123. nd.root.mnt = mnt;
  2124. nd.root.dentry = dentry;
  2125. flags |= LOOKUP_ROOT;
  2126. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2127. return ERR_PTR(-ELOOP);
  2128. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2129. if (unlikely(file == ERR_PTR(-ECHILD)))
  2130. file = path_openat(-1, name, &nd, op, flags);
  2131. if (unlikely(file == ERR_PTR(-ESTALE)))
  2132. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2133. return file;
  2134. }
  2135. /**
  2136. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2137. * @nd: nameidata info
  2138. * @is_dir: directory flag
  2139. *
  2140. * Simple function to lookup and return a dentry and create it
  2141. * if it doesn't exist. Is SMP-safe.
  2142. *
  2143. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2144. */
  2145. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2146. {
  2147. struct dentry *dentry = ERR_PTR(-EEXIST);
  2148. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2149. /*
  2150. * Yucky last component or no last component at all?
  2151. * (foo/., foo/.., /////)
  2152. */
  2153. if (nd->last_type != LAST_NORM)
  2154. goto fail;
  2155. nd->flags &= ~LOOKUP_PARENT;
  2156. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2157. nd->intent.open.flags = O_EXCL;
  2158. /*
  2159. * Do the final lookup.
  2160. */
  2161. dentry = lookup_hash(nd);
  2162. if (IS_ERR(dentry))
  2163. goto fail;
  2164. if (dentry->d_inode)
  2165. goto eexist;
  2166. /*
  2167. * Special case - lookup gave negative, but... we had foo/bar/
  2168. * From the vfs_mknod() POV we just have a negative dentry -
  2169. * all is fine. Let's be bastards - you had / on the end, you've
  2170. * been asking for (non-existent) directory. -ENOENT for you.
  2171. */
  2172. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2173. dput(dentry);
  2174. dentry = ERR_PTR(-ENOENT);
  2175. }
  2176. return dentry;
  2177. eexist:
  2178. dput(dentry);
  2179. dentry = ERR_PTR(-EEXIST);
  2180. fail:
  2181. return dentry;
  2182. }
  2183. EXPORT_SYMBOL_GPL(lookup_create);
  2184. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2185. {
  2186. int error = may_create(dir, dentry);
  2187. if (error)
  2188. return error;
  2189. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2190. return -EPERM;
  2191. if (!dir->i_op->mknod)
  2192. return -EPERM;
  2193. error = devcgroup_inode_mknod(mode, dev);
  2194. if (error)
  2195. return error;
  2196. error = security_inode_mknod(dir, dentry, mode, dev);
  2197. if (error)
  2198. return error;
  2199. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2200. if (!error)
  2201. fsnotify_create(dir, dentry);
  2202. return error;
  2203. }
  2204. static int may_mknod(mode_t mode)
  2205. {
  2206. switch (mode & S_IFMT) {
  2207. case S_IFREG:
  2208. case S_IFCHR:
  2209. case S_IFBLK:
  2210. case S_IFIFO:
  2211. case S_IFSOCK:
  2212. case 0: /* zero mode translates to S_IFREG */
  2213. return 0;
  2214. case S_IFDIR:
  2215. return -EPERM;
  2216. default:
  2217. return -EINVAL;
  2218. }
  2219. }
  2220. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2221. unsigned, dev)
  2222. {
  2223. int error;
  2224. char *tmp;
  2225. struct dentry *dentry;
  2226. struct nameidata nd;
  2227. if (S_ISDIR(mode))
  2228. return -EPERM;
  2229. error = user_path_parent(dfd, filename, &nd, &tmp);
  2230. if (error)
  2231. return error;
  2232. dentry = lookup_create(&nd, 0);
  2233. if (IS_ERR(dentry)) {
  2234. error = PTR_ERR(dentry);
  2235. goto out_unlock;
  2236. }
  2237. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2238. mode &= ~current_umask();
  2239. error = may_mknod(mode);
  2240. if (error)
  2241. goto out_dput;
  2242. error = mnt_want_write(nd.path.mnt);
  2243. if (error)
  2244. goto out_dput;
  2245. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2246. if (error)
  2247. goto out_drop_write;
  2248. switch (mode & S_IFMT) {
  2249. case 0: case S_IFREG:
  2250. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2251. break;
  2252. case S_IFCHR: case S_IFBLK:
  2253. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2254. new_decode_dev(dev));
  2255. break;
  2256. case S_IFIFO: case S_IFSOCK:
  2257. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2258. break;
  2259. }
  2260. out_drop_write:
  2261. mnt_drop_write(nd.path.mnt);
  2262. out_dput:
  2263. dput(dentry);
  2264. out_unlock:
  2265. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2266. path_put(&nd.path);
  2267. putname(tmp);
  2268. return error;
  2269. }
  2270. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2271. {
  2272. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2273. }
  2274. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2275. {
  2276. int error = may_create(dir, dentry);
  2277. if (error)
  2278. return error;
  2279. if (!dir->i_op->mkdir)
  2280. return -EPERM;
  2281. mode &= (S_IRWXUGO|S_ISVTX);
  2282. error = security_inode_mkdir(dir, dentry, mode);
  2283. if (error)
  2284. return error;
  2285. error = dir->i_op->mkdir(dir, dentry, mode);
  2286. if (!error)
  2287. fsnotify_mkdir(dir, dentry);
  2288. return error;
  2289. }
  2290. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2291. {
  2292. int error = 0;
  2293. char * tmp;
  2294. struct dentry *dentry;
  2295. struct nameidata nd;
  2296. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2297. if (error)
  2298. goto out_err;
  2299. dentry = lookup_create(&nd, 1);
  2300. error = PTR_ERR(dentry);
  2301. if (IS_ERR(dentry))
  2302. goto out_unlock;
  2303. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2304. mode &= ~current_umask();
  2305. error = mnt_want_write(nd.path.mnt);
  2306. if (error)
  2307. goto out_dput;
  2308. error = security_path_mkdir(&nd.path, dentry, mode);
  2309. if (error)
  2310. goto out_drop_write;
  2311. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2312. out_drop_write:
  2313. mnt_drop_write(nd.path.mnt);
  2314. out_dput:
  2315. dput(dentry);
  2316. out_unlock:
  2317. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2318. path_put(&nd.path);
  2319. putname(tmp);
  2320. out_err:
  2321. return error;
  2322. }
  2323. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2324. {
  2325. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2326. }
  2327. /*
  2328. * We try to drop the dentry early: we should have
  2329. * a usage count of 2 if we're the only user of this
  2330. * dentry, and if that is true (possibly after pruning
  2331. * the dcache), then we drop the dentry now.
  2332. *
  2333. * A low-level filesystem can, if it choses, legally
  2334. * do a
  2335. *
  2336. * if (!d_unhashed(dentry))
  2337. * return -EBUSY;
  2338. *
  2339. * if it cannot handle the case of removing a directory
  2340. * that is still in use by something else..
  2341. */
  2342. void dentry_unhash(struct dentry *dentry)
  2343. {
  2344. dget(dentry);
  2345. shrink_dcache_parent(dentry);
  2346. spin_lock(&dentry->d_lock);
  2347. if (dentry->d_count == 2)
  2348. __d_drop(dentry);
  2349. spin_unlock(&dentry->d_lock);
  2350. }
  2351. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2352. {
  2353. int error = may_delete(dir, dentry, 1);
  2354. if (error)
  2355. return error;
  2356. if (!dir->i_op->rmdir)
  2357. return -EPERM;
  2358. mutex_lock(&dentry->d_inode->i_mutex);
  2359. dentry_unhash(dentry);
  2360. if (d_mountpoint(dentry))
  2361. error = -EBUSY;
  2362. else {
  2363. error = security_inode_rmdir(dir, dentry);
  2364. if (!error) {
  2365. error = dir->i_op->rmdir(dir, dentry);
  2366. if (!error) {
  2367. dentry->d_inode->i_flags |= S_DEAD;
  2368. dont_mount(dentry);
  2369. }
  2370. }
  2371. }
  2372. mutex_unlock(&dentry->d_inode->i_mutex);
  2373. if (!error) {
  2374. d_delete(dentry);
  2375. }
  2376. dput(dentry);
  2377. return error;
  2378. }
  2379. static long do_rmdir(int dfd, const char __user *pathname)
  2380. {
  2381. int error = 0;
  2382. char * name;
  2383. struct dentry *dentry;
  2384. struct nameidata nd;
  2385. error = user_path_parent(dfd, pathname, &nd, &name);
  2386. if (error)
  2387. return error;
  2388. switch(nd.last_type) {
  2389. case LAST_DOTDOT:
  2390. error = -ENOTEMPTY;
  2391. goto exit1;
  2392. case LAST_DOT:
  2393. error = -EINVAL;
  2394. goto exit1;
  2395. case LAST_ROOT:
  2396. error = -EBUSY;
  2397. goto exit1;
  2398. }
  2399. nd.flags &= ~LOOKUP_PARENT;
  2400. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2401. dentry = lookup_hash(&nd);
  2402. error = PTR_ERR(dentry);
  2403. if (IS_ERR(dentry))
  2404. goto exit2;
  2405. error = mnt_want_write(nd.path.mnt);
  2406. if (error)
  2407. goto exit3;
  2408. error = security_path_rmdir(&nd.path, dentry);
  2409. if (error)
  2410. goto exit4;
  2411. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2412. exit4:
  2413. mnt_drop_write(nd.path.mnt);
  2414. exit3:
  2415. dput(dentry);
  2416. exit2:
  2417. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2418. exit1:
  2419. path_put(&nd.path);
  2420. putname(name);
  2421. return error;
  2422. }
  2423. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2424. {
  2425. return do_rmdir(AT_FDCWD, pathname);
  2426. }
  2427. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2428. {
  2429. int error = may_delete(dir, dentry, 0);
  2430. if (error)
  2431. return error;
  2432. if (!dir->i_op->unlink)
  2433. return -EPERM;
  2434. mutex_lock(&dentry->d_inode->i_mutex);
  2435. if (d_mountpoint(dentry))
  2436. error = -EBUSY;
  2437. else {
  2438. error = security_inode_unlink(dir, dentry);
  2439. if (!error) {
  2440. error = dir->i_op->unlink(dir, dentry);
  2441. if (!error)
  2442. dont_mount(dentry);
  2443. }
  2444. }
  2445. mutex_unlock(&dentry->d_inode->i_mutex);
  2446. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2447. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2448. fsnotify_link_count(dentry->d_inode);
  2449. d_delete(dentry);
  2450. }
  2451. return error;
  2452. }
  2453. /*
  2454. * Make sure that the actual truncation of the file will occur outside its
  2455. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2456. * writeout happening, and we don't want to prevent access to the directory
  2457. * while waiting on the I/O.
  2458. */
  2459. static long do_unlinkat(int dfd, const char __user *pathname)
  2460. {
  2461. int error;
  2462. char *name;
  2463. struct dentry *dentry;
  2464. struct nameidata nd;
  2465. struct inode *inode = NULL;
  2466. error = user_path_parent(dfd, pathname, &nd, &name);
  2467. if (error)
  2468. return error;
  2469. error = -EISDIR;
  2470. if (nd.last_type != LAST_NORM)
  2471. goto exit1;
  2472. nd.flags &= ~LOOKUP_PARENT;
  2473. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2474. dentry = lookup_hash(&nd);
  2475. error = PTR_ERR(dentry);
  2476. if (!IS_ERR(dentry)) {
  2477. /* Why not before? Because we want correct error value */
  2478. if (nd.last.name[nd.last.len])
  2479. goto slashes;
  2480. inode = dentry->d_inode;
  2481. if (inode)
  2482. ihold(inode);
  2483. error = mnt_want_write(nd.path.mnt);
  2484. if (error)
  2485. goto exit2;
  2486. error = security_path_unlink(&nd.path, dentry);
  2487. if (error)
  2488. goto exit3;
  2489. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2490. exit3:
  2491. mnt_drop_write(nd.path.mnt);
  2492. exit2:
  2493. dput(dentry);
  2494. }
  2495. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2496. if (inode)
  2497. iput(inode); /* truncate the inode here */
  2498. exit1:
  2499. path_put(&nd.path);
  2500. putname(name);
  2501. return error;
  2502. slashes:
  2503. error = !dentry->d_inode ? -ENOENT :
  2504. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2505. goto exit2;
  2506. }
  2507. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2508. {
  2509. if ((flag & ~AT_REMOVEDIR) != 0)
  2510. return -EINVAL;
  2511. if (flag & AT_REMOVEDIR)
  2512. return do_rmdir(dfd, pathname);
  2513. return do_unlinkat(dfd, pathname);
  2514. }
  2515. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2516. {
  2517. return do_unlinkat(AT_FDCWD, pathname);
  2518. }
  2519. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2520. {
  2521. int error = may_create(dir, dentry);
  2522. if (error)
  2523. return error;
  2524. if (!dir->i_op->symlink)
  2525. return -EPERM;
  2526. error = security_inode_symlink(dir, dentry, oldname);
  2527. if (error)
  2528. return error;
  2529. error = dir->i_op->symlink(dir, dentry, oldname);
  2530. if (!error)
  2531. fsnotify_create(dir, dentry);
  2532. return error;
  2533. }
  2534. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2535. int, newdfd, const char __user *, newname)
  2536. {
  2537. int error;
  2538. char *from;
  2539. char *to;
  2540. struct dentry *dentry;
  2541. struct nameidata nd;
  2542. from = getname(oldname);
  2543. if (IS_ERR(from))
  2544. return PTR_ERR(from);
  2545. error = user_path_parent(newdfd, newname, &nd, &to);
  2546. if (error)
  2547. goto out_putname;
  2548. dentry = lookup_create(&nd, 0);
  2549. error = PTR_ERR(dentry);
  2550. if (IS_ERR(dentry))
  2551. goto out_unlock;
  2552. error = mnt_want_write(nd.path.mnt);
  2553. if (error)
  2554. goto out_dput;
  2555. error = security_path_symlink(&nd.path, dentry, from);
  2556. if (error)
  2557. goto out_drop_write;
  2558. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2559. out_drop_write:
  2560. mnt_drop_write(nd.path.mnt);
  2561. out_dput:
  2562. dput(dentry);
  2563. out_unlock:
  2564. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2565. path_put(&nd.path);
  2566. putname(to);
  2567. out_putname:
  2568. putname(from);
  2569. return error;
  2570. }
  2571. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2572. {
  2573. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2574. }
  2575. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2576. {
  2577. struct inode *inode = old_dentry->d_inode;
  2578. int error;
  2579. if (!inode)
  2580. return -ENOENT;
  2581. error = may_create(dir, new_dentry);
  2582. if (error)
  2583. return error;
  2584. if (dir->i_sb != inode->i_sb)
  2585. return -EXDEV;
  2586. /*
  2587. * A link to an append-only or immutable file cannot be created.
  2588. */
  2589. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2590. return -EPERM;
  2591. if (!dir->i_op->link)
  2592. return -EPERM;
  2593. if (S_ISDIR(inode->i_mode))
  2594. return -EPERM;
  2595. error = security_inode_link(old_dentry, dir, new_dentry);
  2596. if (error)
  2597. return error;
  2598. mutex_lock(&inode->i_mutex);
  2599. /* Make sure we don't allow creating hardlink to an unlinked file */
  2600. if (inode->i_nlink == 0)
  2601. error = -ENOENT;
  2602. else
  2603. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2604. mutex_unlock(&inode->i_mutex);
  2605. if (!error)
  2606. fsnotify_link(dir, inode, new_dentry);
  2607. return error;
  2608. }
  2609. /*
  2610. * Hardlinks are often used in delicate situations. We avoid
  2611. * security-related surprises by not following symlinks on the
  2612. * newname. --KAB
  2613. *
  2614. * We don't follow them on the oldname either to be compatible
  2615. * with linux 2.0, and to avoid hard-linking to directories
  2616. * and other special files. --ADM
  2617. */
  2618. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2619. int, newdfd, const char __user *, newname, int, flags)
  2620. {
  2621. struct dentry *new_dentry;
  2622. struct nameidata nd;
  2623. struct path old_path;
  2624. int how = 0;
  2625. int error;
  2626. char *to;
  2627. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2628. return -EINVAL;
  2629. /*
  2630. * To use null names we require CAP_DAC_READ_SEARCH
  2631. * This ensures that not everyone will be able to create
  2632. * handlink using the passed filedescriptor.
  2633. */
  2634. if (flags & AT_EMPTY_PATH) {
  2635. if (!capable(CAP_DAC_READ_SEARCH))
  2636. return -ENOENT;
  2637. how = LOOKUP_EMPTY;
  2638. }
  2639. if (flags & AT_SYMLINK_FOLLOW)
  2640. how |= LOOKUP_FOLLOW;
  2641. error = user_path_at(olddfd, oldname, how, &old_path);
  2642. if (error)
  2643. return error;
  2644. error = user_path_parent(newdfd, newname, &nd, &to);
  2645. if (error)
  2646. goto out;
  2647. error = -EXDEV;
  2648. if (old_path.mnt != nd.path.mnt)
  2649. goto out_release;
  2650. new_dentry = lookup_create(&nd, 0);
  2651. error = PTR_ERR(new_dentry);
  2652. if (IS_ERR(new_dentry))
  2653. goto out_unlock;
  2654. error = mnt_want_write(nd.path.mnt);
  2655. if (error)
  2656. goto out_dput;
  2657. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2658. if (error)
  2659. goto out_drop_write;
  2660. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2661. out_drop_write:
  2662. mnt_drop_write(nd.path.mnt);
  2663. out_dput:
  2664. dput(new_dentry);
  2665. out_unlock:
  2666. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2667. out_release:
  2668. path_put(&nd.path);
  2669. putname(to);
  2670. out:
  2671. path_put(&old_path);
  2672. return error;
  2673. }
  2674. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2675. {
  2676. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2677. }
  2678. /*
  2679. * The worst of all namespace operations - renaming directory. "Perverted"
  2680. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2681. * Problems:
  2682. * a) we can get into loop creation. Check is done in is_subdir().
  2683. * b) race potential - two innocent renames can create a loop together.
  2684. * That's where 4.4 screws up. Current fix: serialization on
  2685. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2686. * story.
  2687. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2688. * And that - after we got ->i_mutex on parents (until then we don't know
  2689. * whether the target exists). Solution: try to be smart with locking
  2690. * order for inodes. We rely on the fact that tree topology may change
  2691. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2692. * move will be locked. Thus we can rank directories by the tree
  2693. * (ancestors first) and rank all non-directories after them.
  2694. * That works since everybody except rename does "lock parent, lookup,
  2695. * lock child" and rename is under ->s_vfs_rename_mutex.
  2696. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2697. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2698. * we'd better make sure that there's no link(2) for them.
  2699. * d) some filesystems don't support opened-but-unlinked directories,
  2700. * either because of layout or because they are not ready to deal with
  2701. * all cases correctly. The latter will be fixed (taking this sort of
  2702. * stuff into VFS), but the former is not going away. Solution: the same
  2703. * trick as in rmdir().
  2704. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2705. * we are removing the target. Solution: we will have to grab ->i_mutex
  2706. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2707. * ->i_mutex on parents, which works but leads to some truly excessive
  2708. * locking].
  2709. */
  2710. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2711. struct inode *new_dir, struct dentry *new_dentry)
  2712. {
  2713. int error = 0;
  2714. struct inode *target;
  2715. /*
  2716. * If we are going to change the parent - check write permissions,
  2717. * we'll need to flip '..'.
  2718. */
  2719. if (new_dir != old_dir) {
  2720. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2721. if (error)
  2722. return error;
  2723. }
  2724. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2725. if (error)
  2726. return error;
  2727. target = new_dentry->d_inode;
  2728. if (target)
  2729. mutex_lock(&target->i_mutex);
  2730. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2731. error = -EBUSY;
  2732. else {
  2733. if (target)
  2734. dentry_unhash(new_dentry);
  2735. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2736. }
  2737. if (target) {
  2738. if (!error) {
  2739. target->i_flags |= S_DEAD;
  2740. dont_mount(new_dentry);
  2741. }
  2742. mutex_unlock(&target->i_mutex);
  2743. if (d_unhashed(new_dentry))
  2744. d_rehash(new_dentry);
  2745. dput(new_dentry);
  2746. }
  2747. if (!error)
  2748. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2749. d_move(old_dentry,new_dentry);
  2750. return error;
  2751. }
  2752. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2753. struct inode *new_dir, struct dentry *new_dentry)
  2754. {
  2755. struct inode *target;
  2756. int error;
  2757. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2758. if (error)
  2759. return error;
  2760. dget(new_dentry);
  2761. target = new_dentry->d_inode;
  2762. if (target)
  2763. mutex_lock(&target->i_mutex);
  2764. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2765. error = -EBUSY;
  2766. else
  2767. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2768. if (!error) {
  2769. if (target)
  2770. dont_mount(new_dentry);
  2771. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2772. d_move(old_dentry, new_dentry);
  2773. }
  2774. if (target)
  2775. mutex_unlock(&target->i_mutex);
  2776. dput(new_dentry);
  2777. return error;
  2778. }
  2779. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2780. struct inode *new_dir, struct dentry *new_dentry)
  2781. {
  2782. int error;
  2783. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2784. const unsigned char *old_name;
  2785. if (old_dentry->d_inode == new_dentry->d_inode)
  2786. return 0;
  2787. error = may_delete(old_dir, old_dentry, is_dir);
  2788. if (error)
  2789. return error;
  2790. if (!new_dentry->d_inode)
  2791. error = may_create(new_dir, new_dentry);
  2792. else
  2793. error = may_delete(new_dir, new_dentry, is_dir);
  2794. if (error)
  2795. return error;
  2796. if (!old_dir->i_op->rename)
  2797. return -EPERM;
  2798. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2799. if (is_dir)
  2800. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2801. else
  2802. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2803. if (!error)
  2804. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2805. new_dentry->d_inode, old_dentry);
  2806. fsnotify_oldname_free(old_name);
  2807. return error;
  2808. }
  2809. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2810. int, newdfd, const char __user *, newname)
  2811. {
  2812. struct dentry *old_dir, *new_dir;
  2813. struct dentry *old_dentry, *new_dentry;
  2814. struct dentry *trap;
  2815. struct nameidata oldnd, newnd;
  2816. char *from;
  2817. char *to;
  2818. int error;
  2819. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2820. if (error)
  2821. goto exit;
  2822. error = user_path_parent(newdfd, newname, &newnd, &to);
  2823. if (error)
  2824. goto exit1;
  2825. error = -EXDEV;
  2826. if (oldnd.path.mnt != newnd.path.mnt)
  2827. goto exit2;
  2828. old_dir = oldnd.path.dentry;
  2829. error = -EBUSY;
  2830. if (oldnd.last_type != LAST_NORM)
  2831. goto exit2;
  2832. new_dir = newnd.path.dentry;
  2833. if (newnd.last_type != LAST_NORM)
  2834. goto exit2;
  2835. oldnd.flags &= ~LOOKUP_PARENT;
  2836. newnd.flags &= ~LOOKUP_PARENT;
  2837. newnd.flags |= LOOKUP_RENAME_TARGET;
  2838. trap = lock_rename(new_dir, old_dir);
  2839. old_dentry = lookup_hash(&oldnd);
  2840. error = PTR_ERR(old_dentry);
  2841. if (IS_ERR(old_dentry))
  2842. goto exit3;
  2843. /* source must exist */
  2844. error = -ENOENT;
  2845. if (!old_dentry->d_inode)
  2846. goto exit4;
  2847. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2848. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2849. error = -ENOTDIR;
  2850. if (oldnd.last.name[oldnd.last.len])
  2851. goto exit4;
  2852. if (newnd.last.name[newnd.last.len])
  2853. goto exit4;
  2854. }
  2855. /* source should not be ancestor of target */
  2856. error = -EINVAL;
  2857. if (old_dentry == trap)
  2858. goto exit4;
  2859. new_dentry = lookup_hash(&newnd);
  2860. error = PTR_ERR(new_dentry);
  2861. if (IS_ERR(new_dentry))
  2862. goto exit4;
  2863. /* target should not be an ancestor of source */
  2864. error = -ENOTEMPTY;
  2865. if (new_dentry == trap)
  2866. goto exit5;
  2867. error = mnt_want_write(oldnd.path.mnt);
  2868. if (error)
  2869. goto exit5;
  2870. error = security_path_rename(&oldnd.path, old_dentry,
  2871. &newnd.path, new_dentry);
  2872. if (error)
  2873. goto exit6;
  2874. error = vfs_rename(old_dir->d_inode, old_dentry,
  2875. new_dir->d_inode, new_dentry);
  2876. exit6:
  2877. mnt_drop_write(oldnd.path.mnt);
  2878. exit5:
  2879. dput(new_dentry);
  2880. exit4:
  2881. dput(old_dentry);
  2882. exit3:
  2883. unlock_rename(new_dir, old_dir);
  2884. exit2:
  2885. path_put(&newnd.path);
  2886. putname(to);
  2887. exit1:
  2888. path_put(&oldnd.path);
  2889. putname(from);
  2890. exit:
  2891. return error;
  2892. }
  2893. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2894. {
  2895. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2896. }
  2897. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2898. {
  2899. int len;
  2900. len = PTR_ERR(link);
  2901. if (IS_ERR(link))
  2902. goto out;
  2903. len = strlen(link);
  2904. if (len > (unsigned) buflen)
  2905. len = buflen;
  2906. if (copy_to_user(buffer, link, len))
  2907. len = -EFAULT;
  2908. out:
  2909. return len;
  2910. }
  2911. /*
  2912. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2913. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2914. * using) it for any given inode is up to filesystem.
  2915. */
  2916. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2917. {
  2918. struct nameidata nd;
  2919. void *cookie;
  2920. int res;
  2921. nd.depth = 0;
  2922. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2923. if (IS_ERR(cookie))
  2924. return PTR_ERR(cookie);
  2925. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2926. if (dentry->d_inode->i_op->put_link)
  2927. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2928. return res;
  2929. }
  2930. int vfs_follow_link(struct nameidata *nd, const char *link)
  2931. {
  2932. return __vfs_follow_link(nd, link);
  2933. }
  2934. /* get the link contents into pagecache */
  2935. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2936. {
  2937. char *kaddr;
  2938. struct page *page;
  2939. struct address_space *mapping = dentry->d_inode->i_mapping;
  2940. page = read_mapping_page(mapping, 0, NULL);
  2941. if (IS_ERR(page))
  2942. return (char*)page;
  2943. *ppage = page;
  2944. kaddr = kmap(page);
  2945. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2946. return kaddr;
  2947. }
  2948. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2949. {
  2950. struct page *page = NULL;
  2951. char *s = page_getlink(dentry, &page);
  2952. int res = vfs_readlink(dentry,buffer,buflen,s);
  2953. if (page) {
  2954. kunmap(page);
  2955. page_cache_release(page);
  2956. }
  2957. return res;
  2958. }
  2959. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2960. {
  2961. struct page *page = NULL;
  2962. nd_set_link(nd, page_getlink(dentry, &page));
  2963. return page;
  2964. }
  2965. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2966. {
  2967. struct page *page = cookie;
  2968. if (page) {
  2969. kunmap(page);
  2970. page_cache_release(page);
  2971. }
  2972. }
  2973. /*
  2974. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2975. */
  2976. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2977. {
  2978. struct address_space *mapping = inode->i_mapping;
  2979. struct page *page;
  2980. void *fsdata;
  2981. int err;
  2982. char *kaddr;
  2983. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2984. if (nofs)
  2985. flags |= AOP_FLAG_NOFS;
  2986. retry:
  2987. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2988. flags, &page, &fsdata);
  2989. if (err)
  2990. goto fail;
  2991. kaddr = kmap_atomic(page, KM_USER0);
  2992. memcpy(kaddr, symname, len-1);
  2993. kunmap_atomic(kaddr, KM_USER0);
  2994. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2995. page, fsdata);
  2996. if (err < 0)
  2997. goto fail;
  2998. if (err < len-1)
  2999. goto retry;
  3000. mark_inode_dirty(inode);
  3001. return 0;
  3002. fail:
  3003. return err;
  3004. }
  3005. int page_symlink(struct inode *inode, const char *symname, int len)
  3006. {
  3007. return __page_symlink(inode, symname, len,
  3008. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3009. }
  3010. const struct inode_operations page_symlink_inode_operations = {
  3011. .readlink = generic_readlink,
  3012. .follow_link = page_follow_link_light,
  3013. .put_link = page_put_link,
  3014. };
  3015. EXPORT_SYMBOL(user_path_at);
  3016. EXPORT_SYMBOL(follow_down_one);
  3017. EXPORT_SYMBOL(follow_down);
  3018. EXPORT_SYMBOL(follow_up);
  3019. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3020. EXPORT_SYMBOL(getname);
  3021. EXPORT_SYMBOL(lock_rename);
  3022. EXPORT_SYMBOL(lookup_one_len);
  3023. EXPORT_SYMBOL(page_follow_link_light);
  3024. EXPORT_SYMBOL(page_put_link);
  3025. EXPORT_SYMBOL(page_readlink);
  3026. EXPORT_SYMBOL(__page_symlink);
  3027. EXPORT_SYMBOL(page_symlink);
  3028. EXPORT_SYMBOL(page_symlink_inode_operations);
  3029. EXPORT_SYMBOL(kern_path_parent);
  3030. EXPORT_SYMBOL(kern_path);
  3031. EXPORT_SYMBOL(vfs_path_lookup);
  3032. EXPORT_SYMBOL(inode_permission);
  3033. EXPORT_SYMBOL(file_permission);
  3034. EXPORT_SYMBOL(unlock_rename);
  3035. EXPORT_SYMBOL(vfs_create);
  3036. EXPORT_SYMBOL(vfs_follow_link);
  3037. EXPORT_SYMBOL(vfs_link);
  3038. EXPORT_SYMBOL(vfs_mkdir);
  3039. EXPORT_SYMBOL(vfs_mknod);
  3040. EXPORT_SYMBOL(generic_permission);
  3041. EXPORT_SYMBOL(vfs_readlink);
  3042. EXPORT_SYMBOL(vfs_rename);
  3043. EXPORT_SYMBOL(vfs_rmdir);
  3044. EXPORT_SYMBOL(vfs_symlink);
  3045. EXPORT_SYMBOL(vfs_unlink);
  3046. EXPORT_SYMBOL(dentry_unhash);
  3047. EXPORT_SYMBOL(generic_readlink);