namei.c 88 KB

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