namei.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <linux/fs_struct.h>
  32. #include <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, 0);
  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(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1016. status = d_revalidate(dentry, nd);
  1017. if (unlikely(status <= 0)) {
  1018. if (status != -ECHILD)
  1019. need_reval = 0;
  1020. goto unlazy;
  1021. }
  1022. }
  1023. if (unlikely(d_need_lookup(dentry)))
  1024. goto unlazy;
  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 (likely(!dentry)) {
  1049. dentry = d_alloc_and_lookup(parent, name, nd);
  1050. if (IS_ERR(dentry)) {
  1051. mutex_unlock(&dir->i_mutex);
  1052. return PTR_ERR(dentry);
  1053. }
  1054. /* known good */
  1055. need_reval = 0;
  1056. status = 1;
  1057. } else if (unlikely(d_need_lookup(dentry))) {
  1058. dentry = d_inode_lookup(parent, dentry, nd);
  1059. if (IS_ERR(dentry)) {
  1060. mutex_unlock(&dir->i_mutex);
  1061. return PTR_ERR(dentry);
  1062. }
  1063. /* known good */
  1064. need_reval = 0;
  1065. status = 1;
  1066. }
  1067. mutex_unlock(&dir->i_mutex);
  1068. }
  1069. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1070. status = d_revalidate(dentry, nd);
  1071. if (unlikely(status <= 0)) {
  1072. if (status < 0) {
  1073. dput(dentry);
  1074. return status;
  1075. }
  1076. if (!d_invalidate(dentry)) {
  1077. dput(dentry);
  1078. dentry = NULL;
  1079. need_reval = 1;
  1080. goto retry;
  1081. }
  1082. }
  1083. path->mnt = mnt;
  1084. path->dentry = dentry;
  1085. err = follow_managed(path, nd->flags);
  1086. if (unlikely(err < 0)) {
  1087. path_put_conditional(path, nd);
  1088. return err;
  1089. }
  1090. if (err)
  1091. nd->flags |= LOOKUP_JUMPED;
  1092. *inode = path->dentry->d_inode;
  1093. return 0;
  1094. }
  1095. static inline int may_lookup(struct nameidata *nd)
  1096. {
  1097. if (nd->flags & LOOKUP_RCU) {
  1098. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1099. if (err != -ECHILD)
  1100. return err;
  1101. if (unlazy_walk(nd, NULL))
  1102. return -ECHILD;
  1103. }
  1104. return inode_permission(nd->inode, MAY_EXEC);
  1105. }
  1106. static inline int handle_dots(struct nameidata *nd, int type)
  1107. {
  1108. if (type == LAST_DOTDOT) {
  1109. if (nd->flags & LOOKUP_RCU) {
  1110. if (follow_dotdot_rcu(nd))
  1111. return -ECHILD;
  1112. } else
  1113. follow_dotdot(nd);
  1114. }
  1115. return 0;
  1116. }
  1117. static void terminate_walk(struct nameidata *nd)
  1118. {
  1119. if (!(nd->flags & LOOKUP_RCU)) {
  1120. path_put(&nd->path);
  1121. } else {
  1122. nd->flags &= ~LOOKUP_RCU;
  1123. if (!(nd->flags & LOOKUP_ROOT))
  1124. nd->root.mnt = NULL;
  1125. rcu_read_unlock();
  1126. br_read_unlock(vfsmount_lock);
  1127. }
  1128. }
  1129. /*
  1130. * Do we need to follow links? We _really_ want to be able
  1131. * to do this check without having to look at inode->i_op,
  1132. * so we keep a cache of "no, this doesn't need follow_link"
  1133. * for the common case.
  1134. */
  1135. static inline int should_follow_link(struct inode *inode, int follow)
  1136. {
  1137. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1138. if (likely(inode->i_op->follow_link))
  1139. return follow;
  1140. /* This gets set once for the inode lifetime */
  1141. spin_lock(&inode->i_lock);
  1142. inode->i_opflags |= IOP_NOFOLLOW;
  1143. spin_unlock(&inode->i_lock);
  1144. }
  1145. return 0;
  1146. }
  1147. static inline int walk_component(struct nameidata *nd, struct path *path,
  1148. struct qstr *name, int type, int follow)
  1149. {
  1150. struct inode *inode;
  1151. int err;
  1152. /*
  1153. * "." and ".." are special - ".." especially so because it has
  1154. * to be able to know about the current root directory and
  1155. * parent relationships.
  1156. */
  1157. if (unlikely(type != LAST_NORM))
  1158. return handle_dots(nd, type);
  1159. err = do_lookup(nd, name, path, &inode);
  1160. if (unlikely(err)) {
  1161. terminate_walk(nd);
  1162. return err;
  1163. }
  1164. if (!inode) {
  1165. path_to_nameidata(path, nd);
  1166. terminate_walk(nd);
  1167. return -ENOENT;
  1168. }
  1169. if (should_follow_link(inode, follow)) {
  1170. if (nd->flags & LOOKUP_RCU) {
  1171. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1172. terminate_walk(nd);
  1173. return -ECHILD;
  1174. }
  1175. }
  1176. BUG_ON(inode != path->dentry->d_inode);
  1177. return 1;
  1178. }
  1179. path_to_nameidata(path, nd);
  1180. nd->inode = inode;
  1181. return 0;
  1182. }
  1183. /*
  1184. * This limits recursive symlink follows to 8, while
  1185. * limiting consecutive symlinks to 40.
  1186. *
  1187. * Without that kind of total limit, nasty chains of consecutive
  1188. * symlinks can cause almost arbitrarily long lookups.
  1189. */
  1190. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1191. {
  1192. int res;
  1193. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1194. path_put_conditional(path, nd);
  1195. path_put(&nd->path);
  1196. return -ELOOP;
  1197. }
  1198. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1199. nd->depth++;
  1200. current->link_count++;
  1201. do {
  1202. struct path link = *path;
  1203. void *cookie;
  1204. res = follow_link(&link, nd, &cookie);
  1205. if (!res)
  1206. res = walk_component(nd, path, &nd->last,
  1207. nd->last_type, LOOKUP_FOLLOW);
  1208. put_link(nd, &link, cookie);
  1209. } while (res > 0);
  1210. current->link_count--;
  1211. nd->depth--;
  1212. return res;
  1213. }
  1214. /*
  1215. * We really don't want to look at inode->i_op->lookup
  1216. * when we don't have to. So we keep a cache bit in
  1217. * the inode ->i_opflags field that says "yes, we can
  1218. * do lookup on this inode".
  1219. */
  1220. static inline int can_lookup(struct inode *inode)
  1221. {
  1222. if (likely(inode->i_opflags & IOP_LOOKUP))
  1223. return 1;
  1224. if (likely(!inode->i_op->lookup))
  1225. return 0;
  1226. /* We do this once for the lifetime of the inode */
  1227. spin_lock(&inode->i_lock);
  1228. inode->i_opflags |= IOP_LOOKUP;
  1229. spin_unlock(&inode->i_lock);
  1230. return 1;
  1231. }
  1232. /*
  1233. * We can do the critical dentry name comparison and hashing
  1234. * operations one word at a time, but we are limited to:
  1235. *
  1236. * - Architectures with fast unaligned word accesses. We could
  1237. * do a "get_unaligned()" if this helps and is sufficiently
  1238. * fast.
  1239. *
  1240. * - Little-endian machines (so that we can generate the mask
  1241. * of low bytes efficiently). Again, we *could* do a byte
  1242. * swapping load on big-endian architectures if that is not
  1243. * expensive enough to make the optimization worthless.
  1244. *
  1245. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1246. * do not trap on the (extremely unlikely) case of a page
  1247. * crossing operation.
  1248. *
  1249. * - Furthermore, we need an efficient 64-bit compile for the
  1250. * 64-bit case in order to generate the "number of bytes in
  1251. * the final mask". Again, that could be replaced with a
  1252. * efficient population count instruction or similar.
  1253. */
  1254. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1255. #ifdef CONFIG_64BIT
  1256. /*
  1257. * Jan Achrenius on G+: microoptimized version of
  1258. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  1259. * that works for the bytemasks without having to
  1260. * mask them first.
  1261. */
  1262. static inline long count_masked_bytes(unsigned long mask)
  1263. {
  1264. return mask*0x0001020304050608 >> 56;
  1265. }
  1266. static inline unsigned int fold_hash(unsigned long hash)
  1267. {
  1268. hash += hash >> (8*sizeof(int));
  1269. return hash;
  1270. }
  1271. #else /* 32-bit case */
  1272. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  1273. static inline long count_masked_bytes(long mask)
  1274. {
  1275. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  1276. long a = (0x0ff0001+mask) >> 23;
  1277. /* Fix the 1 for 00 case */
  1278. return a & mask;
  1279. }
  1280. #define fold_hash(x) (x)
  1281. #endif
  1282. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1283. {
  1284. unsigned long a, mask;
  1285. unsigned long hash = 0;
  1286. for (;;) {
  1287. a = *(unsigned long *)name;
  1288. hash *= 9;
  1289. if (len < sizeof(unsigned long))
  1290. break;
  1291. hash += a;
  1292. name += sizeof(unsigned long);
  1293. len -= sizeof(unsigned long);
  1294. if (!len)
  1295. goto done;
  1296. }
  1297. mask = ~(~0ul << len*8);
  1298. hash += mask & a;
  1299. done:
  1300. return fold_hash(hash);
  1301. }
  1302. EXPORT_SYMBOL(full_name_hash);
  1303. #define ONEBYTES 0x0101010101010101ul
  1304. #define SLASHBYTES 0x2f2f2f2f2f2f2f2ful
  1305. #define HIGHBITS 0x8080808080808080ul
  1306. /* Return the high bit set in the first byte that is a zero */
  1307. static inline unsigned long has_zero(unsigned long a)
  1308. {
  1309. return ((a - ONEBYTES) & ~a) & HIGHBITS;
  1310. }
  1311. /*
  1312. * Calculate the length and hash of the path component, and
  1313. * return the length of the component;
  1314. */
  1315. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1316. {
  1317. unsigned long a, mask, hash, len;
  1318. hash = a = 0;
  1319. len = -sizeof(unsigned long);
  1320. do {
  1321. hash = (hash + a) * 9;
  1322. len += sizeof(unsigned long);
  1323. a = *(unsigned long *)(name+len);
  1324. /* Do we have any NUL or '/' bytes in this word? */
  1325. mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
  1326. } while (!mask);
  1327. /* The mask *below* the first high bit set */
  1328. mask = (mask - 1) & ~mask;
  1329. mask >>= 7;
  1330. hash += a & mask;
  1331. *hashp = fold_hash(hash);
  1332. return len + count_masked_bytes(mask);
  1333. }
  1334. #else
  1335. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1336. {
  1337. unsigned long hash = init_name_hash();
  1338. while (len--)
  1339. hash = partial_name_hash(*name++, hash);
  1340. return end_name_hash(hash);
  1341. }
  1342. EXPORT_SYMBOL(full_name_hash);
  1343. /*
  1344. * We know there's a real path component here of at least
  1345. * one character.
  1346. */
  1347. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1348. {
  1349. unsigned long hash = init_name_hash();
  1350. unsigned long len = 0, c;
  1351. c = (unsigned char)*name;
  1352. do {
  1353. len++;
  1354. hash = partial_name_hash(c, hash);
  1355. c = (unsigned char)name[len];
  1356. } while (c && c != '/');
  1357. *hashp = end_name_hash(hash);
  1358. return len;
  1359. }
  1360. #endif
  1361. /*
  1362. * Name resolution.
  1363. * This is the basic name resolution function, turning a pathname into
  1364. * the final dentry. We expect 'base' to be positive and a directory.
  1365. *
  1366. * Returns 0 and nd will have valid dentry and mnt on success.
  1367. * Returns error and drops reference to input namei data on failure.
  1368. */
  1369. static int link_path_walk(const char *name, struct nameidata *nd)
  1370. {
  1371. struct path next;
  1372. int err;
  1373. while (*name=='/')
  1374. name++;
  1375. if (!*name)
  1376. return 0;
  1377. /* At this point we know we have a real path component. */
  1378. for(;;) {
  1379. struct qstr this;
  1380. long len;
  1381. int type;
  1382. err = may_lookup(nd);
  1383. if (err)
  1384. break;
  1385. len = hash_name(name, &this.hash);
  1386. this.name = name;
  1387. this.len = len;
  1388. type = LAST_NORM;
  1389. if (name[0] == '.') switch (len) {
  1390. case 2:
  1391. if (name[1] == '.') {
  1392. type = LAST_DOTDOT;
  1393. nd->flags |= LOOKUP_JUMPED;
  1394. }
  1395. break;
  1396. case 1:
  1397. type = LAST_DOT;
  1398. }
  1399. if (likely(type == LAST_NORM)) {
  1400. struct dentry *parent = nd->path.dentry;
  1401. nd->flags &= ~LOOKUP_JUMPED;
  1402. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1403. err = parent->d_op->d_hash(parent, nd->inode,
  1404. &this);
  1405. if (err < 0)
  1406. break;
  1407. }
  1408. }
  1409. if (!name[len])
  1410. goto last_component;
  1411. /*
  1412. * If it wasn't NUL, we know it was '/'. Skip that
  1413. * slash, and continue until no more slashes.
  1414. */
  1415. do {
  1416. len++;
  1417. } while (unlikely(name[len] == '/'));
  1418. if (!name[len])
  1419. goto last_component;
  1420. name += len;
  1421. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1422. if (err < 0)
  1423. return err;
  1424. if (err) {
  1425. err = nested_symlink(&next, nd);
  1426. if (err)
  1427. return err;
  1428. }
  1429. if (can_lookup(nd->inode))
  1430. continue;
  1431. err = -ENOTDIR;
  1432. break;
  1433. /* here ends the main loop */
  1434. last_component:
  1435. nd->last = this;
  1436. nd->last_type = type;
  1437. return 0;
  1438. }
  1439. terminate_walk(nd);
  1440. return err;
  1441. }
  1442. static int path_init(int dfd, const char *name, unsigned int flags,
  1443. struct nameidata *nd, struct file **fp)
  1444. {
  1445. int retval = 0;
  1446. int fput_needed;
  1447. struct file *file;
  1448. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1449. nd->flags = flags | LOOKUP_JUMPED;
  1450. nd->depth = 0;
  1451. if (flags & LOOKUP_ROOT) {
  1452. struct inode *inode = nd->root.dentry->d_inode;
  1453. if (*name) {
  1454. if (!inode->i_op->lookup)
  1455. return -ENOTDIR;
  1456. retval = inode_permission(inode, MAY_EXEC);
  1457. if (retval)
  1458. return retval;
  1459. }
  1460. nd->path = nd->root;
  1461. nd->inode = inode;
  1462. if (flags & LOOKUP_RCU) {
  1463. br_read_lock(vfsmount_lock);
  1464. rcu_read_lock();
  1465. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1466. } else {
  1467. path_get(&nd->path);
  1468. }
  1469. return 0;
  1470. }
  1471. nd->root.mnt = NULL;
  1472. if (*name=='/') {
  1473. if (flags & LOOKUP_RCU) {
  1474. br_read_lock(vfsmount_lock);
  1475. rcu_read_lock();
  1476. set_root_rcu(nd);
  1477. } else {
  1478. set_root(nd);
  1479. path_get(&nd->root);
  1480. }
  1481. nd->path = nd->root;
  1482. } else if (dfd == AT_FDCWD) {
  1483. if (flags & LOOKUP_RCU) {
  1484. struct fs_struct *fs = current->fs;
  1485. unsigned seq;
  1486. br_read_lock(vfsmount_lock);
  1487. rcu_read_lock();
  1488. do {
  1489. seq = read_seqcount_begin(&fs->seq);
  1490. nd->path = fs->pwd;
  1491. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1492. } while (read_seqcount_retry(&fs->seq, seq));
  1493. } else {
  1494. get_fs_pwd(current->fs, &nd->path);
  1495. }
  1496. } else {
  1497. struct dentry *dentry;
  1498. file = fget_raw_light(dfd, &fput_needed);
  1499. retval = -EBADF;
  1500. if (!file)
  1501. goto out_fail;
  1502. dentry = file->f_path.dentry;
  1503. if (*name) {
  1504. retval = -ENOTDIR;
  1505. if (!S_ISDIR(dentry->d_inode->i_mode))
  1506. goto fput_fail;
  1507. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1508. if (retval)
  1509. goto fput_fail;
  1510. }
  1511. nd->path = file->f_path;
  1512. if (flags & LOOKUP_RCU) {
  1513. if (fput_needed)
  1514. *fp = file;
  1515. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1516. br_read_lock(vfsmount_lock);
  1517. rcu_read_lock();
  1518. } else {
  1519. path_get(&file->f_path);
  1520. fput_light(file, fput_needed);
  1521. }
  1522. }
  1523. nd->inode = nd->path.dentry->d_inode;
  1524. return 0;
  1525. fput_fail:
  1526. fput_light(file, fput_needed);
  1527. out_fail:
  1528. return retval;
  1529. }
  1530. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1531. {
  1532. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1533. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1534. nd->flags &= ~LOOKUP_PARENT;
  1535. return walk_component(nd, path, &nd->last, nd->last_type,
  1536. nd->flags & LOOKUP_FOLLOW);
  1537. }
  1538. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1539. static int path_lookupat(int dfd, const char *name,
  1540. unsigned int flags, struct nameidata *nd)
  1541. {
  1542. struct file *base = NULL;
  1543. struct path path;
  1544. int err;
  1545. /*
  1546. * Path walking is largely split up into 2 different synchronisation
  1547. * schemes, rcu-walk and ref-walk (explained in
  1548. * Documentation/filesystems/path-lookup.txt). These share much of the
  1549. * path walk code, but some things particularly setup, cleanup, and
  1550. * following mounts are sufficiently divergent that functions are
  1551. * duplicated. Typically there is a function foo(), and its RCU
  1552. * analogue, foo_rcu().
  1553. *
  1554. * -ECHILD is the error number of choice (just to avoid clashes) that
  1555. * is returned if some aspect of an rcu-walk fails. Such an error must
  1556. * be handled by restarting a traditional ref-walk (which will always
  1557. * be able to complete).
  1558. */
  1559. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1560. if (unlikely(err))
  1561. return err;
  1562. current->total_link_count = 0;
  1563. err = link_path_walk(name, nd);
  1564. if (!err && !(flags & LOOKUP_PARENT)) {
  1565. err = lookup_last(nd, &path);
  1566. while (err > 0) {
  1567. void *cookie;
  1568. struct path link = path;
  1569. nd->flags |= LOOKUP_PARENT;
  1570. err = follow_link(&link, nd, &cookie);
  1571. if (!err)
  1572. err = lookup_last(nd, &path);
  1573. put_link(nd, &link, cookie);
  1574. }
  1575. }
  1576. if (!err)
  1577. err = complete_walk(nd);
  1578. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1579. if (!nd->inode->i_op->lookup) {
  1580. path_put(&nd->path);
  1581. err = -ENOTDIR;
  1582. }
  1583. }
  1584. if (base)
  1585. fput(base);
  1586. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1587. path_put(&nd->root);
  1588. nd->root.mnt = NULL;
  1589. }
  1590. return err;
  1591. }
  1592. static int do_path_lookup(int dfd, const char *name,
  1593. unsigned int flags, struct nameidata *nd)
  1594. {
  1595. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1596. if (unlikely(retval == -ECHILD))
  1597. retval = path_lookupat(dfd, name, flags, nd);
  1598. if (unlikely(retval == -ESTALE))
  1599. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1600. if (likely(!retval)) {
  1601. if (unlikely(!audit_dummy_context())) {
  1602. if (nd->path.dentry && nd->inode)
  1603. audit_inode(name, nd->path.dentry);
  1604. }
  1605. }
  1606. return retval;
  1607. }
  1608. int kern_path_parent(const char *name, struct nameidata *nd)
  1609. {
  1610. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1611. }
  1612. int kern_path(const char *name, unsigned int flags, struct path *path)
  1613. {
  1614. struct nameidata nd;
  1615. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1616. if (!res)
  1617. *path = nd.path;
  1618. return res;
  1619. }
  1620. /**
  1621. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1622. * @dentry: pointer to dentry of the base directory
  1623. * @mnt: pointer to vfs mount of the base directory
  1624. * @name: pointer to file name
  1625. * @flags: lookup flags
  1626. * @path: pointer to struct path to fill
  1627. */
  1628. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1629. const char *name, unsigned int flags,
  1630. struct path *path)
  1631. {
  1632. struct nameidata nd;
  1633. int err;
  1634. nd.root.dentry = dentry;
  1635. nd.root.mnt = mnt;
  1636. BUG_ON(flags & LOOKUP_PARENT);
  1637. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1638. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1639. if (!err)
  1640. *path = nd.path;
  1641. return err;
  1642. }
  1643. static struct dentry *__lookup_hash(struct qstr *name,
  1644. struct dentry *base, struct nameidata *nd)
  1645. {
  1646. struct inode *inode = base->d_inode;
  1647. struct dentry *dentry;
  1648. int err;
  1649. err = inode_permission(inode, MAY_EXEC);
  1650. if (err)
  1651. return ERR_PTR(err);
  1652. /*
  1653. * Don't bother with __d_lookup: callers are for creat as
  1654. * well as unlink, so a lot of the time it would cost
  1655. * a double lookup.
  1656. */
  1657. dentry = d_lookup(base, name);
  1658. if (dentry && d_need_lookup(dentry)) {
  1659. /*
  1660. * __lookup_hash is called with the parent dir's i_mutex already
  1661. * held, so we are good to go here.
  1662. */
  1663. dentry = d_inode_lookup(base, dentry, nd);
  1664. if (IS_ERR(dentry))
  1665. return dentry;
  1666. }
  1667. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1668. int status = d_revalidate(dentry, nd);
  1669. if (unlikely(status <= 0)) {
  1670. /*
  1671. * The dentry failed validation.
  1672. * If d_revalidate returned 0 attempt to invalidate
  1673. * the dentry otherwise d_revalidate is asking us
  1674. * to return a fail status.
  1675. */
  1676. if (status < 0) {
  1677. dput(dentry);
  1678. return ERR_PTR(status);
  1679. } else if (!d_invalidate(dentry)) {
  1680. dput(dentry);
  1681. dentry = NULL;
  1682. }
  1683. }
  1684. }
  1685. if (!dentry)
  1686. dentry = d_alloc_and_lookup(base, name, nd);
  1687. return dentry;
  1688. }
  1689. /*
  1690. * Restricted form of lookup. Doesn't follow links, single-component only,
  1691. * needs parent already locked. Doesn't follow mounts.
  1692. * SMP-safe.
  1693. */
  1694. static struct dentry *lookup_hash(struct nameidata *nd)
  1695. {
  1696. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1697. }
  1698. /**
  1699. * lookup_one_len - filesystem helper to lookup single pathname component
  1700. * @name: pathname component to lookup
  1701. * @base: base directory to lookup from
  1702. * @len: maximum length @len should be interpreted to
  1703. *
  1704. * Note that this routine is purely a helper for filesystem usage and should
  1705. * not be called by generic code. Also note that by using this function the
  1706. * nameidata argument is passed to the filesystem methods and a filesystem
  1707. * using this helper needs to be prepared for that.
  1708. */
  1709. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1710. {
  1711. struct qstr this;
  1712. unsigned int c;
  1713. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1714. this.name = name;
  1715. this.len = len;
  1716. this.hash = full_name_hash(name, len);
  1717. if (!len)
  1718. return ERR_PTR(-EACCES);
  1719. while (len--) {
  1720. c = *(const unsigned char *)name++;
  1721. if (c == '/' || c == '\0')
  1722. return ERR_PTR(-EACCES);
  1723. }
  1724. /*
  1725. * See if the low-level filesystem might want
  1726. * to use its own hash..
  1727. */
  1728. if (base->d_flags & DCACHE_OP_HASH) {
  1729. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1730. if (err < 0)
  1731. return ERR_PTR(err);
  1732. }
  1733. return __lookup_hash(&this, base, NULL);
  1734. }
  1735. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1736. struct path *path, int *empty)
  1737. {
  1738. struct nameidata nd;
  1739. char *tmp = getname_flags(name, flags, empty);
  1740. int err = PTR_ERR(tmp);
  1741. if (!IS_ERR(tmp)) {
  1742. BUG_ON(flags & LOOKUP_PARENT);
  1743. err = do_path_lookup(dfd, tmp, flags, &nd);
  1744. putname(tmp);
  1745. if (!err)
  1746. *path = nd.path;
  1747. }
  1748. return err;
  1749. }
  1750. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1751. struct path *path)
  1752. {
  1753. return user_path_at_empty(dfd, name, flags, path, 0);
  1754. }
  1755. static int user_path_parent(int dfd, const char __user *path,
  1756. struct nameidata *nd, char **name)
  1757. {
  1758. char *s = getname(path);
  1759. int error;
  1760. if (IS_ERR(s))
  1761. return PTR_ERR(s);
  1762. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1763. if (error)
  1764. putname(s);
  1765. else
  1766. *name = s;
  1767. return error;
  1768. }
  1769. /*
  1770. * It's inline, so penalty for filesystems that don't use sticky bit is
  1771. * minimal.
  1772. */
  1773. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1774. {
  1775. uid_t fsuid = current_fsuid();
  1776. if (!(dir->i_mode & S_ISVTX))
  1777. return 0;
  1778. if (current_user_ns() != inode_userns(inode))
  1779. goto other_userns;
  1780. if (inode->i_uid == fsuid)
  1781. return 0;
  1782. if (dir->i_uid == fsuid)
  1783. return 0;
  1784. other_userns:
  1785. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1786. }
  1787. /*
  1788. * Check whether we can remove a link victim from directory dir, check
  1789. * whether the type of victim is right.
  1790. * 1. We can't do it if dir is read-only (done in permission())
  1791. * 2. We should have write and exec permissions on dir
  1792. * 3. We can't remove anything from append-only dir
  1793. * 4. We can't do anything with immutable dir (done in permission())
  1794. * 5. If the sticky bit on dir is set we should either
  1795. * a. be owner of dir, or
  1796. * b. be owner of victim, or
  1797. * c. have CAP_FOWNER capability
  1798. * 6. If the victim is append-only or immutable we can't do antyhing with
  1799. * links pointing to it.
  1800. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1801. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1802. * 9. We can't remove a root or mountpoint.
  1803. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1804. * nfs_async_unlink().
  1805. */
  1806. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1807. {
  1808. int error;
  1809. if (!victim->d_inode)
  1810. return -ENOENT;
  1811. BUG_ON(victim->d_parent->d_inode != dir);
  1812. audit_inode_child(victim, dir);
  1813. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1814. if (error)
  1815. return error;
  1816. if (IS_APPEND(dir))
  1817. return -EPERM;
  1818. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1819. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1820. return -EPERM;
  1821. if (isdir) {
  1822. if (!S_ISDIR(victim->d_inode->i_mode))
  1823. return -ENOTDIR;
  1824. if (IS_ROOT(victim))
  1825. return -EBUSY;
  1826. } else if (S_ISDIR(victim->d_inode->i_mode))
  1827. return -EISDIR;
  1828. if (IS_DEADDIR(dir))
  1829. return -ENOENT;
  1830. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1831. return -EBUSY;
  1832. return 0;
  1833. }
  1834. /* Check whether we can create an object with dentry child in directory
  1835. * dir.
  1836. * 1. We can't do it if child already exists (open has special treatment for
  1837. * this case, but since we are inlined it's OK)
  1838. * 2. We can't do it if dir is read-only (done in permission())
  1839. * 3. We should have write and exec permissions on dir
  1840. * 4. We can't do it if dir is immutable (done in permission())
  1841. */
  1842. static inline int may_create(struct inode *dir, struct dentry *child)
  1843. {
  1844. if (child->d_inode)
  1845. return -EEXIST;
  1846. if (IS_DEADDIR(dir))
  1847. return -ENOENT;
  1848. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1849. }
  1850. /*
  1851. * p1 and p2 should be directories on the same fs.
  1852. */
  1853. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1854. {
  1855. struct dentry *p;
  1856. if (p1 == p2) {
  1857. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1858. return NULL;
  1859. }
  1860. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1861. p = d_ancestor(p2, p1);
  1862. if (p) {
  1863. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1864. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1865. return p;
  1866. }
  1867. p = d_ancestor(p1, p2);
  1868. if (p) {
  1869. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1870. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1871. return p;
  1872. }
  1873. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1874. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1875. return NULL;
  1876. }
  1877. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1878. {
  1879. mutex_unlock(&p1->d_inode->i_mutex);
  1880. if (p1 != p2) {
  1881. mutex_unlock(&p2->d_inode->i_mutex);
  1882. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1883. }
  1884. }
  1885. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1886. struct nameidata *nd)
  1887. {
  1888. int error = may_create(dir, dentry);
  1889. if (error)
  1890. return error;
  1891. if (!dir->i_op->create)
  1892. return -EACCES; /* shouldn't it be ENOSYS? */
  1893. mode &= S_IALLUGO;
  1894. mode |= S_IFREG;
  1895. error = security_inode_create(dir, dentry, mode);
  1896. if (error)
  1897. return error;
  1898. error = dir->i_op->create(dir, dentry, mode, nd);
  1899. if (!error)
  1900. fsnotify_create(dir, dentry);
  1901. return error;
  1902. }
  1903. static int may_open(struct path *path, int acc_mode, int flag)
  1904. {
  1905. struct dentry *dentry = path->dentry;
  1906. struct inode *inode = dentry->d_inode;
  1907. int error;
  1908. /* O_PATH? */
  1909. if (!acc_mode)
  1910. return 0;
  1911. if (!inode)
  1912. return -ENOENT;
  1913. switch (inode->i_mode & S_IFMT) {
  1914. case S_IFLNK:
  1915. return -ELOOP;
  1916. case S_IFDIR:
  1917. if (acc_mode & MAY_WRITE)
  1918. return -EISDIR;
  1919. break;
  1920. case S_IFBLK:
  1921. case S_IFCHR:
  1922. if (path->mnt->mnt_flags & MNT_NODEV)
  1923. return -EACCES;
  1924. /*FALLTHRU*/
  1925. case S_IFIFO:
  1926. case S_IFSOCK:
  1927. flag &= ~O_TRUNC;
  1928. break;
  1929. }
  1930. error = inode_permission(inode, acc_mode);
  1931. if (error)
  1932. return error;
  1933. /*
  1934. * An append-only file must be opened in append mode for writing.
  1935. */
  1936. if (IS_APPEND(inode)) {
  1937. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1938. return -EPERM;
  1939. if (flag & O_TRUNC)
  1940. return -EPERM;
  1941. }
  1942. /* O_NOATIME can only be set by the owner or superuser */
  1943. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1944. return -EPERM;
  1945. return 0;
  1946. }
  1947. static int handle_truncate(struct file *filp)
  1948. {
  1949. struct path *path = &filp->f_path;
  1950. struct inode *inode = path->dentry->d_inode;
  1951. int error = get_write_access(inode);
  1952. if (error)
  1953. return error;
  1954. /*
  1955. * Refuse to truncate files with mandatory locks held on them.
  1956. */
  1957. error = locks_verify_locked(inode);
  1958. if (!error)
  1959. error = security_path_truncate(path);
  1960. if (!error) {
  1961. error = do_truncate(path->dentry, 0,
  1962. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1963. filp);
  1964. }
  1965. put_write_access(inode);
  1966. return error;
  1967. }
  1968. static inline int open_to_namei_flags(int flag)
  1969. {
  1970. if ((flag & O_ACCMODE) == 3)
  1971. flag--;
  1972. return flag;
  1973. }
  1974. /*
  1975. * Handle the last step of open()
  1976. */
  1977. static struct file *do_last(struct nameidata *nd, struct path *path,
  1978. const struct open_flags *op, const char *pathname)
  1979. {
  1980. struct dentry *dir = nd->path.dentry;
  1981. struct dentry *dentry;
  1982. int open_flag = op->open_flag;
  1983. int will_truncate = open_flag & O_TRUNC;
  1984. int want_write = 0;
  1985. int acc_mode = op->acc_mode;
  1986. struct file *filp;
  1987. int error;
  1988. nd->flags &= ~LOOKUP_PARENT;
  1989. nd->flags |= op->intent;
  1990. switch (nd->last_type) {
  1991. case LAST_DOTDOT:
  1992. case LAST_DOT:
  1993. error = handle_dots(nd, nd->last_type);
  1994. if (error)
  1995. return ERR_PTR(error);
  1996. /* fallthrough */
  1997. case LAST_ROOT:
  1998. error = complete_walk(nd);
  1999. if (error)
  2000. return ERR_PTR(error);
  2001. audit_inode(pathname, nd->path.dentry);
  2002. if (open_flag & O_CREAT) {
  2003. error = -EISDIR;
  2004. goto exit;
  2005. }
  2006. goto ok;
  2007. case LAST_BIND:
  2008. error = complete_walk(nd);
  2009. if (error)
  2010. return ERR_PTR(error);
  2011. audit_inode(pathname, dir);
  2012. goto ok;
  2013. }
  2014. if (!(open_flag & O_CREAT)) {
  2015. int symlink_ok = 0;
  2016. if (nd->last.name[nd->last.len])
  2017. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2018. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  2019. symlink_ok = 1;
  2020. /* we _can_ be in RCU mode here */
  2021. error = walk_component(nd, path, &nd->last, LAST_NORM,
  2022. !symlink_ok);
  2023. if (error < 0)
  2024. return ERR_PTR(error);
  2025. if (error) /* symlink */
  2026. return NULL;
  2027. /* sayonara */
  2028. error = complete_walk(nd);
  2029. if (error)
  2030. return ERR_PTR(error);
  2031. error = -ENOTDIR;
  2032. if (nd->flags & LOOKUP_DIRECTORY) {
  2033. if (!nd->inode->i_op->lookup)
  2034. goto exit;
  2035. }
  2036. audit_inode(pathname, nd->path.dentry);
  2037. goto ok;
  2038. }
  2039. /* create side of things */
  2040. /*
  2041. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
  2042. * cleared when we got to the last component we are about to look up
  2043. */
  2044. error = complete_walk(nd);
  2045. if (error)
  2046. return ERR_PTR(error);
  2047. audit_inode(pathname, dir);
  2048. error = -EISDIR;
  2049. /* trailing slashes? */
  2050. if (nd->last.name[nd->last.len])
  2051. goto exit;
  2052. mutex_lock(&dir->d_inode->i_mutex);
  2053. dentry = lookup_hash(nd);
  2054. error = PTR_ERR(dentry);
  2055. if (IS_ERR(dentry)) {
  2056. mutex_unlock(&dir->d_inode->i_mutex);
  2057. goto exit;
  2058. }
  2059. path->dentry = dentry;
  2060. path->mnt = nd->path.mnt;
  2061. /* Negative dentry, just create the file */
  2062. if (!dentry->d_inode) {
  2063. umode_t mode = op->mode;
  2064. if (!IS_POSIXACL(dir->d_inode))
  2065. mode &= ~current_umask();
  2066. /*
  2067. * This write is needed to ensure that a
  2068. * rw->ro transition does not occur between
  2069. * the time when the file is created and when
  2070. * a permanent write count is taken through
  2071. * the 'struct file' in nameidata_to_filp().
  2072. */
  2073. error = mnt_want_write(nd->path.mnt);
  2074. if (error)
  2075. goto exit_mutex_unlock;
  2076. want_write = 1;
  2077. /* Don't check for write permission, don't truncate */
  2078. open_flag &= ~O_TRUNC;
  2079. will_truncate = 0;
  2080. acc_mode = MAY_OPEN;
  2081. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2082. if (error)
  2083. goto exit_mutex_unlock;
  2084. error = vfs_create(dir->d_inode, dentry, mode, nd);
  2085. if (error)
  2086. goto exit_mutex_unlock;
  2087. mutex_unlock(&dir->d_inode->i_mutex);
  2088. dput(nd->path.dentry);
  2089. nd->path.dentry = dentry;
  2090. goto common;
  2091. }
  2092. /*
  2093. * It already exists.
  2094. */
  2095. mutex_unlock(&dir->d_inode->i_mutex);
  2096. audit_inode(pathname, path->dentry);
  2097. error = -EEXIST;
  2098. if (open_flag & O_EXCL)
  2099. goto exit_dput;
  2100. error = follow_managed(path, nd->flags);
  2101. if (error < 0)
  2102. goto exit_dput;
  2103. if (error)
  2104. nd->flags |= LOOKUP_JUMPED;
  2105. error = -ENOENT;
  2106. if (!path->dentry->d_inode)
  2107. goto exit_dput;
  2108. if (path->dentry->d_inode->i_op->follow_link)
  2109. return NULL;
  2110. path_to_nameidata(path, nd);
  2111. nd->inode = path->dentry->d_inode;
  2112. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2113. error = complete_walk(nd);
  2114. if (error)
  2115. return ERR_PTR(error);
  2116. error = -EISDIR;
  2117. if (S_ISDIR(nd->inode->i_mode))
  2118. goto exit;
  2119. ok:
  2120. if (!S_ISREG(nd->inode->i_mode))
  2121. will_truncate = 0;
  2122. if (will_truncate) {
  2123. error = mnt_want_write(nd->path.mnt);
  2124. if (error)
  2125. goto exit;
  2126. want_write = 1;
  2127. }
  2128. common:
  2129. error = may_open(&nd->path, acc_mode, open_flag);
  2130. if (error)
  2131. goto exit;
  2132. filp = nameidata_to_filp(nd);
  2133. if (!IS_ERR(filp)) {
  2134. error = ima_file_check(filp, op->acc_mode);
  2135. if (error) {
  2136. fput(filp);
  2137. filp = ERR_PTR(error);
  2138. }
  2139. }
  2140. if (!IS_ERR(filp)) {
  2141. if (will_truncate) {
  2142. error = handle_truncate(filp);
  2143. if (error) {
  2144. fput(filp);
  2145. filp = ERR_PTR(error);
  2146. }
  2147. }
  2148. }
  2149. out:
  2150. if (want_write)
  2151. mnt_drop_write(nd->path.mnt);
  2152. path_put(&nd->path);
  2153. return filp;
  2154. exit_mutex_unlock:
  2155. mutex_unlock(&dir->d_inode->i_mutex);
  2156. exit_dput:
  2157. path_put_conditional(path, nd);
  2158. exit:
  2159. filp = ERR_PTR(error);
  2160. goto out;
  2161. }
  2162. static struct file *path_openat(int dfd, const char *pathname,
  2163. struct nameidata *nd, const struct open_flags *op, int flags)
  2164. {
  2165. struct file *base = NULL;
  2166. struct file *filp;
  2167. struct path path;
  2168. int error;
  2169. filp = get_empty_filp();
  2170. if (!filp)
  2171. return ERR_PTR(-ENFILE);
  2172. filp->f_flags = op->open_flag;
  2173. nd->intent.open.file = filp;
  2174. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2175. nd->intent.open.create_mode = op->mode;
  2176. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2177. if (unlikely(error))
  2178. goto out_filp;
  2179. current->total_link_count = 0;
  2180. error = link_path_walk(pathname, nd);
  2181. if (unlikely(error))
  2182. goto out_filp;
  2183. filp = do_last(nd, &path, op, pathname);
  2184. while (unlikely(!filp)) { /* trailing symlink */
  2185. struct path link = path;
  2186. void *cookie;
  2187. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2188. path_put_conditional(&path, nd);
  2189. path_put(&nd->path);
  2190. filp = ERR_PTR(-ELOOP);
  2191. break;
  2192. }
  2193. nd->flags |= LOOKUP_PARENT;
  2194. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2195. error = follow_link(&link, nd, &cookie);
  2196. if (unlikely(error))
  2197. filp = ERR_PTR(error);
  2198. else
  2199. filp = do_last(nd, &path, op, pathname);
  2200. put_link(nd, &link, cookie);
  2201. }
  2202. out:
  2203. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2204. path_put(&nd->root);
  2205. if (base)
  2206. fput(base);
  2207. release_open_intent(nd);
  2208. return filp;
  2209. out_filp:
  2210. filp = ERR_PTR(error);
  2211. goto out;
  2212. }
  2213. struct file *do_filp_open(int dfd, const char *pathname,
  2214. const struct open_flags *op, int flags)
  2215. {
  2216. struct nameidata nd;
  2217. struct file *filp;
  2218. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2219. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2220. filp = path_openat(dfd, pathname, &nd, op, flags);
  2221. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2222. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2223. return filp;
  2224. }
  2225. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2226. const char *name, const struct open_flags *op, int flags)
  2227. {
  2228. struct nameidata nd;
  2229. struct file *file;
  2230. nd.root.mnt = mnt;
  2231. nd.root.dentry = dentry;
  2232. flags |= LOOKUP_ROOT;
  2233. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2234. return ERR_PTR(-ELOOP);
  2235. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2236. if (unlikely(file == ERR_PTR(-ECHILD)))
  2237. file = path_openat(-1, name, &nd, op, flags);
  2238. if (unlikely(file == ERR_PTR(-ESTALE)))
  2239. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2240. return file;
  2241. }
  2242. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2243. {
  2244. struct dentry *dentry = ERR_PTR(-EEXIST);
  2245. struct nameidata nd;
  2246. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2247. if (error)
  2248. return ERR_PTR(error);
  2249. /*
  2250. * Yucky last component or no last component at all?
  2251. * (foo/., foo/.., /////)
  2252. */
  2253. if (nd.last_type != LAST_NORM)
  2254. goto out;
  2255. nd.flags &= ~LOOKUP_PARENT;
  2256. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2257. nd.intent.open.flags = O_EXCL;
  2258. /*
  2259. * Do the final lookup.
  2260. */
  2261. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2262. dentry = lookup_hash(&nd);
  2263. if (IS_ERR(dentry))
  2264. goto fail;
  2265. if (dentry->d_inode)
  2266. goto eexist;
  2267. /*
  2268. * Special case - lookup gave negative, but... we had foo/bar/
  2269. * From the vfs_mknod() POV we just have a negative dentry -
  2270. * all is fine. Let's be bastards - you had / on the end, you've
  2271. * been asking for (non-existent) directory. -ENOENT for you.
  2272. */
  2273. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2274. dput(dentry);
  2275. dentry = ERR_PTR(-ENOENT);
  2276. goto fail;
  2277. }
  2278. *path = nd.path;
  2279. return dentry;
  2280. eexist:
  2281. dput(dentry);
  2282. dentry = ERR_PTR(-EEXIST);
  2283. fail:
  2284. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2285. out:
  2286. path_put(&nd.path);
  2287. return dentry;
  2288. }
  2289. EXPORT_SYMBOL(kern_path_create);
  2290. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2291. {
  2292. char *tmp = getname(pathname);
  2293. struct dentry *res;
  2294. if (IS_ERR(tmp))
  2295. return ERR_CAST(tmp);
  2296. res = kern_path_create(dfd, tmp, path, is_dir);
  2297. putname(tmp);
  2298. return res;
  2299. }
  2300. EXPORT_SYMBOL(user_path_create);
  2301. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2302. {
  2303. int error = may_create(dir, dentry);
  2304. if (error)
  2305. return error;
  2306. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2307. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2308. return -EPERM;
  2309. if (!dir->i_op->mknod)
  2310. return -EPERM;
  2311. error = devcgroup_inode_mknod(mode, dev);
  2312. if (error)
  2313. return error;
  2314. error = security_inode_mknod(dir, dentry, mode, dev);
  2315. if (error)
  2316. return error;
  2317. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2318. if (!error)
  2319. fsnotify_create(dir, dentry);
  2320. return error;
  2321. }
  2322. static int may_mknod(umode_t mode)
  2323. {
  2324. switch (mode & S_IFMT) {
  2325. case S_IFREG:
  2326. case S_IFCHR:
  2327. case S_IFBLK:
  2328. case S_IFIFO:
  2329. case S_IFSOCK:
  2330. case 0: /* zero mode translates to S_IFREG */
  2331. return 0;
  2332. case S_IFDIR:
  2333. return -EPERM;
  2334. default:
  2335. return -EINVAL;
  2336. }
  2337. }
  2338. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  2339. unsigned, dev)
  2340. {
  2341. struct dentry *dentry;
  2342. struct path path;
  2343. int error;
  2344. if (S_ISDIR(mode))
  2345. return -EPERM;
  2346. dentry = user_path_create(dfd, filename, &path, 0);
  2347. if (IS_ERR(dentry))
  2348. return PTR_ERR(dentry);
  2349. if (!IS_POSIXACL(path.dentry->d_inode))
  2350. mode &= ~current_umask();
  2351. error = may_mknod(mode);
  2352. if (error)
  2353. goto out_dput;
  2354. error = mnt_want_write(path.mnt);
  2355. if (error)
  2356. goto out_dput;
  2357. error = security_path_mknod(&path, dentry, mode, dev);
  2358. if (error)
  2359. goto out_drop_write;
  2360. switch (mode & S_IFMT) {
  2361. case 0: case S_IFREG:
  2362. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2363. break;
  2364. case S_IFCHR: case S_IFBLK:
  2365. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2366. new_decode_dev(dev));
  2367. break;
  2368. case S_IFIFO: case S_IFSOCK:
  2369. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2370. break;
  2371. }
  2372. out_drop_write:
  2373. mnt_drop_write(path.mnt);
  2374. out_dput:
  2375. dput(dentry);
  2376. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2377. path_put(&path);
  2378. return error;
  2379. }
  2380. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  2381. {
  2382. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2383. }
  2384. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2385. {
  2386. int error = may_create(dir, dentry);
  2387. unsigned max_links = dir->i_sb->s_max_links;
  2388. if (error)
  2389. return error;
  2390. if (!dir->i_op->mkdir)
  2391. return -EPERM;
  2392. mode &= (S_IRWXUGO|S_ISVTX);
  2393. error = security_inode_mkdir(dir, dentry, mode);
  2394. if (error)
  2395. return error;
  2396. if (max_links && dir->i_nlink >= max_links)
  2397. return -EMLINK;
  2398. error = dir->i_op->mkdir(dir, dentry, mode);
  2399. if (!error)
  2400. fsnotify_mkdir(dir, dentry);
  2401. return error;
  2402. }
  2403. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  2404. {
  2405. struct dentry *dentry;
  2406. struct path path;
  2407. int error;
  2408. dentry = user_path_create(dfd, pathname, &path, 1);
  2409. if (IS_ERR(dentry))
  2410. return PTR_ERR(dentry);
  2411. if (!IS_POSIXACL(path.dentry->d_inode))
  2412. mode &= ~current_umask();
  2413. error = mnt_want_write(path.mnt);
  2414. if (error)
  2415. goto out_dput;
  2416. error = security_path_mkdir(&path, dentry, mode);
  2417. if (error)
  2418. goto out_drop_write;
  2419. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2420. out_drop_write:
  2421. mnt_drop_write(path.mnt);
  2422. out_dput:
  2423. dput(dentry);
  2424. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2425. path_put(&path);
  2426. return error;
  2427. }
  2428. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  2429. {
  2430. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2431. }
  2432. /*
  2433. * The dentry_unhash() helper will try to drop the dentry early: we
  2434. * should have a usage count of 2 if we're the only user of this
  2435. * dentry, and if that is true (possibly after pruning the dcache),
  2436. * then we drop the dentry now.
  2437. *
  2438. * A low-level filesystem can, if it choses, legally
  2439. * do a
  2440. *
  2441. * if (!d_unhashed(dentry))
  2442. * return -EBUSY;
  2443. *
  2444. * if it cannot handle the case of removing a directory
  2445. * that is still in use by something else..
  2446. */
  2447. void dentry_unhash(struct dentry *dentry)
  2448. {
  2449. shrink_dcache_parent(dentry);
  2450. spin_lock(&dentry->d_lock);
  2451. if (dentry->d_count == 1)
  2452. __d_drop(dentry);
  2453. spin_unlock(&dentry->d_lock);
  2454. }
  2455. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2456. {
  2457. int error = may_delete(dir, dentry, 1);
  2458. if (error)
  2459. return error;
  2460. if (!dir->i_op->rmdir)
  2461. return -EPERM;
  2462. dget(dentry);
  2463. mutex_lock(&dentry->d_inode->i_mutex);
  2464. error = -EBUSY;
  2465. if (d_mountpoint(dentry))
  2466. goto out;
  2467. error = security_inode_rmdir(dir, dentry);
  2468. if (error)
  2469. goto out;
  2470. shrink_dcache_parent(dentry);
  2471. error = dir->i_op->rmdir(dir, dentry);
  2472. if (error)
  2473. goto out;
  2474. dentry->d_inode->i_flags |= S_DEAD;
  2475. dont_mount(dentry);
  2476. out:
  2477. mutex_unlock(&dentry->d_inode->i_mutex);
  2478. dput(dentry);
  2479. if (!error)
  2480. d_delete(dentry);
  2481. return error;
  2482. }
  2483. static long do_rmdir(int dfd, const char __user *pathname)
  2484. {
  2485. int error = 0;
  2486. char * name;
  2487. struct dentry *dentry;
  2488. struct nameidata nd;
  2489. error = user_path_parent(dfd, pathname, &nd, &name);
  2490. if (error)
  2491. return error;
  2492. switch(nd.last_type) {
  2493. case LAST_DOTDOT:
  2494. error = -ENOTEMPTY;
  2495. goto exit1;
  2496. case LAST_DOT:
  2497. error = -EINVAL;
  2498. goto exit1;
  2499. case LAST_ROOT:
  2500. error = -EBUSY;
  2501. goto exit1;
  2502. }
  2503. nd.flags &= ~LOOKUP_PARENT;
  2504. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2505. dentry = lookup_hash(&nd);
  2506. error = PTR_ERR(dentry);
  2507. if (IS_ERR(dentry))
  2508. goto exit2;
  2509. if (!dentry->d_inode) {
  2510. error = -ENOENT;
  2511. goto exit3;
  2512. }
  2513. error = mnt_want_write(nd.path.mnt);
  2514. if (error)
  2515. goto exit3;
  2516. error = security_path_rmdir(&nd.path, dentry);
  2517. if (error)
  2518. goto exit4;
  2519. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2520. exit4:
  2521. mnt_drop_write(nd.path.mnt);
  2522. exit3:
  2523. dput(dentry);
  2524. exit2:
  2525. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2526. exit1:
  2527. path_put(&nd.path);
  2528. putname(name);
  2529. return error;
  2530. }
  2531. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2532. {
  2533. return do_rmdir(AT_FDCWD, pathname);
  2534. }
  2535. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2536. {
  2537. int error = may_delete(dir, dentry, 0);
  2538. if (error)
  2539. return error;
  2540. if (!dir->i_op->unlink)
  2541. return -EPERM;
  2542. mutex_lock(&dentry->d_inode->i_mutex);
  2543. if (d_mountpoint(dentry))
  2544. error = -EBUSY;
  2545. else {
  2546. error = security_inode_unlink(dir, dentry);
  2547. if (!error) {
  2548. error = dir->i_op->unlink(dir, dentry);
  2549. if (!error)
  2550. dont_mount(dentry);
  2551. }
  2552. }
  2553. mutex_unlock(&dentry->d_inode->i_mutex);
  2554. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2555. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2556. fsnotify_link_count(dentry->d_inode);
  2557. d_delete(dentry);
  2558. }
  2559. return error;
  2560. }
  2561. /*
  2562. * Make sure that the actual truncation of the file will occur outside its
  2563. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2564. * writeout happening, and we don't want to prevent access to the directory
  2565. * while waiting on the I/O.
  2566. */
  2567. static long do_unlinkat(int dfd, const char __user *pathname)
  2568. {
  2569. int error;
  2570. char *name;
  2571. struct dentry *dentry;
  2572. struct nameidata nd;
  2573. struct inode *inode = NULL;
  2574. error = user_path_parent(dfd, pathname, &nd, &name);
  2575. if (error)
  2576. return error;
  2577. error = -EISDIR;
  2578. if (nd.last_type != LAST_NORM)
  2579. goto exit1;
  2580. nd.flags &= ~LOOKUP_PARENT;
  2581. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2582. dentry = lookup_hash(&nd);
  2583. error = PTR_ERR(dentry);
  2584. if (!IS_ERR(dentry)) {
  2585. /* Why not before? Because we want correct error value */
  2586. if (nd.last.name[nd.last.len])
  2587. goto slashes;
  2588. inode = dentry->d_inode;
  2589. if (!inode)
  2590. goto slashes;
  2591. ihold(inode);
  2592. error = mnt_want_write(nd.path.mnt);
  2593. if (error)
  2594. goto exit2;
  2595. error = security_path_unlink(&nd.path, dentry);
  2596. if (error)
  2597. goto exit3;
  2598. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2599. exit3:
  2600. mnt_drop_write(nd.path.mnt);
  2601. exit2:
  2602. dput(dentry);
  2603. }
  2604. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2605. if (inode)
  2606. iput(inode); /* truncate the inode here */
  2607. exit1:
  2608. path_put(&nd.path);
  2609. putname(name);
  2610. return error;
  2611. slashes:
  2612. error = !dentry->d_inode ? -ENOENT :
  2613. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2614. goto exit2;
  2615. }
  2616. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2617. {
  2618. if ((flag & ~AT_REMOVEDIR) != 0)
  2619. return -EINVAL;
  2620. if (flag & AT_REMOVEDIR)
  2621. return do_rmdir(dfd, pathname);
  2622. return do_unlinkat(dfd, pathname);
  2623. }
  2624. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2625. {
  2626. return do_unlinkat(AT_FDCWD, pathname);
  2627. }
  2628. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2629. {
  2630. int error = may_create(dir, dentry);
  2631. if (error)
  2632. return error;
  2633. if (!dir->i_op->symlink)
  2634. return -EPERM;
  2635. error = security_inode_symlink(dir, dentry, oldname);
  2636. if (error)
  2637. return error;
  2638. error = dir->i_op->symlink(dir, dentry, oldname);
  2639. if (!error)
  2640. fsnotify_create(dir, dentry);
  2641. return error;
  2642. }
  2643. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2644. int, newdfd, const char __user *, newname)
  2645. {
  2646. int error;
  2647. char *from;
  2648. struct dentry *dentry;
  2649. struct path path;
  2650. from = getname(oldname);
  2651. if (IS_ERR(from))
  2652. return PTR_ERR(from);
  2653. dentry = user_path_create(newdfd, newname, &path, 0);
  2654. error = PTR_ERR(dentry);
  2655. if (IS_ERR(dentry))
  2656. goto out_putname;
  2657. error = mnt_want_write(path.mnt);
  2658. if (error)
  2659. goto out_dput;
  2660. error = security_path_symlink(&path, dentry, from);
  2661. if (error)
  2662. goto out_drop_write;
  2663. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2664. out_drop_write:
  2665. mnt_drop_write(path.mnt);
  2666. out_dput:
  2667. dput(dentry);
  2668. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2669. path_put(&path);
  2670. out_putname:
  2671. putname(from);
  2672. return error;
  2673. }
  2674. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2675. {
  2676. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2677. }
  2678. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2679. {
  2680. struct inode *inode = old_dentry->d_inode;
  2681. unsigned max_links = dir->i_sb->s_max_links;
  2682. int error;
  2683. if (!inode)
  2684. return -ENOENT;
  2685. error = may_create(dir, new_dentry);
  2686. if (error)
  2687. return error;
  2688. if (dir->i_sb != inode->i_sb)
  2689. return -EXDEV;
  2690. /*
  2691. * A link to an append-only or immutable file cannot be created.
  2692. */
  2693. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2694. return -EPERM;
  2695. if (!dir->i_op->link)
  2696. return -EPERM;
  2697. if (S_ISDIR(inode->i_mode))
  2698. return -EPERM;
  2699. error = security_inode_link(old_dentry, dir, new_dentry);
  2700. if (error)
  2701. return error;
  2702. mutex_lock(&inode->i_mutex);
  2703. /* Make sure we don't allow creating hardlink to an unlinked file */
  2704. if (inode->i_nlink == 0)
  2705. error = -ENOENT;
  2706. else if (max_links && inode->i_nlink >= max_links)
  2707. error = -EMLINK;
  2708. else
  2709. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2710. mutex_unlock(&inode->i_mutex);
  2711. if (!error)
  2712. fsnotify_link(dir, inode, new_dentry);
  2713. return error;
  2714. }
  2715. /*
  2716. * Hardlinks are often used in delicate situations. We avoid
  2717. * security-related surprises by not following symlinks on the
  2718. * newname. --KAB
  2719. *
  2720. * We don't follow them on the oldname either to be compatible
  2721. * with linux 2.0, and to avoid hard-linking to directories
  2722. * and other special files. --ADM
  2723. */
  2724. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2725. int, newdfd, const char __user *, newname, int, flags)
  2726. {
  2727. struct dentry *new_dentry;
  2728. struct path old_path, new_path;
  2729. int how = 0;
  2730. int error;
  2731. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2732. return -EINVAL;
  2733. /*
  2734. * To use null names we require CAP_DAC_READ_SEARCH
  2735. * This ensures that not everyone will be able to create
  2736. * handlink using the passed filedescriptor.
  2737. */
  2738. if (flags & AT_EMPTY_PATH) {
  2739. if (!capable(CAP_DAC_READ_SEARCH))
  2740. return -ENOENT;
  2741. how = LOOKUP_EMPTY;
  2742. }
  2743. if (flags & AT_SYMLINK_FOLLOW)
  2744. how |= LOOKUP_FOLLOW;
  2745. error = user_path_at(olddfd, oldname, how, &old_path);
  2746. if (error)
  2747. return error;
  2748. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2749. error = PTR_ERR(new_dentry);
  2750. if (IS_ERR(new_dentry))
  2751. goto out;
  2752. error = -EXDEV;
  2753. if (old_path.mnt != new_path.mnt)
  2754. goto out_dput;
  2755. error = mnt_want_write(new_path.mnt);
  2756. if (error)
  2757. goto out_dput;
  2758. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2759. if (error)
  2760. goto out_drop_write;
  2761. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2762. out_drop_write:
  2763. mnt_drop_write(new_path.mnt);
  2764. out_dput:
  2765. dput(new_dentry);
  2766. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2767. path_put(&new_path);
  2768. out:
  2769. path_put(&old_path);
  2770. return error;
  2771. }
  2772. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2773. {
  2774. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2775. }
  2776. /*
  2777. * The worst of all namespace operations - renaming directory. "Perverted"
  2778. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2779. * Problems:
  2780. * a) we can get into loop creation. Check is done in is_subdir().
  2781. * b) race potential - two innocent renames can create a loop together.
  2782. * That's where 4.4 screws up. Current fix: serialization on
  2783. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2784. * story.
  2785. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2786. * And that - after we got ->i_mutex on parents (until then we don't know
  2787. * whether the target exists). Solution: try to be smart with locking
  2788. * order for inodes. We rely on the fact that tree topology may change
  2789. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2790. * move will be locked. Thus we can rank directories by the tree
  2791. * (ancestors first) and rank all non-directories after them.
  2792. * That works since everybody except rename does "lock parent, lookup,
  2793. * lock child" and rename is under ->s_vfs_rename_mutex.
  2794. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2795. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2796. * we'd better make sure that there's no link(2) for them.
  2797. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2798. * we are removing the target. Solution: we will have to grab ->i_mutex
  2799. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2800. * ->i_mutex on parents, which works but leads to some truly excessive
  2801. * locking].
  2802. */
  2803. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2804. struct inode *new_dir, struct dentry *new_dentry)
  2805. {
  2806. int error = 0;
  2807. struct inode *target = new_dentry->d_inode;
  2808. unsigned max_links = new_dir->i_sb->s_max_links;
  2809. /*
  2810. * If we are going to change the parent - check write permissions,
  2811. * we'll need to flip '..'.
  2812. */
  2813. if (new_dir != old_dir) {
  2814. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2815. if (error)
  2816. return error;
  2817. }
  2818. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2819. if (error)
  2820. return error;
  2821. dget(new_dentry);
  2822. if (target)
  2823. mutex_lock(&target->i_mutex);
  2824. error = -EBUSY;
  2825. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2826. goto out;
  2827. error = -EMLINK;
  2828. if (max_links && !target && new_dir != old_dir &&
  2829. new_dir->i_nlink >= max_links)
  2830. goto out;
  2831. if (target)
  2832. shrink_dcache_parent(new_dentry);
  2833. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2834. if (error)
  2835. goto out;
  2836. if (target) {
  2837. target->i_flags |= S_DEAD;
  2838. dont_mount(new_dentry);
  2839. }
  2840. out:
  2841. if (target)
  2842. mutex_unlock(&target->i_mutex);
  2843. dput(new_dentry);
  2844. if (!error)
  2845. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2846. d_move(old_dentry,new_dentry);
  2847. return error;
  2848. }
  2849. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2850. struct inode *new_dir, struct dentry *new_dentry)
  2851. {
  2852. struct inode *target = new_dentry->d_inode;
  2853. int error;
  2854. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2855. if (error)
  2856. return error;
  2857. dget(new_dentry);
  2858. if (target)
  2859. mutex_lock(&target->i_mutex);
  2860. error = -EBUSY;
  2861. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2862. goto out;
  2863. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2864. if (error)
  2865. goto out;
  2866. if (target)
  2867. dont_mount(new_dentry);
  2868. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2869. d_move(old_dentry, new_dentry);
  2870. out:
  2871. if (target)
  2872. mutex_unlock(&target->i_mutex);
  2873. dput(new_dentry);
  2874. return error;
  2875. }
  2876. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2877. struct inode *new_dir, struct dentry *new_dentry)
  2878. {
  2879. int error;
  2880. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2881. const unsigned char *old_name;
  2882. if (old_dentry->d_inode == new_dentry->d_inode)
  2883. return 0;
  2884. error = may_delete(old_dir, old_dentry, is_dir);
  2885. if (error)
  2886. return error;
  2887. if (!new_dentry->d_inode)
  2888. error = may_create(new_dir, new_dentry);
  2889. else
  2890. error = may_delete(new_dir, new_dentry, is_dir);
  2891. if (error)
  2892. return error;
  2893. if (!old_dir->i_op->rename)
  2894. return -EPERM;
  2895. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2896. if (is_dir)
  2897. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2898. else
  2899. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2900. if (!error)
  2901. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2902. new_dentry->d_inode, old_dentry);
  2903. fsnotify_oldname_free(old_name);
  2904. return error;
  2905. }
  2906. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2907. int, newdfd, const char __user *, newname)
  2908. {
  2909. struct dentry *old_dir, *new_dir;
  2910. struct dentry *old_dentry, *new_dentry;
  2911. struct dentry *trap;
  2912. struct nameidata oldnd, newnd;
  2913. char *from;
  2914. char *to;
  2915. int error;
  2916. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2917. if (error)
  2918. goto exit;
  2919. error = user_path_parent(newdfd, newname, &newnd, &to);
  2920. if (error)
  2921. goto exit1;
  2922. error = -EXDEV;
  2923. if (oldnd.path.mnt != newnd.path.mnt)
  2924. goto exit2;
  2925. old_dir = oldnd.path.dentry;
  2926. error = -EBUSY;
  2927. if (oldnd.last_type != LAST_NORM)
  2928. goto exit2;
  2929. new_dir = newnd.path.dentry;
  2930. if (newnd.last_type != LAST_NORM)
  2931. goto exit2;
  2932. oldnd.flags &= ~LOOKUP_PARENT;
  2933. newnd.flags &= ~LOOKUP_PARENT;
  2934. newnd.flags |= LOOKUP_RENAME_TARGET;
  2935. trap = lock_rename(new_dir, old_dir);
  2936. old_dentry = lookup_hash(&oldnd);
  2937. error = PTR_ERR(old_dentry);
  2938. if (IS_ERR(old_dentry))
  2939. goto exit3;
  2940. /* source must exist */
  2941. error = -ENOENT;
  2942. if (!old_dentry->d_inode)
  2943. goto exit4;
  2944. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2945. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2946. error = -ENOTDIR;
  2947. if (oldnd.last.name[oldnd.last.len])
  2948. goto exit4;
  2949. if (newnd.last.name[newnd.last.len])
  2950. goto exit4;
  2951. }
  2952. /* source should not be ancestor of target */
  2953. error = -EINVAL;
  2954. if (old_dentry == trap)
  2955. goto exit4;
  2956. new_dentry = lookup_hash(&newnd);
  2957. error = PTR_ERR(new_dentry);
  2958. if (IS_ERR(new_dentry))
  2959. goto exit4;
  2960. /* target should not be an ancestor of source */
  2961. error = -ENOTEMPTY;
  2962. if (new_dentry == trap)
  2963. goto exit5;
  2964. error = mnt_want_write(oldnd.path.mnt);
  2965. if (error)
  2966. goto exit5;
  2967. error = security_path_rename(&oldnd.path, old_dentry,
  2968. &newnd.path, new_dentry);
  2969. if (error)
  2970. goto exit6;
  2971. error = vfs_rename(old_dir->d_inode, old_dentry,
  2972. new_dir->d_inode, new_dentry);
  2973. exit6:
  2974. mnt_drop_write(oldnd.path.mnt);
  2975. exit5:
  2976. dput(new_dentry);
  2977. exit4:
  2978. dput(old_dentry);
  2979. exit3:
  2980. unlock_rename(new_dir, old_dir);
  2981. exit2:
  2982. path_put(&newnd.path);
  2983. putname(to);
  2984. exit1:
  2985. path_put(&oldnd.path);
  2986. putname(from);
  2987. exit:
  2988. return error;
  2989. }
  2990. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2991. {
  2992. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2993. }
  2994. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2995. {
  2996. int len;
  2997. len = PTR_ERR(link);
  2998. if (IS_ERR(link))
  2999. goto out;
  3000. len = strlen(link);
  3001. if (len > (unsigned) buflen)
  3002. len = buflen;
  3003. if (copy_to_user(buffer, link, len))
  3004. len = -EFAULT;
  3005. out:
  3006. return len;
  3007. }
  3008. /*
  3009. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  3010. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  3011. * using) it for any given inode is up to filesystem.
  3012. */
  3013. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3014. {
  3015. struct nameidata nd;
  3016. void *cookie;
  3017. int res;
  3018. nd.depth = 0;
  3019. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3020. if (IS_ERR(cookie))
  3021. return PTR_ERR(cookie);
  3022. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  3023. if (dentry->d_inode->i_op->put_link)
  3024. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3025. return res;
  3026. }
  3027. int vfs_follow_link(struct nameidata *nd, const char *link)
  3028. {
  3029. return __vfs_follow_link(nd, link);
  3030. }
  3031. /* get the link contents into pagecache */
  3032. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3033. {
  3034. char *kaddr;
  3035. struct page *page;
  3036. struct address_space *mapping = dentry->d_inode->i_mapping;
  3037. page = read_mapping_page(mapping, 0, NULL);
  3038. if (IS_ERR(page))
  3039. return (char*)page;
  3040. *ppage = page;
  3041. kaddr = kmap(page);
  3042. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3043. return kaddr;
  3044. }
  3045. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3046. {
  3047. struct page *page = NULL;
  3048. char *s = page_getlink(dentry, &page);
  3049. int res = vfs_readlink(dentry,buffer,buflen,s);
  3050. if (page) {
  3051. kunmap(page);
  3052. page_cache_release(page);
  3053. }
  3054. return res;
  3055. }
  3056. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3057. {
  3058. struct page *page = NULL;
  3059. nd_set_link(nd, page_getlink(dentry, &page));
  3060. return page;
  3061. }
  3062. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3063. {
  3064. struct page *page = cookie;
  3065. if (page) {
  3066. kunmap(page);
  3067. page_cache_release(page);
  3068. }
  3069. }
  3070. /*
  3071. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3072. */
  3073. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3074. {
  3075. struct address_space *mapping = inode->i_mapping;
  3076. struct page *page;
  3077. void *fsdata;
  3078. int err;
  3079. char *kaddr;
  3080. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3081. if (nofs)
  3082. flags |= AOP_FLAG_NOFS;
  3083. retry:
  3084. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3085. flags, &page, &fsdata);
  3086. if (err)
  3087. goto fail;
  3088. kaddr = kmap_atomic(page);
  3089. memcpy(kaddr, symname, len-1);
  3090. kunmap_atomic(kaddr);
  3091. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3092. page, fsdata);
  3093. if (err < 0)
  3094. goto fail;
  3095. if (err < len-1)
  3096. goto retry;
  3097. mark_inode_dirty(inode);
  3098. return 0;
  3099. fail:
  3100. return err;
  3101. }
  3102. int page_symlink(struct inode *inode, const char *symname, int len)
  3103. {
  3104. return __page_symlink(inode, symname, len,
  3105. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3106. }
  3107. const struct inode_operations page_symlink_inode_operations = {
  3108. .readlink = generic_readlink,
  3109. .follow_link = page_follow_link_light,
  3110. .put_link = page_put_link,
  3111. };
  3112. EXPORT_SYMBOL(user_path_at);
  3113. EXPORT_SYMBOL(follow_down_one);
  3114. EXPORT_SYMBOL(follow_down);
  3115. EXPORT_SYMBOL(follow_up);
  3116. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3117. EXPORT_SYMBOL(getname);
  3118. EXPORT_SYMBOL(lock_rename);
  3119. EXPORT_SYMBOL(lookup_one_len);
  3120. EXPORT_SYMBOL(page_follow_link_light);
  3121. EXPORT_SYMBOL(page_put_link);
  3122. EXPORT_SYMBOL(page_readlink);
  3123. EXPORT_SYMBOL(__page_symlink);
  3124. EXPORT_SYMBOL(page_symlink);
  3125. EXPORT_SYMBOL(page_symlink_inode_operations);
  3126. EXPORT_SYMBOL(kern_path);
  3127. EXPORT_SYMBOL(vfs_path_lookup);
  3128. EXPORT_SYMBOL(inode_permission);
  3129. EXPORT_SYMBOL(unlock_rename);
  3130. EXPORT_SYMBOL(vfs_create);
  3131. EXPORT_SYMBOL(vfs_follow_link);
  3132. EXPORT_SYMBOL(vfs_link);
  3133. EXPORT_SYMBOL(vfs_mkdir);
  3134. EXPORT_SYMBOL(vfs_mknod);
  3135. EXPORT_SYMBOL(generic_permission);
  3136. EXPORT_SYMBOL(vfs_readlink);
  3137. EXPORT_SYMBOL(vfs_rename);
  3138. EXPORT_SYMBOL(vfs_rmdir);
  3139. EXPORT_SYMBOL(vfs_symlink);
  3140. EXPORT_SYMBOL(vfs_unlink);
  3141. EXPORT_SYMBOL(dentry_unhash);
  3142. EXPORT_SYMBOL(generic_readlink);