namei.c 83 KB

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