namei.c 82 KB

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