namei.c 84 KB

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