namei.c 87 KB

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