namei.c 70 KB

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