namei.c 82 KB

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