namei.c 69 KB

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