namei.c 84 KB

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