namei.c 71 KB

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