namei.c 71 KB

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