namei.c 70 KB

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