namei.c 92 KB

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