namei.c 69 KB

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