namei.c 70 KB

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