namei.c 84 KB

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