namei.c 82 KB

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