100-overlayfs.patch 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191
  1. --- a/Documentation/filesystems/Locking
  2. +++ b/Documentation/filesystems/Locking
  3. @@ -66,6 +66,7 @@ prototypes:
  4. int (*atomic_open)(struct inode *, struct dentry *,
  5. struct file *, unsigned open_flag,
  6. umode_t create_mode, int *opened);
  7. + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
  8. locking rules:
  9. all may block
  10. @@ -93,6 +94,7 @@ removexattr: yes
  11. fiemap: no
  12. update_time: no
  13. atomic_open: yes
  14. +dentry_open: no
  15. Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
  16. victim.
  17. --- /dev/null
  18. +++ b/Documentation/filesystems/overlayfs.txt
  19. @@ -0,0 +1,199 @@
  20. +Written by: Neil Brown <[email protected]>
  21. +
  22. +Overlay Filesystem
  23. +==================
  24. +
  25. +This document describes a prototype for a new approach to providing
  26. +overlay-filesystem functionality in Linux (sometimes referred to as
  27. +union-filesystems). An overlay-filesystem tries to present a
  28. +filesystem which is the result over overlaying one filesystem on top
  29. +of the other.
  30. +
  31. +The result will inevitably fail to look exactly like a normal
  32. +filesystem for various technical reasons. The expectation is that
  33. +many use cases will be able to ignore these differences.
  34. +
  35. +This approach is 'hybrid' because the objects that appear in the
  36. +filesystem do not all appear to belong to that filesystem. In many
  37. +cases an object accessed in the union will be indistinguishable
  38. +from accessing the corresponding object from the original filesystem.
  39. +This is most obvious from the 'st_dev' field returned by stat(2).
  40. +
  41. +While directories will report an st_dev from the overlay-filesystem,
  42. +all non-directory objects will report an st_dev from the lower or
  43. +upper filesystem that is providing the object. Similarly st_ino will
  44. +only be unique when combined with st_dev, and both of these can change
  45. +over the lifetime of a non-directory object. Many applications and
  46. +tools ignore these values and will not be affected.
  47. +
  48. +Upper and Lower
  49. +---------------
  50. +
  51. +An overlay filesystem combines two filesystems - an 'upper' filesystem
  52. +and a 'lower' filesystem. When a name exists in both filesystems, the
  53. +object in the 'upper' filesystem is visible while the object in the
  54. +'lower' filesystem is either hidden or, in the case of directories,
  55. +merged with the 'upper' object.
  56. +
  57. +It would be more correct to refer to an upper and lower 'directory
  58. +tree' rather than 'filesystem' as it is quite possible for both
  59. +directory trees to be in the same filesystem and there is no
  60. +requirement that the root of a filesystem be given for either upper or
  61. +lower.
  62. +
  63. +The lower filesystem can be any filesystem supported by Linux and does
  64. +not need to be writable. The lower filesystem can even be another
  65. +overlayfs. The upper filesystem will normally be writable and if it
  66. +is it must support the creation of trusted.* extended attributes, and
  67. +must provide valid d_type in readdir responses, at least for symbolic
  68. +links - so NFS is not suitable.
  69. +
  70. +A read-only overlay of two read-only filesystems may use any
  71. +filesystem type.
  72. +
  73. +Directories
  74. +-----------
  75. +
  76. +Overlaying mainly involves directories. If a given name appears in both
  77. +upper and lower filesystems and refers to a non-directory in either,
  78. +then the lower object is hidden - the name refers only to the upper
  79. +object.
  80. +
  81. +Where both upper and lower objects are directories, a merged directory
  82. +is formed.
  83. +
  84. +At mount time, the two directories given as mount options are combined
  85. +into a merged directory:
  86. +
  87. + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
  88. +
  89. +Then whenever a lookup is requested in such a merged directory, the
  90. +lookup is performed in each actual directory and the combined result
  91. +is cached in the dentry belonging to the overlay filesystem. If both
  92. +actual lookups find directories, both are stored and a merged
  93. +directory is created, otherwise only one is stored: the upper if it
  94. +exists, else the lower.
  95. +
  96. +Only the lists of names from directories are merged. Other content
  97. +such as metadata and extended attributes are reported for the upper
  98. +directory only. These attributes of the lower directory are hidden.
  99. +
  100. +whiteouts and opaque directories
  101. +--------------------------------
  102. +
  103. +In order to support rm and rmdir without changing the lower
  104. +filesystem, an overlay filesystem needs to record in the upper filesystem
  105. +that files have been removed. This is done using whiteouts and opaque
  106. +directories (non-directories are always opaque).
  107. +
  108. +The overlay filesystem uses extended attributes with a
  109. +"trusted.overlay." prefix to record these details.
  110. +
  111. +A whiteout is created as a symbolic link with target
  112. +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
  113. +When a whiteout is found in the upper level of a merged directory, any
  114. +matching name in the lower level is ignored, and the whiteout itself
  115. +is also hidden.
  116. +
  117. +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  118. +to "y". Where the upper filesystem contains an opaque directory, any
  119. +directory in the lower filesystem with the same name is ignored.
  120. +
  121. +readdir
  122. +-------
  123. +
  124. +When a 'readdir' request is made on a merged directory, the upper and
  125. +lower directories are each read and the name lists merged in the
  126. +obvious way (upper is read first, then lower - entries that already
  127. +exist are not re-added). This merged name list is cached in the
  128. +'struct file' and so remains as long as the file is kept open. If the
  129. +directory is opened and read by two processes at the same time, they
  130. +will each have separate caches. A seekdir to the start of the
  131. +directory (offset 0) followed by a readdir will cause the cache to be
  132. +discarded and rebuilt.
  133. +
  134. +This means that changes to the merged directory do not appear while a
  135. +directory is being read. This is unlikely to be noticed by many
  136. +programs.
  137. +
  138. +seek offsets are assigned sequentially when the directories are read.
  139. +Thus if
  140. + - read part of a directory
  141. + - remember an offset, and close the directory
  142. + - re-open the directory some time later
  143. + - seek to the remembered offset
  144. +
  145. +there may be little correlation between the old and new locations in
  146. +the list of filenames, particularly if anything has changed in the
  147. +directory.
  148. +
  149. +Readdir on directories that are not merged is simply handled by the
  150. +underlying directory (upper or lower).
  151. +
  152. +
  153. +Non-directories
  154. +---------------
  155. +
  156. +Objects that are not directories (files, symlinks, device-special
  157. +files etc.) are presented either from the upper or lower filesystem as
  158. +appropriate. When a file in the lower filesystem is accessed in a way
  159. +the requires write-access, such as opening for write access, changing
  160. +some metadata etc., the file is first copied from the lower filesystem
  161. +to the upper filesystem (copy_up). Note that creating a hard-link
  162. +also requires copy_up, though of course creation of a symlink does
  163. +not.
  164. +
  165. +The copy_up may turn out to be unnecessary, for example if the file is
  166. +opened for read-write but the data is not modified.
  167. +
  168. +The copy_up process first makes sure that the containing directory
  169. +exists in the upper filesystem - creating it and any parents as
  170. +necessary. It then creates the object with the same metadata (owner,
  171. +mode, mtime, symlink-target etc.) and then if the object is a file, the
  172. +data is copied from the lower to the upper filesystem. Finally any
  173. +extended attributes are copied up.
  174. +
  175. +Once the copy_up is complete, the overlay filesystem simply
  176. +provides direct access to the newly created file in the upper
  177. +filesystem - future operations on the file are barely noticed by the
  178. +overlay filesystem (though an operation on the name of the file such as
  179. +rename or unlink will of course be noticed and handled).
  180. +
  181. +
  182. +Non-standard behavior
  183. +---------------------
  184. +
  185. +The copy_up operation essentially creates a new, identical file and
  186. +moves it over to the old name. The new file may be on a different
  187. +filesystem, so both st_dev and st_ino of the file may change.
  188. +
  189. +Any open files referring to this inode will access the old data and
  190. +metadata. Similarly any file locks obtained before copy_up will not
  191. +apply to the copied up file.
  192. +
  193. +On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
  194. +fsetxattr(2) will fail with EROFS.
  195. +
  196. +If a file with multiple hard links is copied up, then this will
  197. +"break" the link. Changes will not be propagated to other names
  198. +referring to the same inode.
  199. +
  200. +Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
  201. +object in overlayfs will not contain valid absolute paths, only
  202. +relative paths leading up to the filesystem's root. This will be
  203. +fixed in the future.
  204. +
  205. +Some operations are not atomic, for example a crash during copy_up or
  206. +rename will leave the filesystem in an inconsistent state. This will
  207. +be addressed in the future.
  208. +
  209. +Changes to underlying filesystems
  210. +---------------------------------
  211. +
  212. +Offline changes, when the overlay is not mounted, are allowed to either
  213. +the upper or the lower trees.
  214. +
  215. +Changes to the underlying filesystems while part of a mounted overlay
  216. +filesystem are not allowed. If the underlying filesystem is changed,
  217. +the behavior of the overlay is undefined, though it will not result in
  218. +a crash or deadlock.
  219. --- a/Documentation/filesystems/vfs.txt
  220. +++ b/Documentation/filesystems/vfs.txt
  221. @@ -362,6 +362,7 @@ struct inode_operations {
  222. int (*atomic_open)(struct inode *, struct dentry *,
  223. struct file *, unsigned open_flag,
  224. umode_t create_mode, int *opened);
  225. + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
  226. };
  227. Again, all methods are called without any locks being held, unless
  228. @@ -681,6 +682,12 @@ struct address_space_operations {
  229. but instead uses bmap to find out where the blocks in the file
  230. are and uses those addresses directly.
  231. + dentry_open: this is an alternative to f_op->open(), the difference is that
  232. + this method may open a file not necessarily originating from the same
  233. + filesystem as the one i_op->open() was called on. It may be
  234. + useful for stacking filesystems which want to allow native I/O directly
  235. + on underlying files.
  236. +
  237. invalidatepage: If a page has PagePrivate set, then invalidatepage
  238. will be called when part or all of the page is to be removed
  239. --- a/MAINTAINERS
  240. +++ b/MAINTAINERS
  241. @@ -5917,6 +5917,13 @@ F: drivers/scsi/osd/
  242. F: include/scsi/osd_*
  243. F: fs/exofs/
  244. +OVERLAYFS FILESYSTEM
  245. +M: Miklos Szeredi <[email protected]>
  246. +L: [email protected]
  247. +S: Supported
  248. +F: fs/overlayfs/*
  249. +F: Documentation/filesystems/overlayfs.txt
  250. +
  251. P54 WIRELESS DRIVER
  252. M: Christian Lamparter <[email protected]>
  253. L: [email protected]
  254. --- a/fs/Kconfig
  255. +++ b/fs/Kconfig
  256. @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
  257. source "fs/autofs4/Kconfig"
  258. source "fs/fuse/Kconfig"
  259. +source "fs/overlayfs/Kconfig"
  260. config GENERIC_ACL
  261. bool
  262. --- a/fs/Makefile
  263. +++ b/fs/Makefile
  264. @@ -107,6 +107,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
  265. obj-$(CONFIG_AUTOFS4_FS) += autofs4/
  266. obj-$(CONFIG_ADFS_FS) += adfs/
  267. obj-$(CONFIG_FUSE_FS) += fuse/
  268. +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
  269. obj-$(CONFIG_UDF_FS) += udf/
  270. obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
  271. obj-$(CONFIG_OMFS_FS) += omfs/
  272. --- a/fs/ecryptfs/main.c
  273. +++ b/fs/ecryptfs/main.c
  274. @@ -567,6 +567,13 @@ static struct dentry *ecryptfs_mount(str
  275. s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
  276. s->s_blocksize = path.dentry->d_sb->s_blocksize;
  277. s->s_magic = ECRYPTFS_SUPER_MAGIC;
  278. + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
  279. +
  280. + rc = -EINVAL;
  281. + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  282. + printk(KERN_ERR "eCryptfs: maximum fs stacking depth exceeded\n");
  283. + goto out_free;
  284. + }
  285. inode = ecryptfs_get_inode(path.dentry->d_inode, s);
  286. rc = PTR_ERR(inode);
  287. --- a/fs/internal.h
  288. +++ b/fs/internal.h
  289. @@ -42,11 +42,6 @@ static inline int __sync_blockdev(struct
  290. extern void __init chrdev_init(void);
  291. /*
  292. - * namei.c
  293. - */
  294. -extern int __inode_permission(struct inode *, int);
  295. -
  296. -/*
  297. * namespace.c
  298. */
  299. extern int copy_mount_options(const void __user *, unsigned long *);
  300. --- a/fs/namei.c
  301. +++ b/fs/namei.c
  302. @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
  303. return security_inode_permission(inode, mask);
  304. }
  305. +EXPORT_SYMBOL(__inode_permission);
  306. /**
  307. * sb_permission - Check superblock-level permissions
  308. @@ -2867,9 +2868,12 @@ finish_open_created:
  309. error = may_open(&nd->path, acc_mode, open_flag);
  310. if (error)
  311. goto out;
  312. - file->f_path.mnt = nd->path.mnt;
  313. - error = finish_open(file, nd->path.dentry, NULL, opened);
  314. - if (error) {
  315. +
  316. + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  317. + error = vfs_open(&nd->path, file, current_cred());
  318. + if (!error) {
  319. + *opened |= FILE_OPENED;
  320. + } else {
  321. if (error == -EOPENSTALE)
  322. goto stale_open;
  323. goto out;
  324. --- a/fs/namespace.c
  325. +++ b/fs/namespace.c
  326. @@ -1403,6 +1403,24 @@ void drop_collected_mounts(struct vfsmou
  327. release_mounts(&umount_list);
  328. }
  329. +struct vfsmount *clone_private_mount(struct path *path)
  330. +{
  331. + struct mount *old_mnt = real_mount(path->mnt);
  332. + struct mount *new_mnt;
  333. +
  334. + if (IS_MNT_UNBINDABLE(old_mnt))
  335. + return ERR_PTR(-EINVAL);
  336. +
  337. + down_read(&namespace_sem);
  338. + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
  339. + up_read(&namespace_sem);
  340. + if (!new_mnt)
  341. + return ERR_PTR(-ENOMEM);
  342. +
  343. + return &new_mnt->mnt;
  344. +}
  345. +EXPORT_SYMBOL_GPL(clone_private_mount);
  346. +
  347. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  348. struct vfsmount *root)
  349. {
  350. --- a/fs/open.c
  351. +++ b/fs/open.c
  352. @@ -828,8 +828,7 @@ struct file *dentry_open(const struct pa
  353. f = get_empty_filp();
  354. if (!IS_ERR(f)) {
  355. f->f_flags = flags;
  356. - f->f_path = *path;
  357. - error = do_dentry_open(f, NULL, cred);
  358. + error = vfs_open(path, f, cred);
  359. if (!error) {
  360. /* from now on we need fput() to dispose of f */
  361. error = open_check_o_direct(f);
  362. @@ -846,6 +845,26 @@ struct file *dentry_open(const struct pa
  363. }
  364. EXPORT_SYMBOL(dentry_open);
  365. +/**
  366. + * vfs_open - open the file at the given path
  367. + * @path: path to open
  368. + * @filp: newly allocated file with f_flag initialized
  369. + * @cred: credentials to use
  370. + */
  371. +int vfs_open(const struct path *path, struct file *filp,
  372. + const struct cred *cred)
  373. +{
  374. + struct inode *inode = path->dentry->d_inode;
  375. +
  376. + if (inode->i_op->dentry_open)
  377. + return inode->i_op->dentry_open(path->dentry, filp, cred);
  378. + else {
  379. + filp->f_path = *path;
  380. + return do_dentry_open(filp, NULL, cred);
  381. + }
  382. +}
  383. +EXPORT_SYMBOL(vfs_open);
  384. +
  385. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  386. {
  387. int lookup_flags = 0;
  388. --- /dev/null
  389. +++ b/fs/overlayfs/Kconfig
  390. @@ -0,0 +1,4 @@
  391. +config OVERLAYFS_FS
  392. + tristate "Overlay filesystem support"
  393. + help
  394. + Add support for overlay filesystem.
  395. --- /dev/null
  396. +++ b/fs/overlayfs/Makefile
  397. @@ -0,0 +1,7 @@
  398. +#
  399. +# Makefile for the overlay filesystem.
  400. +#
  401. +
  402. +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
  403. +
  404. +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
  405. --- /dev/null
  406. +++ b/fs/overlayfs/copy_up.c
  407. @@ -0,0 +1,385 @@
  408. +/*
  409. + *
  410. + * Copyright (C) 2011 Novell Inc.
  411. + *
  412. + * This program is free software; you can redistribute it and/or modify it
  413. + * under the terms of the GNU General Public License version 2 as published by
  414. + * the Free Software Foundation.
  415. + */
  416. +
  417. +#include <linux/fs.h>
  418. +#include <linux/slab.h>
  419. +#include <linux/file.h>
  420. +#include <linux/splice.h>
  421. +#include <linux/xattr.h>
  422. +#include <linux/security.h>
  423. +#include <linux/uaccess.h>
  424. +#include <linux/sched.h>
  425. +#include "overlayfs.h"
  426. +
  427. +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
  428. +
  429. +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
  430. +{
  431. + ssize_t list_size, size;
  432. + char *buf, *name, *value;
  433. + int error;
  434. +
  435. + if (!old->d_inode->i_op->getxattr ||
  436. + !new->d_inode->i_op->getxattr)
  437. + return 0;
  438. +
  439. + list_size = vfs_listxattr(old, NULL, 0);
  440. + if (list_size <= 0) {
  441. + if (list_size == -EOPNOTSUPP)
  442. + return 0;
  443. + return list_size;
  444. + }
  445. +
  446. + buf = kzalloc(list_size, GFP_KERNEL);
  447. + if (!buf)
  448. + return -ENOMEM;
  449. +
  450. + error = -ENOMEM;
  451. + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
  452. + if (!value)
  453. + goto out;
  454. +
  455. + list_size = vfs_listxattr(old, buf, list_size);
  456. + if (list_size <= 0) {
  457. + error = list_size;
  458. + goto out_free_value;
  459. + }
  460. +
  461. + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
  462. + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
  463. + if (size <= 0) {
  464. + error = size;
  465. + goto out_free_value;
  466. + }
  467. + error = vfs_setxattr(new, name, value, size, 0);
  468. + if (error)
  469. + goto out_free_value;
  470. + }
  471. +
  472. +out_free_value:
  473. + kfree(value);
  474. +out:
  475. + kfree(buf);
  476. + return error;
  477. +}
  478. +
  479. +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
  480. +{
  481. + struct file *old_file;
  482. + struct file *new_file;
  483. + int error = 0;
  484. +
  485. + if (len == 0)
  486. + return 0;
  487. +
  488. + old_file = ovl_path_open(old, O_RDONLY);
  489. + if (IS_ERR(old_file))
  490. + return PTR_ERR(old_file);
  491. +
  492. + new_file = ovl_path_open(new, O_WRONLY);
  493. + if (IS_ERR(new_file)) {
  494. + error = PTR_ERR(new_file);
  495. + goto out_fput;
  496. + }
  497. +
  498. + /* FIXME: copy up sparse files efficiently */
  499. + while (len) {
  500. + loff_t offset = new_file->f_pos;
  501. + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  502. + long bytes;
  503. +
  504. + if (len < this_len)
  505. + this_len = len;
  506. +
  507. + if (signal_pending_state(TASK_KILLABLE, current)) {
  508. + error = -EINTR;
  509. + break;
  510. + }
  511. +
  512. + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
  513. + SPLICE_F_MOVE);
  514. + if (bytes <= 0) {
  515. + error = bytes;
  516. + break;
  517. + }
  518. +
  519. + len -= bytes;
  520. + }
  521. +
  522. + fput(new_file);
  523. +out_fput:
  524. + fput(old_file);
  525. + return error;
  526. +}
  527. +
  528. +static char *ovl_read_symlink(struct dentry *realdentry)
  529. +{
  530. + int res;
  531. + char *buf;
  532. + struct inode *inode = realdentry->d_inode;
  533. + mm_segment_t old_fs;
  534. +
  535. + res = -EINVAL;
  536. + if (!inode->i_op->readlink)
  537. + goto err;
  538. +
  539. + res = -ENOMEM;
  540. + buf = (char *) __get_free_page(GFP_KERNEL);
  541. + if (!buf)
  542. + goto err;
  543. +
  544. + old_fs = get_fs();
  545. + set_fs(get_ds());
  546. + /* The cast to a user pointer is valid due to the set_fs() */
  547. + res = inode->i_op->readlink(realdentry,
  548. + (char __user *)buf, PAGE_SIZE - 1);
  549. + set_fs(old_fs);
  550. + if (res < 0) {
  551. + free_page((unsigned long) buf);
  552. + goto err;
  553. + }
  554. + buf[res] = '\0';
  555. +
  556. + return buf;
  557. +
  558. +err:
  559. + return ERR_PTR(res);
  560. +}
  561. +
  562. +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  563. +{
  564. + struct iattr attr = {
  565. + .ia_valid =
  566. + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  567. + .ia_atime = stat->atime,
  568. + .ia_mtime = stat->mtime,
  569. + };
  570. +
  571. + return notify_change(upperdentry, &attr);
  572. +}
  573. +
  574. +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
  575. +{
  576. + struct iattr attr = {
  577. + .ia_valid = ATTR_MODE,
  578. + .ia_mode = mode,
  579. + };
  580. +
  581. + return notify_change(upperdentry, &attr);
  582. +}
  583. +
  584. +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
  585. + struct path *lowerpath, struct kstat *stat,
  586. + const char *link)
  587. +{
  588. + int err;
  589. + struct path newpath;
  590. + umode_t mode = stat->mode;
  591. +
  592. + /* Can't properly set mode on creation because of the umask */
  593. + stat->mode &= S_IFMT;
  594. +
  595. + ovl_path_upper(dentry, &newpath);
  596. + WARN_ON(newpath.dentry);
  597. + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
  598. + if (IS_ERR(newpath.dentry))
  599. + return PTR_ERR(newpath.dentry);
  600. +
  601. + if (S_ISREG(stat->mode)) {
  602. + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
  603. + if (err)
  604. + goto err_remove;
  605. + }
  606. +
  607. + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
  608. + if (err)
  609. + goto err_remove;
  610. +
  611. + mutex_lock(&newpath.dentry->d_inode->i_mutex);
  612. + if (!S_ISLNK(stat->mode))
  613. + err = ovl_set_mode(newpath.dentry, mode);
  614. + if (!err)
  615. + err = ovl_set_timestamps(newpath.dentry, stat);
  616. + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
  617. + if (err)
  618. + goto err_remove;
  619. +
  620. + ovl_dentry_update(dentry, newpath.dentry);
  621. +
  622. + /*
  623. + * Easiest way to get rid of the lower dentry reference is to
  624. + * drop this dentry. This is neither needed nor possible for
  625. + * directories.
  626. + */
  627. + if (!S_ISDIR(stat->mode))
  628. + d_drop(dentry);
  629. +
  630. + return 0;
  631. +
  632. +err_remove:
  633. + if (S_ISDIR(stat->mode))
  634. + vfs_rmdir(upperdir->d_inode, newpath.dentry);
  635. + else
  636. + vfs_unlink(upperdir->d_inode, newpath.dentry);
  637. +
  638. + dput(newpath.dentry);
  639. +
  640. + return err;
  641. +}
  642. +
  643. +/*
  644. + * Copy up a single dentry
  645. + *
  646. + * Directory renames only allowed on "pure upper" (already created on
  647. + * upper filesystem, never copied up). Directories which are on lower or
  648. + * are merged may not be renamed. For these -EXDEV is returned and
  649. + * userspace has to deal with it. This means, when copying up a
  650. + * directory we can rely on it and ancestors being stable.
  651. + *
  652. + * Non-directory renames start with copy up of source if necessary. The
  653. + * actual rename will only proceed once the copy up was successful. Copy
  654. + * up uses upper parent i_mutex for exclusion. Since rename can change
  655. + * d_parent it is possible that the copy up will lock the old parent. At
  656. + * that point the file will have already been copied up anyway.
  657. + */
  658. +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  659. + struct path *lowerpath, struct kstat *stat)
  660. +{
  661. + int err;
  662. + struct kstat pstat;
  663. + struct path parentpath;
  664. + struct dentry *upperdir;
  665. + const struct cred *old_cred;
  666. + struct cred *override_cred;
  667. + char *link = NULL;
  668. +
  669. + ovl_path_upper(parent, &parentpath);
  670. + upperdir = parentpath.dentry;
  671. +
  672. + err = vfs_getattr(&parentpath, &pstat);
  673. + if (err)
  674. + return err;
  675. +
  676. + if (S_ISLNK(stat->mode)) {
  677. + link = ovl_read_symlink(lowerpath->dentry);
  678. + if (IS_ERR(link))
  679. + return PTR_ERR(link);
  680. + }
  681. +
  682. + err = -ENOMEM;
  683. + override_cred = prepare_creds();
  684. + if (!override_cred)
  685. + goto out_free_link;
  686. +
  687. + override_cred->fsuid = stat->uid;
  688. + override_cred->fsgid = stat->gid;
  689. + /*
  690. + * CAP_SYS_ADMIN for copying up extended attributes
  691. + * CAP_DAC_OVERRIDE for create
  692. + * CAP_FOWNER for chmod, timestamp update
  693. + * CAP_FSETID for chmod
  694. + * CAP_MKNOD for mknod
  695. + */
  696. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  697. + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  698. + cap_raise(override_cred->cap_effective, CAP_FOWNER);
  699. + cap_raise(override_cred->cap_effective, CAP_FSETID);
  700. + cap_raise(override_cred->cap_effective, CAP_MKNOD);
  701. + old_cred = override_creds(override_cred);
  702. +
  703. + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
  704. + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
  705. + err = 0;
  706. + } else {
  707. + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
  708. + stat, link);
  709. + if (!err) {
  710. + /* Restore timestamps on parent (best effort) */
  711. + ovl_set_timestamps(upperdir, &pstat);
  712. + }
  713. + }
  714. +
  715. + mutex_unlock(&upperdir->d_inode->i_mutex);
  716. +
  717. + revert_creds(old_cred);
  718. + put_cred(override_cred);
  719. +
  720. +out_free_link:
  721. + if (link)
  722. + free_page((unsigned long) link);
  723. +
  724. + return err;
  725. +}
  726. +
  727. +int ovl_copy_up(struct dentry *dentry)
  728. +{
  729. + int err;
  730. +
  731. + err = 0;
  732. + while (!err) {
  733. + struct dentry *next;
  734. + struct dentry *parent;
  735. + struct path lowerpath;
  736. + struct kstat stat;
  737. + enum ovl_path_type type = ovl_path_type(dentry);
  738. +
  739. + if (type != OVL_PATH_LOWER)
  740. + break;
  741. +
  742. + next = dget(dentry);
  743. + /* find the topmost dentry not yet copied up */
  744. + for (;;) {
  745. + parent = dget_parent(next);
  746. +
  747. + type = ovl_path_type(parent);
  748. + if (type != OVL_PATH_LOWER)
  749. + break;
  750. +
  751. + dput(next);
  752. + next = parent;
  753. + }
  754. +
  755. + ovl_path_lower(next, &lowerpath);
  756. + err = vfs_getattr(&lowerpath, &stat);
  757. + if (!err)
  758. + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
  759. +
  760. + dput(parent);
  761. + dput(next);
  762. + }
  763. +
  764. + return err;
  765. +}
  766. +
  767. +/* Optimize by not copying up the file first and truncating later */
  768. +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
  769. +{
  770. + int err;
  771. + struct kstat stat;
  772. + struct path lowerpath;
  773. + struct dentry *parent = dget_parent(dentry);
  774. +
  775. + err = ovl_copy_up(parent);
  776. + if (err)
  777. + goto out_dput_parent;
  778. +
  779. + ovl_path_lower(dentry, &lowerpath);
  780. + err = vfs_getattr(&lowerpath, &stat);
  781. + if (err)
  782. + goto out_dput_parent;
  783. +
  784. + if (size < stat.size)
  785. + stat.size = size;
  786. +
  787. + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
  788. +
  789. +out_dput_parent:
  790. + dput(parent);
  791. + return err;
  792. +}
  793. --- /dev/null
  794. +++ b/fs/overlayfs/dir.c
  795. @@ -0,0 +1,604 @@
  796. +/*
  797. + *
  798. + * Copyright (C) 2011 Novell Inc.
  799. + *
  800. + * This program is free software; you can redistribute it and/or modify it
  801. + * under the terms of the GNU General Public License version 2 as published by
  802. + * the Free Software Foundation.
  803. + */
  804. +
  805. +#include <linux/fs.h>
  806. +#include <linux/namei.h>
  807. +#include <linux/xattr.h>
  808. +#include <linux/security.h>
  809. +#include <linux/cred.h>
  810. +#include "overlayfs.h"
  811. +
  812. +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
  813. +
  814. +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
  815. +{
  816. + int err;
  817. + struct dentry *newdentry;
  818. + const struct cred *old_cred;
  819. + struct cred *override_cred;
  820. +
  821. + /* FIXME: recheck lower dentry to see if whiteout is really needed */
  822. +
  823. + err = -ENOMEM;
  824. + override_cred = prepare_creds();
  825. + if (!override_cred)
  826. + goto out;
  827. +
  828. + /*
  829. + * CAP_SYS_ADMIN for setxattr
  830. + * CAP_DAC_OVERRIDE for symlink creation
  831. + * CAP_FOWNER for unlink in sticky directory
  832. + */
  833. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  834. + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  835. + cap_raise(override_cred->cap_effective, CAP_FOWNER);
  836. + override_cred->fsuid = GLOBAL_ROOT_UID;
  837. + override_cred->fsgid = GLOBAL_ROOT_GID;
  838. + old_cred = override_creds(override_cred);
  839. +
  840. + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
  841. + dentry->d_name.len);
  842. + err = PTR_ERR(newdentry);
  843. + if (IS_ERR(newdentry))
  844. + goto out_put_cred;
  845. +
  846. + /* Just been removed within the same locked region */
  847. + WARN_ON(newdentry->d_inode);
  848. +
  849. + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
  850. + if (err)
  851. + goto out_dput;
  852. +
  853. + ovl_dentry_version_inc(dentry->d_parent);
  854. +
  855. + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
  856. + if (err)
  857. + vfs_unlink(upperdir->d_inode, newdentry);
  858. +
  859. +out_dput:
  860. + dput(newdentry);
  861. +out_put_cred:
  862. + revert_creds(old_cred);
  863. + put_cred(override_cred);
  864. +out:
  865. + if (err) {
  866. + /*
  867. + * There's no way to recover from failure to whiteout.
  868. + * What should we do? Log a big fat error and... ?
  869. + */
  870. + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
  871. + dentry->d_name.name);
  872. + }
  873. +
  874. + return err;
  875. +}
  876. +
  877. +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
  878. + struct dentry *template)
  879. +{
  880. + int err;
  881. + struct dentry *newdentry;
  882. + struct qstr *name = &template->d_name;
  883. +
  884. + newdentry = lookup_one_len(name->name, upperdir, name->len);
  885. + if (IS_ERR(newdentry))
  886. + return newdentry;
  887. +
  888. + if (newdentry->d_inode) {
  889. + const struct cred *old_cred;
  890. + struct cred *override_cred;
  891. +
  892. + /* No need to check whiteout if lower parent is non-existent */
  893. + err = -EEXIST;
  894. + if (!ovl_dentry_lower(template->d_parent))
  895. + goto out_dput;
  896. +
  897. + if (!S_ISLNK(newdentry->d_inode->i_mode))
  898. + goto out_dput;
  899. +
  900. + err = -ENOMEM;
  901. + override_cred = prepare_creds();
  902. + if (!override_cred)
  903. + goto out_dput;
  904. +
  905. + /*
  906. + * CAP_SYS_ADMIN for getxattr
  907. + * CAP_FOWNER for unlink in sticky directory
  908. + */
  909. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  910. + cap_raise(override_cred->cap_effective, CAP_FOWNER);
  911. + old_cred = override_creds(override_cred);
  912. +
  913. + err = -EEXIST;
  914. + if (ovl_is_whiteout(newdentry))
  915. + err = vfs_unlink(upperdir->d_inode, newdentry);
  916. +
  917. + revert_creds(old_cred);
  918. + put_cred(override_cred);
  919. + if (err)
  920. + goto out_dput;
  921. +
  922. + dput(newdentry);
  923. + newdentry = lookup_one_len(name->name, upperdir, name->len);
  924. + if (IS_ERR(newdentry)) {
  925. + ovl_whiteout(upperdir, template);
  926. + return newdentry;
  927. + }
  928. +
  929. + /*
  930. + * Whiteout just been successfully removed, parent
  931. + * i_mutex is still held, there's no way the lookup
  932. + * could return positive.
  933. + */
  934. + WARN_ON(newdentry->d_inode);
  935. + }
  936. +
  937. + return newdentry;
  938. +
  939. +out_dput:
  940. + dput(newdentry);
  941. + return ERR_PTR(err);
  942. +}
  943. +
  944. +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
  945. + struct kstat *stat, const char *link)
  946. +{
  947. + int err;
  948. + struct dentry *newdentry;
  949. + struct inode *dir = upperdir->d_inode;
  950. +
  951. + newdentry = ovl_lookup_create(upperdir, dentry);
  952. + if (IS_ERR(newdentry))
  953. + goto out;
  954. +
  955. + switch (stat->mode & S_IFMT) {
  956. + case S_IFREG:
  957. + err = vfs_create(dir, newdentry, stat->mode, NULL);
  958. + break;
  959. +
  960. + case S_IFDIR:
  961. + err = vfs_mkdir(dir, newdentry, stat->mode);
  962. + break;
  963. +
  964. + case S_IFCHR:
  965. + case S_IFBLK:
  966. + case S_IFIFO:
  967. + case S_IFSOCK:
  968. + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
  969. + break;
  970. +
  971. + case S_IFLNK:
  972. + err = vfs_symlink(dir, newdentry, link);
  973. + break;
  974. +
  975. + default:
  976. + err = -EPERM;
  977. + }
  978. + if (err) {
  979. + if (ovl_dentry_is_opaque(dentry))
  980. + ovl_whiteout(upperdir, dentry);
  981. + dput(newdentry);
  982. + newdentry = ERR_PTR(err);
  983. + } else if (WARN_ON(!newdentry->d_inode)) {
  984. + /*
  985. + * Not quite sure if non-instantiated dentry is legal or not.
  986. + * VFS doesn't seem to care so check and warn here.
  987. + */
  988. + dput(newdentry);
  989. + newdentry = ERR_PTR(-ENOENT);
  990. + }
  991. +
  992. +out:
  993. + return newdentry;
  994. +
  995. +}
  996. +
  997. +static int ovl_set_opaque(struct dentry *upperdentry)
  998. +{
  999. + int err;
  1000. + const struct cred *old_cred;
  1001. + struct cred *override_cred;
  1002. +
  1003. + override_cred = prepare_creds();
  1004. + if (!override_cred)
  1005. + return -ENOMEM;
  1006. +
  1007. + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
  1008. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  1009. + old_cred = override_creds(override_cred);
  1010. + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
  1011. + revert_creds(old_cred);
  1012. + put_cred(override_cred);
  1013. +
  1014. + return err;
  1015. +}
  1016. +
  1017. +static int ovl_remove_opaque(struct dentry *upperdentry)
  1018. +{
  1019. + int err;
  1020. + const struct cred *old_cred;
  1021. + struct cred *override_cred;
  1022. +
  1023. + override_cred = prepare_creds();
  1024. + if (!override_cred)
  1025. + return -ENOMEM;
  1026. +
  1027. + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
  1028. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  1029. + old_cred = override_creds(override_cred);
  1030. + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
  1031. + revert_creds(old_cred);
  1032. + put_cred(override_cred);
  1033. +
  1034. + return err;
  1035. +}
  1036. +
  1037. +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1038. + struct kstat *stat)
  1039. +{
  1040. + int err;
  1041. + enum ovl_path_type type;
  1042. + struct path realpath;
  1043. +
  1044. + type = ovl_path_real(dentry, &realpath);
  1045. + err = vfs_getattr(&realpath, stat);
  1046. + if (err)
  1047. + return err;
  1048. +
  1049. + stat->dev = dentry->d_sb->s_dev;
  1050. + stat->ino = dentry->d_inode->i_ino;
  1051. +
  1052. + /*
  1053. + * It's probably not worth it to count subdirs to get the
  1054. + * correct link count. nlink=1 seems to pacify 'find' and
  1055. + * other utilities.
  1056. + */
  1057. + if (type == OVL_PATH_MERGE)
  1058. + stat->nlink = 1;
  1059. +
  1060. + return 0;
  1061. +}
  1062. +
  1063. +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
  1064. + const char *link)
  1065. +{
  1066. + int err;
  1067. + struct dentry *newdentry;
  1068. + struct dentry *upperdir;
  1069. + struct inode *inode;
  1070. + struct kstat stat = {
  1071. + .mode = mode,
  1072. + .rdev = rdev,
  1073. + };
  1074. +
  1075. + err = -ENOMEM;
  1076. + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
  1077. + if (!inode)
  1078. + goto out;
  1079. +
  1080. + err = ovl_copy_up(dentry->d_parent);
  1081. + if (err)
  1082. + goto out_iput;
  1083. +
  1084. + upperdir = ovl_dentry_upper(dentry->d_parent);
  1085. + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
  1086. +
  1087. + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
  1088. + err = PTR_ERR(newdentry);
  1089. + if (IS_ERR(newdentry))
  1090. + goto out_unlock;
  1091. +
  1092. + ovl_dentry_version_inc(dentry->d_parent);
  1093. + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
  1094. + err = ovl_set_opaque(newdentry);
  1095. + if (err) {
  1096. + vfs_rmdir(upperdir->d_inode, newdentry);
  1097. + ovl_whiteout(upperdir, dentry);
  1098. + goto out_dput;
  1099. + }
  1100. + }
  1101. + ovl_dentry_update(dentry, newdentry);
  1102. + ovl_copyattr(newdentry->d_inode, inode);
  1103. + d_instantiate(dentry, inode);
  1104. + inode = NULL;
  1105. + newdentry = NULL;
  1106. + err = 0;
  1107. +
  1108. +out_dput:
  1109. + dput(newdentry);
  1110. +out_unlock:
  1111. + mutex_unlock(&upperdir->d_inode->i_mutex);
  1112. +out_iput:
  1113. + iput(inode);
  1114. +out:
  1115. + return err;
  1116. +}
  1117. +
  1118. +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1119. + bool excl)
  1120. +{
  1121. + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
  1122. +}
  1123. +
  1124. +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1125. +{
  1126. + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
  1127. +}
  1128. +
  1129. +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  1130. + dev_t rdev)
  1131. +{
  1132. + return ovl_create_object(dentry, mode, rdev, NULL);
  1133. +}
  1134. +
  1135. +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
  1136. + const char *link)
  1137. +{
  1138. + return ovl_create_object(dentry, S_IFLNK, 0, link);
  1139. +}
  1140. +
  1141. +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
  1142. +{
  1143. + int err;
  1144. + enum ovl_path_type type;
  1145. + struct path realpath;
  1146. + struct dentry *upperdir;
  1147. +
  1148. + err = ovl_copy_up(dentry->d_parent);
  1149. + if (err)
  1150. + return err;
  1151. +
  1152. + upperdir = ovl_dentry_upper(dentry->d_parent);
  1153. + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
  1154. + type = ovl_path_real(dentry, &realpath);
  1155. + if (type != OVL_PATH_LOWER) {
  1156. + err = -ESTALE;
  1157. + if (realpath.dentry->d_parent != upperdir)
  1158. + goto out_d_drop;
  1159. +
  1160. + /* FIXME: create whiteout up front and rename to target */
  1161. +
  1162. + if (is_dir)
  1163. + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
  1164. + else
  1165. + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
  1166. + if (err)
  1167. + goto out_d_drop;
  1168. +
  1169. + ovl_dentry_version_inc(dentry->d_parent);
  1170. + }
  1171. +
  1172. + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
  1173. + err = ovl_whiteout(upperdir, dentry);
  1174. +
  1175. + /*
  1176. + * Keeping this dentry hashed would mean having to release
  1177. + * upperpath/lowerpath, which could only be done if we are the
  1178. + * sole user of this dentry. Too tricky... Just unhash for
  1179. + * now.
  1180. + */
  1181. +out_d_drop:
  1182. + d_drop(dentry);
  1183. + mutex_unlock(&upperdir->d_inode->i_mutex);
  1184. +
  1185. + return err;
  1186. +}
  1187. +
  1188. +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
  1189. +{
  1190. + return ovl_do_remove(dentry, false);
  1191. +}
  1192. +
  1193. +
  1194. +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
  1195. +{
  1196. + int err;
  1197. + enum ovl_path_type type;
  1198. +
  1199. + type = ovl_path_type(dentry);
  1200. + if (type != OVL_PATH_UPPER) {
  1201. + err = ovl_check_empty_and_clear(dentry, type);
  1202. + if (err)
  1203. + return err;
  1204. + }
  1205. +
  1206. + return ovl_do_remove(dentry, true);
  1207. +}
  1208. +
  1209. +static int ovl_link(struct dentry *old, struct inode *newdir,
  1210. + struct dentry *new)
  1211. +{
  1212. + int err;
  1213. + struct dentry *olddentry;
  1214. + struct dentry *newdentry;
  1215. + struct dentry *upperdir;
  1216. + struct inode *newinode;
  1217. +
  1218. + err = ovl_copy_up(old);
  1219. + if (err)
  1220. + goto out;
  1221. +
  1222. + err = ovl_copy_up(new->d_parent);
  1223. + if (err)
  1224. + goto out;
  1225. +
  1226. + upperdir = ovl_dentry_upper(new->d_parent);
  1227. + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
  1228. + newdentry = ovl_lookup_create(upperdir, new);
  1229. + err = PTR_ERR(newdentry);
  1230. + if (IS_ERR(newdentry))
  1231. + goto out_unlock;
  1232. +
  1233. + olddentry = ovl_dentry_upper(old);
  1234. + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
  1235. + if (!err) {
  1236. + if (WARN_ON(!newdentry->d_inode)) {
  1237. + dput(newdentry);
  1238. + err = -ENOENT;
  1239. + goto out_unlock;
  1240. + }
  1241. + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
  1242. + new->d_fsdata);
  1243. + if (!newinode)
  1244. + goto link_fail;
  1245. + ovl_copyattr(upperdir->d_inode, newinode);
  1246. +
  1247. + ovl_dentry_version_inc(new->d_parent);
  1248. + ovl_dentry_update(new, newdentry);
  1249. +
  1250. + d_instantiate(new, newinode);
  1251. + } else {
  1252. +link_fail:
  1253. + if (ovl_dentry_is_opaque(new))
  1254. + ovl_whiteout(upperdir, new);
  1255. + dput(newdentry);
  1256. + }
  1257. +out_unlock:
  1258. + mutex_unlock(&upperdir->d_inode->i_mutex);
  1259. +out:
  1260. + return err;
  1261. +
  1262. +}
  1263. +
  1264. +static int ovl_rename(struct inode *olddir, struct dentry *old,
  1265. + struct inode *newdir, struct dentry *new)
  1266. +{
  1267. + int err;
  1268. + enum ovl_path_type old_type;
  1269. + enum ovl_path_type new_type;
  1270. + struct dentry *old_upperdir;
  1271. + struct dentry *new_upperdir;
  1272. + struct dentry *olddentry;
  1273. + struct dentry *newdentry;
  1274. + struct dentry *trap;
  1275. + bool old_opaque;
  1276. + bool new_opaque;
  1277. + bool new_create = false;
  1278. + bool is_dir = S_ISDIR(old->d_inode->i_mode);
  1279. +
  1280. + /* Don't copy up directory trees */
  1281. + old_type = ovl_path_type(old);
  1282. + if (old_type != OVL_PATH_UPPER && is_dir)
  1283. + return -EXDEV;
  1284. +
  1285. + if (new->d_inode) {
  1286. + new_type = ovl_path_type(new);
  1287. +
  1288. + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
  1289. + if (ovl_dentry_lower(old)->d_inode ==
  1290. + ovl_dentry_lower(new)->d_inode)
  1291. + return 0;
  1292. + }
  1293. + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
  1294. + if (ovl_dentry_upper(old)->d_inode ==
  1295. + ovl_dentry_upper(new)->d_inode)
  1296. + return 0;
  1297. + }
  1298. +
  1299. + if (new_type != OVL_PATH_UPPER &&
  1300. + S_ISDIR(new->d_inode->i_mode)) {
  1301. + err = ovl_check_empty_and_clear(new, new_type);
  1302. + if (err)
  1303. + return err;
  1304. + }
  1305. + } else {
  1306. + new_type = OVL_PATH_UPPER;
  1307. + }
  1308. +
  1309. + err = ovl_copy_up(old);
  1310. + if (err)
  1311. + return err;
  1312. +
  1313. + err = ovl_copy_up(new->d_parent);
  1314. + if (err)
  1315. + return err;
  1316. +
  1317. + old_upperdir = ovl_dentry_upper(old->d_parent);
  1318. + new_upperdir = ovl_dentry_upper(new->d_parent);
  1319. +
  1320. + trap = lock_rename(new_upperdir, old_upperdir);
  1321. +
  1322. + olddentry = ovl_dentry_upper(old);
  1323. + newdentry = ovl_dentry_upper(new);
  1324. + if (newdentry) {
  1325. + dget(newdentry);
  1326. + } else {
  1327. + new_create = true;
  1328. + newdentry = ovl_lookup_create(new_upperdir, new);
  1329. + err = PTR_ERR(newdentry);
  1330. + if (IS_ERR(newdentry))
  1331. + goto out_unlock;
  1332. + }
  1333. +
  1334. + err = -ESTALE;
  1335. + if (olddentry->d_parent != old_upperdir)
  1336. + goto out_dput;
  1337. + if (newdentry->d_parent != new_upperdir)
  1338. + goto out_dput;
  1339. + if (olddentry == trap)
  1340. + goto out_dput;
  1341. + if (newdentry == trap)
  1342. + goto out_dput;
  1343. +
  1344. + old_opaque = ovl_dentry_is_opaque(old);
  1345. + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
  1346. +
  1347. + if (is_dir && !old_opaque && new_opaque) {
  1348. + err = ovl_set_opaque(olddentry);
  1349. + if (err)
  1350. + goto out_dput;
  1351. + }
  1352. +
  1353. + err = vfs_rename(old_upperdir->d_inode, olddentry,
  1354. + new_upperdir->d_inode, newdentry);
  1355. +
  1356. + if (err) {
  1357. + if (new_create && ovl_dentry_is_opaque(new))
  1358. + ovl_whiteout(new_upperdir, new);
  1359. + if (is_dir && !old_opaque && new_opaque)
  1360. + ovl_remove_opaque(olddentry);
  1361. + goto out_dput;
  1362. + }
  1363. +
  1364. + if (old_type != OVL_PATH_UPPER || old_opaque)
  1365. + err = ovl_whiteout(old_upperdir, old);
  1366. + if (is_dir && old_opaque && !new_opaque)
  1367. + ovl_remove_opaque(olddentry);
  1368. +
  1369. + if (old_opaque != new_opaque)
  1370. + ovl_dentry_set_opaque(old, new_opaque);
  1371. +
  1372. + ovl_dentry_version_inc(old->d_parent);
  1373. + ovl_dentry_version_inc(new->d_parent);
  1374. +
  1375. +out_dput:
  1376. + dput(newdentry);
  1377. +out_unlock:
  1378. + unlock_rename(new_upperdir, old_upperdir);
  1379. + return err;
  1380. +}
  1381. +
  1382. +const struct inode_operations ovl_dir_inode_operations = {
  1383. + .lookup = ovl_lookup,
  1384. + .mkdir = ovl_mkdir,
  1385. + .symlink = ovl_symlink,
  1386. + .unlink = ovl_unlink,
  1387. + .rmdir = ovl_rmdir,
  1388. + .rename = ovl_rename,
  1389. + .link = ovl_link,
  1390. + .setattr = ovl_setattr,
  1391. + .create = ovl_create,
  1392. + .mknod = ovl_mknod,
  1393. + .permission = ovl_permission,
  1394. + .getattr = ovl_dir_getattr,
  1395. + .setxattr = ovl_setxattr,
  1396. + .getxattr = ovl_getxattr,
  1397. + .listxattr = ovl_listxattr,
  1398. + .removexattr = ovl_removexattr,
  1399. +};
  1400. --- /dev/null
  1401. +++ b/fs/overlayfs/inode.c
  1402. @@ -0,0 +1,372 @@
  1403. +/*
  1404. + *
  1405. + * Copyright (C) 2011 Novell Inc.
  1406. + *
  1407. + * This program is free software; you can redistribute it and/or modify it
  1408. + * under the terms of the GNU General Public License version 2 as published by
  1409. + * the Free Software Foundation.
  1410. + */
  1411. +
  1412. +#include <linux/fs.h>
  1413. +#include <linux/slab.h>
  1414. +#include <linux/xattr.h>
  1415. +#include "overlayfs.h"
  1416. +
  1417. +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  1418. +{
  1419. + struct dentry *upperdentry;
  1420. + int err;
  1421. +
  1422. + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
  1423. + err = ovl_copy_up_truncate(dentry, attr->ia_size);
  1424. + else
  1425. + err = ovl_copy_up(dentry);
  1426. + if (err)
  1427. + return err;
  1428. +
  1429. + upperdentry = ovl_dentry_upper(dentry);
  1430. +
  1431. + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1432. + attr->ia_valid &= ~ATTR_MODE;
  1433. +
  1434. + mutex_lock(&upperdentry->d_inode->i_mutex);
  1435. + err = notify_change(upperdentry, attr);
  1436. + if (!err)
  1437. + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
  1438. + mutex_unlock(&upperdentry->d_inode->i_mutex);
  1439. +
  1440. + return err;
  1441. +}
  1442. +
  1443. +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1444. + struct kstat *stat)
  1445. +{
  1446. + struct path realpath;
  1447. +
  1448. + ovl_path_real(dentry, &realpath);
  1449. + return vfs_getattr(&realpath, stat);
  1450. +}
  1451. +
  1452. +int ovl_permission(struct inode *inode, int mask)
  1453. +{
  1454. + struct ovl_entry *oe;
  1455. + struct dentry *alias = NULL;
  1456. + struct inode *realinode;
  1457. + struct dentry *realdentry;
  1458. + bool is_upper;
  1459. + int err;
  1460. +
  1461. + if (S_ISDIR(inode->i_mode)) {
  1462. + oe = inode->i_private;
  1463. + } else if (mask & MAY_NOT_BLOCK) {
  1464. + return -ECHILD;
  1465. + } else {
  1466. + /*
  1467. + * For non-directories find an alias and get the info
  1468. + * from there.
  1469. + */
  1470. + alias = d_find_any_alias(inode);
  1471. + if (WARN_ON(!alias))
  1472. + return -ENOENT;
  1473. +
  1474. + oe = alias->d_fsdata;
  1475. + }
  1476. +
  1477. + realdentry = ovl_entry_real(oe, &is_upper);
  1478. +
  1479. + /* Careful in RCU walk mode */
  1480. + realinode = ACCESS_ONCE(realdentry->d_inode);
  1481. + if (!realinode) {
  1482. + WARN_ON(!(mask & MAY_NOT_BLOCK));
  1483. + err = -ENOENT;
  1484. + goto out_dput;
  1485. + }
  1486. +
  1487. + if (mask & MAY_WRITE) {
  1488. + umode_t mode = realinode->i_mode;
  1489. +
  1490. + /*
  1491. + * Writes will always be redirected to upper layer, so
  1492. + * ignore lower layer being read-only.
  1493. + *
  1494. + * If the overlay itself is read-only then proceed
  1495. + * with the permission check, don't return EROFS.
  1496. + * This will only happen if this is the lower layer of
  1497. + * another overlayfs.
  1498. + *
  1499. + * If upper fs becomes read-only after the overlay was
  1500. + * constructed return EROFS to prevent modification of
  1501. + * upper layer.
  1502. + */
  1503. + err = -EROFS;
  1504. + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
  1505. + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  1506. + goto out_dput;
  1507. + }
  1508. +
  1509. + err = __inode_permission(realinode, mask);
  1510. +out_dput:
  1511. + dput(alias);
  1512. + return err;
  1513. +}
  1514. +
  1515. +
  1516. +struct ovl_link_data {
  1517. + struct dentry *realdentry;
  1518. + void *cookie;
  1519. +};
  1520. +
  1521. +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
  1522. +{
  1523. + void *ret;
  1524. + struct dentry *realdentry;
  1525. + struct inode *realinode;
  1526. +
  1527. + realdentry = ovl_dentry_real(dentry);
  1528. + realinode = realdentry->d_inode;
  1529. +
  1530. + if (WARN_ON(!realinode->i_op->follow_link))
  1531. + return ERR_PTR(-EPERM);
  1532. +
  1533. + ret = realinode->i_op->follow_link(realdentry, nd);
  1534. + if (IS_ERR(ret))
  1535. + return ret;
  1536. +
  1537. + if (realinode->i_op->put_link) {
  1538. + struct ovl_link_data *data;
  1539. +
  1540. + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
  1541. + if (!data) {
  1542. + realinode->i_op->put_link(realdentry, nd, ret);
  1543. + return ERR_PTR(-ENOMEM);
  1544. + }
  1545. + data->realdentry = realdentry;
  1546. + data->cookie = ret;
  1547. +
  1548. + return data;
  1549. + } else {
  1550. + return NULL;
  1551. + }
  1552. +}
  1553. +
  1554. +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
  1555. +{
  1556. + struct inode *realinode;
  1557. + struct ovl_link_data *data = c;
  1558. +
  1559. + if (!data)
  1560. + return;
  1561. +
  1562. + realinode = data->realdentry->d_inode;
  1563. + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
  1564. + kfree(data);
  1565. +}
  1566. +
  1567. +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  1568. +{
  1569. + struct path realpath;
  1570. + struct inode *realinode;
  1571. +
  1572. + ovl_path_real(dentry, &realpath);
  1573. + realinode = realpath.dentry->d_inode;
  1574. +
  1575. + if (!realinode->i_op->readlink)
  1576. + return -EINVAL;
  1577. +
  1578. + touch_atime(&realpath);
  1579. +
  1580. + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
  1581. +}
  1582. +
  1583. +
  1584. +static bool ovl_is_private_xattr(const char *name)
  1585. +{
  1586. + return strncmp(name, "trusted.overlay.", 14) == 0;
  1587. +}
  1588. +
  1589. +int ovl_setxattr(struct dentry *dentry, const char *name,
  1590. + const void *value, size_t size, int flags)
  1591. +{
  1592. + int err;
  1593. + struct dentry *upperdentry;
  1594. +
  1595. + if (ovl_is_private_xattr(name))
  1596. + return -EPERM;
  1597. +
  1598. + err = ovl_copy_up(dentry);
  1599. + if (err)
  1600. + return err;
  1601. +
  1602. + upperdentry = ovl_dentry_upper(dentry);
  1603. + return vfs_setxattr(upperdentry, name, value, size, flags);
  1604. +}
  1605. +
  1606. +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
  1607. + void *value, size_t size)
  1608. +{
  1609. + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
  1610. + ovl_is_private_xattr(name))
  1611. + return -ENODATA;
  1612. +
  1613. + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
  1614. +}
  1615. +
  1616. +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  1617. +{
  1618. + ssize_t res;
  1619. + int off;
  1620. +
  1621. + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
  1622. + if (res <= 0 || size == 0)
  1623. + return res;
  1624. +
  1625. + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
  1626. + return res;
  1627. +
  1628. + /* filter out private xattrs */
  1629. + for (off = 0; off < res;) {
  1630. + char *s = list + off;
  1631. + size_t slen = strlen(s) + 1;
  1632. +
  1633. + BUG_ON(off + slen > res);
  1634. +
  1635. + if (ovl_is_private_xattr(s)) {
  1636. + res -= slen;
  1637. + memmove(s, s + slen, res - off);
  1638. + } else {
  1639. + off += slen;
  1640. + }
  1641. + }
  1642. +
  1643. + return res;
  1644. +}
  1645. +
  1646. +int ovl_removexattr(struct dentry *dentry, const char *name)
  1647. +{
  1648. + int err;
  1649. + struct path realpath;
  1650. + enum ovl_path_type type;
  1651. +
  1652. + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
  1653. + ovl_is_private_xattr(name))
  1654. + return -ENODATA;
  1655. +
  1656. + type = ovl_path_real(dentry, &realpath);
  1657. + if (type == OVL_PATH_LOWER) {
  1658. + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  1659. + if (err < 0)
  1660. + return err;
  1661. +
  1662. + err = ovl_copy_up(dentry);
  1663. + if (err)
  1664. + return err;
  1665. +
  1666. + ovl_path_upper(dentry, &realpath);
  1667. + }
  1668. +
  1669. + return vfs_removexattr(realpath.dentry, name);
  1670. +}
  1671. +
  1672. +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
  1673. + struct dentry *realdentry)
  1674. +{
  1675. + if (type != OVL_PATH_LOWER)
  1676. + return false;
  1677. +
  1678. + if (special_file(realdentry->d_inode->i_mode))
  1679. + return false;
  1680. +
  1681. + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  1682. + return false;
  1683. +
  1684. + return true;
  1685. +}
  1686. +
  1687. +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
  1688. + const struct cred *cred)
  1689. +{
  1690. + int err;
  1691. + struct path realpath;
  1692. + enum ovl_path_type type;
  1693. +
  1694. + type = ovl_path_real(dentry, &realpath);
  1695. + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
  1696. + if (file->f_flags & O_TRUNC)
  1697. + err = ovl_copy_up_truncate(dentry, 0);
  1698. + else
  1699. + err = ovl_copy_up(dentry);
  1700. + if (err)
  1701. + return err;
  1702. +
  1703. + ovl_path_upper(dentry, &realpath);
  1704. + }
  1705. +
  1706. + return vfs_open(&realpath, file, cred);
  1707. +}
  1708. +
  1709. +static const struct inode_operations ovl_file_inode_operations = {
  1710. + .setattr = ovl_setattr,
  1711. + .permission = ovl_permission,
  1712. + .getattr = ovl_getattr,
  1713. + .setxattr = ovl_setxattr,
  1714. + .getxattr = ovl_getxattr,
  1715. + .listxattr = ovl_listxattr,
  1716. + .removexattr = ovl_removexattr,
  1717. + .dentry_open = ovl_dentry_open,
  1718. +};
  1719. +
  1720. +static const struct inode_operations ovl_symlink_inode_operations = {
  1721. + .setattr = ovl_setattr,
  1722. + .follow_link = ovl_follow_link,
  1723. + .put_link = ovl_put_link,
  1724. + .readlink = ovl_readlink,
  1725. + .getattr = ovl_getattr,
  1726. + .setxattr = ovl_setxattr,
  1727. + .getxattr = ovl_getxattr,
  1728. + .listxattr = ovl_listxattr,
  1729. + .removexattr = ovl_removexattr,
  1730. +};
  1731. +
  1732. +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  1733. + struct ovl_entry *oe)
  1734. +{
  1735. + struct inode *inode;
  1736. +
  1737. + inode = new_inode(sb);
  1738. + if (!inode)
  1739. + return NULL;
  1740. +
  1741. + mode &= S_IFMT;
  1742. +
  1743. + inode->i_ino = get_next_ino();
  1744. + inode->i_mode = mode;
  1745. + inode->i_flags |= S_NOATIME | S_NOCMTIME;
  1746. +
  1747. + switch (mode) {
  1748. + case S_IFDIR:
  1749. + inode->i_private = oe;
  1750. + inode->i_op = &ovl_dir_inode_operations;
  1751. + inode->i_fop = &ovl_dir_operations;
  1752. + break;
  1753. +
  1754. + case S_IFLNK:
  1755. + inode->i_op = &ovl_symlink_inode_operations;
  1756. + break;
  1757. +
  1758. + case S_IFREG:
  1759. + case S_IFSOCK:
  1760. + case S_IFBLK:
  1761. + case S_IFCHR:
  1762. + case S_IFIFO:
  1763. + inode->i_op = &ovl_file_inode_operations;
  1764. + break;
  1765. +
  1766. + default:
  1767. + WARN(1, "illegal file type: %i\n", mode);
  1768. + iput(inode);
  1769. + inode = NULL;
  1770. + }
  1771. +
  1772. + return inode;
  1773. +
  1774. +}
  1775. --- /dev/null
  1776. +++ b/fs/overlayfs/overlayfs.h
  1777. @@ -0,0 +1,70 @@
  1778. +/*
  1779. + *
  1780. + * Copyright (C) 2011 Novell Inc.
  1781. + *
  1782. + * This program is free software; you can redistribute it and/or modify it
  1783. + * under the terms of the GNU General Public License version 2 as published by
  1784. + * the Free Software Foundation.
  1785. + */
  1786. +
  1787. +struct ovl_entry;
  1788. +
  1789. +enum ovl_path_type {
  1790. + OVL_PATH_UPPER,
  1791. + OVL_PATH_MERGE,
  1792. + OVL_PATH_LOWER,
  1793. +};
  1794. +
  1795. +extern const char *ovl_opaque_xattr;
  1796. +extern const char *ovl_whiteout_xattr;
  1797. +extern const struct dentry_operations ovl_dentry_operations;
  1798. +
  1799. +enum ovl_path_type ovl_path_type(struct dentry *dentry);
  1800. +u64 ovl_dentry_version_get(struct dentry *dentry);
  1801. +void ovl_dentry_version_inc(struct dentry *dentry);
  1802. +void ovl_path_upper(struct dentry *dentry, struct path *path);
  1803. +void ovl_path_lower(struct dentry *dentry, struct path *path);
  1804. +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
  1805. +struct dentry *ovl_dentry_upper(struct dentry *dentry);
  1806. +struct dentry *ovl_dentry_lower(struct dentry *dentry);
  1807. +struct dentry *ovl_dentry_real(struct dentry *dentry);
  1808. +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
  1809. +bool ovl_dentry_is_opaque(struct dentry *dentry);
  1810. +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
  1811. +bool ovl_is_whiteout(struct dentry *dentry);
  1812. +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
  1813. +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  1814. + unsigned int flags);
  1815. +struct file *ovl_path_open(struct path *path, int flags);
  1816. +
  1817. +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
  1818. + struct kstat *stat, const char *link);
  1819. +
  1820. +/* readdir.c */
  1821. +extern const struct file_operations ovl_dir_operations;
  1822. +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
  1823. +
  1824. +/* inode.c */
  1825. +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
  1826. +int ovl_permission(struct inode *inode, int mask);
  1827. +int ovl_setxattr(struct dentry *dentry, const char *name,
  1828. + const void *value, size_t size, int flags);
  1829. +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
  1830. + void *value, size_t size);
  1831. +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
  1832. +int ovl_removexattr(struct dentry *dentry, const char *name);
  1833. +
  1834. +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
  1835. + struct ovl_entry *oe);
  1836. +static inline void ovl_copyattr(struct inode *from, struct inode *to)
  1837. +{
  1838. + to->i_uid = from->i_uid;
  1839. + to->i_gid = from->i_gid;
  1840. +}
  1841. +
  1842. +/* dir.c */
  1843. +extern const struct inode_operations ovl_dir_inode_operations;
  1844. +
  1845. +/* copy_up.c */
  1846. +int ovl_copy_up(struct dentry *dentry);
  1847. +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
  1848. --- /dev/null
  1849. +++ b/fs/overlayfs/readdir.c
  1850. @@ -0,0 +1,566 @@
  1851. +/*
  1852. + *
  1853. + * Copyright (C) 2011 Novell Inc.
  1854. + *
  1855. + * This program is free software; you can redistribute it and/or modify it
  1856. + * under the terms of the GNU General Public License version 2 as published by
  1857. + * the Free Software Foundation.
  1858. + */
  1859. +
  1860. +#include <linux/fs.h>
  1861. +#include <linux/slab.h>
  1862. +#include <linux/namei.h>
  1863. +#include <linux/file.h>
  1864. +#include <linux/xattr.h>
  1865. +#include <linux/rbtree.h>
  1866. +#include <linux/security.h>
  1867. +#include <linux/cred.h>
  1868. +#include "overlayfs.h"
  1869. +
  1870. +struct ovl_cache_entry {
  1871. + const char *name;
  1872. + unsigned int len;
  1873. + unsigned int type;
  1874. + u64 ino;
  1875. + bool is_whiteout;
  1876. + struct list_head l_node;
  1877. + struct rb_node node;
  1878. +};
  1879. +
  1880. +struct ovl_readdir_data {
  1881. + struct rb_root *root;
  1882. + struct list_head *list;
  1883. + struct list_head *middle;
  1884. + struct dentry *dir;
  1885. + int count;
  1886. + int err;
  1887. +};
  1888. +
  1889. +struct ovl_dir_file {
  1890. + bool is_real;
  1891. + bool is_cached;
  1892. + struct list_head cursor;
  1893. + u64 cache_version;
  1894. + struct list_head cache;
  1895. + struct file *realfile;
  1896. +};
  1897. +
  1898. +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
  1899. +{
  1900. + return container_of(n, struct ovl_cache_entry, node);
  1901. +}
  1902. +
  1903. +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
  1904. + const char *name, int len)
  1905. +{
  1906. + struct rb_node *node = root->rb_node;
  1907. + int cmp;
  1908. +
  1909. + while (node) {
  1910. + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
  1911. +
  1912. + cmp = strncmp(name, p->name, len);
  1913. + if (cmp > 0)
  1914. + node = p->node.rb_right;
  1915. + else if (cmp < 0 || len < p->len)
  1916. + node = p->node.rb_left;
  1917. + else
  1918. + return p;
  1919. + }
  1920. +
  1921. + return NULL;
  1922. +}
  1923. +
  1924. +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
  1925. + u64 ino, unsigned int d_type)
  1926. +{
  1927. + struct ovl_cache_entry *p;
  1928. +
  1929. + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
  1930. + if (p) {
  1931. + char *name_copy = (char *) (p + 1);
  1932. + memcpy(name_copy, name, len);
  1933. + name_copy[len] = '\0';
  1934. + p->name = name_copy;
  1935. + p->len = len;
  1936. + p->type = d_type;
  1937. + p->ino = ino;
  1938. + p->is_whiteout = false;
  1939. + }
  1940. +
  1941. + return p;
  1942. +}
  1943. +
  1944. +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
  1945. + const char *name, int len, u64 ino,
  1946. + unsigned int d_type)
  1947. +{
  1948. + struct rb_node **newp = &rdd->root->rb_node;
  1949. + struct rb_node *parent = NULL;
  1950. + struct ovl_cache_entry *p;
  1951. +
  1952. + while (*newp) {
  1953. + int cmp;
  1954. + struct ovl_cache_entry *tmp;
  1955. +
  1956. + parent = *newp;
  1957. + tmp = ovl_cache_entry_from_node(*newp);
  1958. + cmp = strncmp(name, tmp->name, len);
  1959. + if (cmp > 0)
  1960. + newp = &tmp->node.rb_right;
  1961. + else if (cmp < 0 || len < tmp->len)
  1962. + newp = &tmp->node.rb_left;
  1963. + else
  1964. + return 0;
  1965. + }
  1966. +
  1967. + p = ovl_cache_entry_new(name, len, ino, d_type);
  1968. + if (p == NULL)
  1969. + return -ENOMEM;
  1970. +
  1971. + list_add_tail(&p->l_node, rdd->list);
  1972. + rb_link_node(&p->node, parent, newp);
  1973. + rb_insert_color(&p->node, rdd->root);
  1974. +
  1975. + return 0;
  1976. +}
  1977. +
  1978. +static int ovl_fill_lower(void *buf, const char *name, int namelen,
  1979. + loff_t offset, u64 ino, unsigned int d_type)
  1980. +{
  1981. + struct ovl_readdir_data *rdd = buf;
  1982. + struct ovl_cache_entry *p;
  1983. +
  1984. + rdd->count++;
  1985. + p = ovl_cache_entry_find(rdd->root, name, namelen);
  1986. + if (p) {
  1987. + list_move_tail(&p->l_node, rdd->middle);
  1988. + } else {
  1989. + p = ovl_cache_entry_new(name, namelen, ino, d_type);
  1990. + if (p == NULL)
  1991. + rdd->err = -ENOMEM;
  1992. + else
  1993. + list_add_tail(&p->l_node, rdd->middle);
  1994. + }
  1995. +
  1996. + return rdd->err;
  1997. +}
  1998. +
  1999. +static void ovl_cache_free(struct list_head *list)
  2000. +{
  2001. + struct ovl_cache_entry *p;
  2002. + struct ovl_cache_entry *n;
  2003. +
  2004. + list_for_each_entry_safe(p, n, list, l_node)
  2005. + kfree(p);
  2006. +
  2007. + INIT_LIST_HEAD(list);
  2008. +}
  2009. +
  2010. +static int ovl_fill_upper(void *buf, const char *name, int namelen,
  2011. + loff_t offset, u64 ino, unsigned int d_type)
  2012. +{
  2013. + struct ovl_readdir_data *rdd = buf;
  2014. +
  2015. + rdd->count++;
  2016. + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
  2017. +}
  2018. +
  2019. +static inline int ovl_dir_read(struct path *realpath,
  2020. + struct ovl_readdir_data *rdd, filldir_t filler)
  2021. +{
  2022. + struct file *realfile;
  2023. + int err;
  2024. +
  2025. + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
  2026. + if (IS_ERR(realfile))
  2027. + return PTR_ERR(realfile);
  2028. +
  2029. + do {
  2030. + rdd->count = 0;
  2031. + rdd->err = 0;
  2032. + err = vfs_readdir(realfile, filler, rdd);
  2033. + if (err >= 0)
  2034. + err = rdd->err;
  2035. + } while (!err && rdd->count);
  2036. + fput(realfile);
  2037. +
  2038. + return 0;
  2039. +}
  2040. +
  2041. +static void ovl_dir_reset(struct file *file)
  2042. +{
  2043. + struct ovl_dir_file *od = file->private_data;
  2044. + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
  2045. +
  2046. + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
  2047. + list_del_init(&od->cursor);
  2048. + ovl_cache_free(&od->cache);
  2049. + od->is_cached = false;
  2050. + }
  2051. + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
  2052. + if (od->is_real && type == OVL_PATH_MERGE) {
  2053. + fput(od->realfile);
  2054. + od->realfile = NULL;
  2055. + od->is_real = false;
  2056. + }
  2057. +}
  2058. +
  2059. +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
  2060. +{
  2061. + struct ovl_cache_entry *p;
  2062. + struct dentry *dentry;
  2063. + const struct cred *old_cred;
  2064. + struct cred *override_cred;
  2065. +
  2066. + override_cred = prepare_creds();
  2067. + if (!override_cred) {
  2068. + ovl_cache_free(rdd->list);
  2069. + return -ENOMEM;
  2070. + }
  2071. +
  2072. + /*
  2073. + * CAP_SYS_ADMIN for getxattr
  2074. + * CAP_DAC_OVERRIDE for lookup
  2075. + */
  2076. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  2077. + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  2078. + old_cred = override_creds(override_cred);
  2079. +
  2080. + mutex_lock(&rdd->dir->d_inode->i_mutex);
  2081. + list_for_each_entry(p, rdd->list, l_node) {
  2082. + if (p->type != DT_LNK)
  2083. + continue;
  2084. +
  2085. + dentry = lookup_one_len(p->name, rdd->dir, p->len);
  2086. + if (IS_ERR(dentry))
  2087. + continue;
  2088. +
  2089. + p->is_whiteout = ovl_is_whiteout(dentry);
  2090. + dput(dentry);
  2091. + }
  2092. + mutex_unlock(&rdd->dir->d_inode->i_mutex);
  2093. +
  2094. + revert_creds(old_cred);
  2095. + put_cred(override_cred);
  2096. +
  2097. + return 0;
  2098. +}
  2099. +
  2100. +static inline int ovl_dir_read_merged(struct path *upperpath,
  2101. + struct path *lowerpath,
  2102. + struct ovl_readdir_data *rdd)
  2103. +{
  2104. + int err;
  2105. + struct rb_root root = RB_ROOT;
  2106. + struct list_head middle;
  2107. +
  2108. + rdd->root = &root;
  2109. + if (upperpath->dentry) {
  2110. + rdd->dir = upperpath->dentry;
  2111. + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
  2112. + if (err)
  2113. + goto out;
  2114. +
  2115. + err = ovl_dir_mark_whiteouts(rdd);
  2116. + if (err)
  2117. + goto out;
  2118. + }
  2119. + /*
  2120. + * Insert lowerpath entries before upperpath ones, this allows
  2121. + * offsets to be reasonably constant
  2122. + */
  2123. + list_add(&middle, rdd->list);
  2124. + rdd->middle = &middle;
  2125. + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
  2126. + list_del(&middle);
  2127. +out:
  2128. + rdd->root = NULL;
  2129. +
  2130. + return err;
  2131. +}
  2132. +
  2133. +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
  2134. +{
  2135. + struct list_head *l;
  2136. + loff_t off;
  2137. +
  2138. + l = od->cache.next;
  2139. + for (off = 0; off < pos; off++) {
  2140. + if (l == &od->cache)
  2141. + break;
  2142. + l = l->next;
  2143. + }
  2144. + list_move_tail(&od->cursor, l);
  2145. +}
  2146. +
  2147. +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
  2148. +{
  2149. + struct ovl_dir_file *od = file->private_data;
  2150. + int res;
  2151. +
  2152. + if (!file->f_pos)
  2153. + ovl_dir_reset(file);
  2154. +
  2155. + if (od->is_real) {
  2156. + res = vfs_readdir(od->realfile, filler, buf);
  2157. + file->f_pos = od->realfile->f_pos;
  2158. +
  2159. + return res;
  2160. + }
  2161. +
  2162. + if (!od->is_cached) {
  2163. + struct path lowerpath;
  2164. + struct path upperpath;
  2165. + struct ovl_readdir_data rdd = { .list = &od->cache };
  2166. +
  2167. + ovl_path_lower(file->f_path.dentry, &lowerpath);
  2168. + ovl_path_upper(file->f_path.dentry, &upperpath);
  2169. +
  2170. + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
  2171. + if (res) {
  2172. + ovl_cache_free(rdd.list);
  2173. + return res;
  2174. + }
  2175. +
  2176. + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
  2177. + od->is_cached = true;
  2178. +
  2179. + ovl_seek_cursor(od, file->f_pos);
  2180. + }
  2181. +
  2182. + while (od->cursor.next != &od->cache) {
  2183. + int over;
  2184. + loff_t off;
  2185. + struct ovl_cache_entry *p;
  2186. +
  2187. + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
  2188. + off = file->f_pos;
  2189. + if (!p->is_whiteout) {
  2190. + over = filler(buf, p->name, p->len, off, p->ino,
  2191. + p->type);
  2192. + if (over)
  2193. + break;
  2194. + }
  2195. + file->f_pos++;
  2196. + list_move(&od->cursor, &p->l_node);
  2197. + }
  2198. +
  2199. + return 0;
  2200. +}
  2201. +
  2202. +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
  2203. +{
  2204. + loff_t res;
  2205. + struct ovl_dir_file *od = file->private_data;
  2206. +
  2207. + mutex_lock(&file->f_dentry->d_inode->i_mutex);
  2208. + if (!file->f_pos)
  2209. + ovl_dir_reset(file);
  2210. +
  2211. + if (od->is_real) {
  2212. + res = vfs_llseek(od->realfile, offset, origin);
  2213. + file->f_pos = od->realfile->f_pos;
  2214. + } else {
  2215. + res = -EINVAL;
  2216. +
  2217. + switch (origin) {
  2218. + case SEEK_CUR:
  2219. + offset += file->f_pos;
  2220. + break;
  2221. + case SEEK_SET:
  2222. + break;
  2223. + default:
  2224. + goto out_unlock;
  2225. + }
  2226. + if (offset < 0)
  2227. + goto out_unlock;
  2228. +
  2229. + if (offset != file->f_pos) {
  2230. + file->f_pos = offset;
  2231. + if (od->is_cached)
  2232. + ovl_seek_cursor(od, offset);
  2233. + }
  2234. + res = offset;
  2235. + }
  2236. +out_unlock:
  2237. + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  2238. +
  2239. + return res;
  2240. +}
  2241. +
  2242. +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
  2243. + int datasync)
  2244. +{
  2245. + struct ovl_dir_file *od = file->private_data;
  2246. +
  2247. + /* May need to reopen directory if it got copied up */
  2248. + if (!od->realfile) {
  2249. + struct path upperpath;
  2250. +
  2251. + ovl_path_upper(file->f_path.dentry, &upperpath);
  2252. + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
  2253. + if (IS_ERR(od->realfile))
  2254. + return PTR_ERR(od->realfile);
  2255. + }
  2256. +
  2257. + return vfs_fsync_range(od->realfile, start, end, datasync);
  2258. +}
  2259. +
  2260. +static int ovl_dir_release(struct inode *inode, struct file *file)
  2261. +{
  2262. + struct ovl_dir_file *od = file->private_data;
  2263. +
  2264. + list_del(&od->cursor);
  2265. + ovl_cache_free(&od->cache);
  2266. + if (od->realfile)
  2267. + fput(od->realfile);
  2268. + kfree(od);
  2269. +
  2270. + return 0;
  2271. +}
  2272. +
  2273. +static int ovl_dir_open(struct inode *inode, struct file *file)
  2274. +{
  2275. + struct path realpath;
  2276. + struct file *realfile;
  2277. + struct ovl_dir_file *od;
  2278. + enum ovl_path_type type;
  2279. +
  2280. + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
  2281. + if (!od)
  2282. + return -ENOMEM;
  2283. +
  2284. + type = ovl_path_real(file->f_path.dentry, &realpath);
  2285. + realfile = ovl_path_open(&realpath, file->f_flags);
  2286. + if (IS_ERR(realfile)) {
  2287. + kfree(od);
  2288. + return PTR_ERR(realfile);
  2289. + }
  2290. + INIT_LIST_HEAD(&od->cache);
  2291. + INIT_LIST_HEAD(&od->cursor);
  2292. + od->is_cached = false;
  2293. + od->realfile = realfile;
  2294. + od->is_real = (type != OVL_PATH_MERGE);
  2295. + file->private_data = od;
  2296. +
  2297. + return 0;
  2298. +}
  2299. +
  2300. +const struct file_operations ovl_dir_operations = {
  2301. + .read = generic_read_dir,
  2302. + .open = ovl_dir_open,
  2303. + .readdir = ovl_readdir,
  2304. + .llseek = ovl_dir_llseek,
  2305. + .fsync = ovl_dir_fsync,
  2306. + .release = ovl_dir_release,
  2307. +};
  2308. +
  2309. +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
  2310. +{
  2311. + int err;
  2312. + struct path lowerpath;
  2313. + struct path upperpath;
  2314. + struct ovl_cache_entry *p;
  2315. + struct ovl_readdir_data rdd = { .list = list };
  2316. +
  2317. + ovl_path_upper(dentry, &upperpath);
  2318. + ovl_path_lower(dentry, &lowerpath);
  2319. +
  2320. + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
  2321. + if (err)
  2322. + return err;
  2323. +
  2324. + err = 0;
  2325. +
  2326. + list_for_each_entry(p, list, l_node) {
  2327. + if (p->is_whiteout)
  2328. + continue;
  2329. +
  2330. + if (p->name[0] == '.') {
  2331. + if (p->len == 1)
  2332. + continue;
  2333. + if (p->len == 2 && p->name[1] == '.')
  2334. + continue;
  2335. + }
  2336. + err = -ENOTEMPTY;
  2337. + break;
  2338. + }
  2339. +
  2340. + return err;
  2341. +}
  2342. +
  2343. +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
  2344. +{
  2345. + struct path upperpath;
  2346. + struct dentry *upperdir;
  2347. + struct ovl_cache_entry *p;
  2348. + const struct cred *old_cred;
  2349. + struct cred *override_cred;
  2350. + int err;
  2351. +
  2352. + ovl_path_upper(dir, &upperpath);
  2353. + upperdir = upperpath.dentry;
  2354. +
  2355. + override_cred = prepare_creds();
  2356. + if (!override_cred)
  2357. + return -ENOMEM;
  2358. +
  2359. + /*
  2360. + * CAP_DAC_OVERRIDE for lookup and unlink
  2361. + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
  2362. + * CAP_FOWNER for unlink in sticky directory
  2363. + */
  2364. + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
  2365. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  2366. + cap_raise(override_cred->cap_effective, CAP_FOWNER);
  2367. + old_cred = override_creds(override_cred);
  2368. +
  2369. + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
  2370. + if (err)
  2371. + goto out_revert_creds;
  2372. +
  2373. + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
  2374. + list_for_each_entry(p, list, l_node) {
  2375. + struct dentry *dentry;
  2376. + int ret;
  2377. +
  2378. + if (!p->is_whiteout)
  2379. + continue;
  2380. +
  2381. + dentry = lookup_one_len(p->name, upperdir, p->len);
  2382. + if (IS_ERR(dentry)) {
  2383. + printk(KERN_WARNING
  2384. + "overlayfs: failed to lookup whiteout %.*s: %li\n",
  2385. + p->len, p->name, PTR_ERR(dentry));
  2386. + continue;
  2387. + }
  2388. + ret = vfs_unlink(upperdir->d_inode, dentry);
  2389. + dput(dentry);
  2390. + if (ret)
  2391. + printk(KERN_WARNING
  2392. + "overlayfs: failed to unlink whiteout %.*s: %i\n",
  2393. + p->len, p->name, ret);
  2394. + }
  2395. + mutex_unlock(&upperdir->d_inode->i_mutex);
  2396. +
  2397. +out_revert_creds:
  2398. + revert_creds(old_cred);
  2399. + put_cred(override_cred);
  2400. +
  2401. + return err;
  2402. +}
  2403. +
  2404. +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
  2405. +{
  2406. + int err;
  2407. + LIST_HEAD(list);
  2408. +
  2409. + err = ovl_check_empty_dir(dentry, &list);
  2410. + if (!err && type == OVL_PATH_MERGE)
  2411. + err = ovl_remove_whiteouts(dentry, &list);
  2412. +
  2413. + ovl_cache_free(&list);
  2414. +
  2415. + return err;
  2416. +}
  2417. --- /dev/null
  2418. +++ b/fs/overlayfs/super.c
  2419. @@ -0,0 +1,685 @@
  2420. +/*
  2421. + *
  2422. + * Copyright (C) 2011 Novell Inc.
  2423. + *
  2424. + * This program is free software; you can redistribute it and/or modify it
  2425. + * under the terms of the GNU General Public License version 2 as published by
  2426. + * the Free Software Foundation.
  2427. + */
  2428. +
  2429. +#include <linux/fs.h>
  2430. +#include <linux/namei.h>
  2431. +#include <linux/xattr.h>
  2432. +#include <linux/security.h>
  2433. +#include <linux/mount.h>
  2434. +#include <linux/slab.h>
  2435. +#include <linux/parser.h>
  2436. +#include <linux/module.h>
  2437. +#include <linux/cred.h>
  2438. +#include <linux/sched.h>
  2439. +#include <linux/statfs.h>
  2440. +#include <linux/seq_file.h>
  2441. +#include "overlayfs.h"
  2442. +
  2443. +MODULE_AUTHOR("Miklos Szeredi <[email protected]>");
  2444. +MODULE_DESCRIPTION("Overlay filesystem");
  2445. +MODULE_LICENSE("GPL");
  2446. +
  2447. +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
  2448. +
  2449. +struct ovl_config {
  2450. + char *lowerdir;
  2451. + char *upperdir;
  2452. +};
  2453. +
  2454. +/* private information held for overlayfs's superblock */
  2455. +struct ovl_fs {
  2456. + struct vfsmount *upper_mnt;
  2457. + struct vfsmount *lower_mnt;
  2458. + long lower_namelen;
  2459. + /* pathnames of lower and upper dirs, for show_options */
  2460. + struct ovl_config config;
  2461. +};
  2462. +
  2463. +/* private information held for every overlayfs dentry */
  2464. +struct ovl_entry {
  2465. + /*
  2466. + * Keep "double reference" on upper dentries, so that
  2467. + * d_delete() doesn't think it's OK to reset d_inode to NULL.
  2468. + */
  2469. + struct dentry *__upperdentry;
  2470. + struct dentry *lowerdentry;
  2471. + union {
  2472. + struct {
  2473. + u64 version;
  2474. + bool opaque;
  2475. + };
  2476. + struct rcu_head rcu;
  2477. + };
  2478. +};
  2479. +
  2480. +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
  2481. +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
  2482. +
  2483. +
  2484. +enum ovl_path_type ovl_path_type(struct dentry *dentry)
  2485. +{
  2486. + struct ovl_entry *oe = dentry->d_fsdata;
  2487. +
  2488. + if (oe->__upperdentry) {
  2489. + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
  2490. + return OVL_PATH_MERGE;
  2491. + else
  2492. + return OVL_PATH_UPPER;
  2493. + } else {
  2494. + return OVL_PATH_LOWER;
  2495. + }
  2496. +}
  2497. +
  2498. +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
  2499. +{
  2500. + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
  2501. + smp_read_barrier_depends();
  2502. + return upperdentry;
  2503. +}
  2504. +
  2505. +void ovl_path_upper(struct dentry *dentry, struct path *path)
  2506. +{
  2507. + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  2508. + struct ovl_entry *oe = dentry->d_fsdata;
  2509. +
  2510. + path->mnt = ofs->upper_mnt;
  2511. + path->dentry = ovl_upperdentry_dereference(oe);
  2512. +}
  2513. +
  2514. +void ovl_path_lower(struct dentry *dentry, struct path *path)
  2515. +{
  2516. + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  2517. + struct ovl_entry *oe = dentry->d_fsdata;
  2518. +
  2519. + path->mnt = ofs->lower_mnt;
  2520. + path->dentry = oe->lowerdentry;
  2521. +}
  2522. +
  2523. +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  2524. +{
  2525. +
  2526. + enum ovl_path_type type = ovl_path_type(dentry);
  2527. +
  2528. + if (type == OVL_PATH_LOWER)
  2529. + ovl_path_lower(dentry, path);
  2530. + else
  2531. + ovl_path_upper(dentry, path);
  2532. +
  2533. + return type;
  2534. +}
  2535. +
  2536. +struct dentry *ovl_dentry_upper(struct dentry *dentry)
  2537. +{
  2538. + struct ovl_entry *oe = dentry->d_fsdata;
  2539. +
  2540. + return ovl_upperdentry_dereference(oe);
  2541. +}
  2542. +
  2543. +struct dentry *ovl_dentry_lower(struct dentry *dentry)
  2544. +{
  2545. + struct ovl_entry *oe = dentry->d_fsdata;
  2546. +
  2547. + return oe->lowerdentry;
  2548. +}
  2549. +
  2550. +struct dentry *ovl_dentry_real(struct dentry *dentry)
  2551. +{
  2552. + struct ovl_entry *oe = dentry->d_fsdata;
  2553. + struct dentry *realdentry;
  2554. +
  2555. + realdentry = ovl_upperdentry_dereference(oe);
  2556. + if (!realdentry)
  2557. + realdentry = oe->lowerdentry;
  2558. +
  2559. + return realdentry;
  2560. +}
  2561. +
  2562. +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
  2563. +{
  2564. + struct dentry *realdentry;
  2565. +
  2566. + realdentry = ovl_upperdentry_dereference(oe);
  2567. + if (realdentry) {
  2568. + *is_upper = true;
  2569. + } else {
  2570. + realdentry = oe->lowerdentry;
  2571. + *is_upper = false;
  2572. + }
  2573. + return realdentry;
  2574. +}
  2575. +
  2576. +bool ovl_dentry_is_opaque(struct dentry *dentry)
  2577. +{
  2578. + struct ovl_entry *oe = dentry->d_fsdata;
  2579. + return oe->opaque;
  2580. +}
  2581. +
  2582. +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
  2583. +{
  2584. + struct ovl_entry *oe = dentry->d_fsdata;
  2585. + oe->opaque = opaque;
  2586. +}
  2587. +
  2588. +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
  2589. +{
  2590. + struct ovl_entry *oe = dentry->d_fsdata;
  2591. +
  2592. + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
  2593. + WARN_ON(oe->__upperdentry);
  2594. + BUG_ON(!upperdentry->d_inode);
  2595. + smp_wmb();
  2596. + oe->__upperdentry = dget(upperdentry);
  2597. +}
  2598. +
  2599. +void ovl_dentry_version_inc(struct dentry *dentry)
  2600. +{
  2601. + struct ovl_entry *oe = dentry->d_fsdata;
  2602. +
  2603. + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  2604. + oe->version++;
  2605. +}
  2606. +
  2607. +u64 ovl_dentry_version_get(struct dentry *dentry)
  2608. +{
  2609. + struct ovl_entry *oe = dentry->d_fsdata;
  2610. +
  2611. + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  2612. + return oe->version;
  2613. +}
  2614. +
  2615. +bool ovl_is_whiteout(struct dentry *dentry)
  2616. +{
  2617. + int res;
  2618. + char val;
  2619. +
  2620. + if (!dentry)
  2621. + return false;
  2622. + if (!dentry->d_inode)
  2623. + return false;
  2624. + if (!S_ISLNK(dentry->d_inode->i_mode))
  2625. + return false;
  2626. +
  2627. + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
  2628. + if (res == 1 && val == 'y')
  2629. + return true;
  2630. +
  2631. + return false;
  2632. +}
  2633. +
  2634. +static bool ovl_is_opaquedir(struct dentry *dentry)
  2635. +{
  2636. + int res;
  2637. + char val;
  2638. +
  2639. + if (!S_ISDIR(dentry->d_inode->i_mode))
  2640. + return false;
  2641. +
  2642. + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
  2643. + if (res == 1 && val == 'y')
  2644. + return true;
  2645. +
  2646. + return false;
  2647. +}
  2648. +
  2649. +static void ovl_entry_free(struct rcu_head *head)
  2650. +{
  2651. + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
  2652. + kfree(oe);
  2653. +}
  2654. +
  2655. +static void ovl_dentry_release(struct dentry *dentry)
  2656. +{
  2657. + struct ovl_entry *oe = dentry->d_fsdata;
  2658. +
  2659. + if (oe) {
  2660. + dput(oe->__upperdentry);
  2661. + dput(oe->__upperdentry);
  2662. + dput(oe->lowerdentry);
  2663. + call_rcu(&oe->rcu, ovl_entry_free);
  2664. + }
  2665. +}
  2666. +
  2667. +const struct dentry_operations ovl_dentry_operations = {
  2668. + .d_release = ovl_dentry_release,
  2669. +};
  2670. +
  2671. +static struct ovl_entry *ovl_alloc_entry(void)
  2672. +{
  2673. + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
  2674. +}
  2675. +
  2676. +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
  2677. + struct qstr *name)
  2678. +{
  2679. + struct dentry *dentry;
  2680. +
  2681. + mutex_lock(&dir->d_inode->i_mutex);
  2682. + dentry = lookup_one_len(name->name, dir, name->len);
  2683. + mutex_unlock(&dir->d_inode->i_mutex);
  2684. +
  2685. + if (IS_ERR(dentry)) {
  2686. + if (PTR_ERR(dentry) == -ENOENT)
  2687. + dentry = NULL;
  2688. + } else if (!dentry->d_inode) {
  2689. + dput(dentry);
  2690. + dentry = NULL;
  2691. + }
  2692. + return dentry;
  2693. +}
  2694. +
  2695. +static int ovl_do_lookup(struct dentry *dentry)
  2696. +{
  2697. + struct ovl_entry *oe;
  2698. + struct dentry *upperdir;
  2699. + struct dentry *lowerdir;
  2700. + struct dentry *upperdentry = NULL;
  2701. + struct dentry *lowerdentry = NULL;
  2702. + struct inode *inode = NULL;
  2703. + int err;
  2704. +
  2705. + err = -ENOMEM;
  2706. + oe = ovl_alloc_entry();
  2707. + if (!oe)
  2708. + goto out;
  2709. +
  2710. + upperdir = ovl_dentry_upper(dentry->d_parent);
  2711. + lowerdir = ovl_dentry_lower(dentry->d_parent);
  2712. +
  2713. + if (upperdir) {
  2714. + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
  2715. + err = PTR_ERR(upperdentry);
  2716. + if (IS_ERR(upperdentry))
  2717. + goto out_put_dir;
  2718. +
  2719. + if (lowerdir && upperdentry &&
  2720. + (S_ISLNK(upperdentry->d_inode->i_mode) ||
  2721. + S_ISDIR(upperdentry->d_inode->i_mode))) {
  2722. + const struct cred *old_cred;
  2723. + struct cred *override_cred;
  2724. +
  2725. + err = -ENOMEM;
  2726. + override_cred = prepare_creds();
  2727. + if (!override_cred)
  2728. + goto out_dput_upper;
  2729. +
  2730. + /* CAP_SYS_ADMIN needed for getxattr */
  2731. + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
  2732. + old_cred = override_creds(override_cred);
  2733. +
  2734. + if (ovl_is_opaquedir(upperdentry)) {
  2735. + oe->opaque = true;
  2736. + } else if (ovl_is_whiteout(upperdentry)) {
  2737. + dput(upperdentry);
  2738. + upperdentry = NULL;
  2739. + oe->opaque = true;
  2740. + }
  2741. + revert_creds(old_cred);
  2742. + put_cred(override_cred);
  2743. + }
  2744. + }
  2745. + if (lowerdir && !oe->opaque) {
  2746. + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
  2747. + err = PTR_ERR(lowerdentry);
  2748. + if (IS_ERR(lowerdentry))
  2749. + goto out_dput_upper;
  2750. + }
  2751. +
  2752. + if (lowerdentry && upperdentry &&
  2753. + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
  2754. + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
  2755. + dput(lowerdentry);
  2756. + lowerdentry = NULL;
  2757. + oe->opaque = true;
  2758. + }
  2759. +
  2760. + if (lowerdentry || upperdentry) {
  2761. + struct dentry *realdentry;
  2762. +
  2763. + realdentry = upperdentry ? upperdentry : lowerdentry;
  2764. + err = -ENOMEM;
  2765. + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
  2766. + oe);
  2767. + if (!inode)
  2768. + goto out_dput;
  2769. + ovl_copyattr(realdentry->d_inode, inode);
  2770. + }
  2771. +
  2772. + if (upperdentry)
  2773. + oe->__upperdentry = dget(upperdentry);
  2774. +
  2775. + if (lowerdentry)
  2776. + oe->lowerdentry = lowerdentry;
  2777. +
  2778. + dentry->d_fsdata = oe;
  2779. + dentry->d_op = &ovl_dentry_operations;
  2780. + d_add(dentry, inode);
  2781. +
  2782. + return 0;
  2783. +
  2784. +out_dput:
  2785. + dput(lowerdentry);
  2786. +out_dput_upper:
  2787. + dput(upperdentry);
  2788. +out_put_dir:
  2789. + kfree(oe);
  2790. +out:
  2791. + return err;
  2792. +}
  2793. +
  2794. +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  2795. + unsigned int flags)
  2796. +{
  2797. + int err = ovl_do_lookup(dentry);
  2798. +
  2799. + if (err)
  2800. + return ERR_PTR(err);
  2801. +
  2802. + return NULL;
  2803. +}
  2804. +
  2805. +struct file *ovl_path_open(struct path *path, int flags)
  2806. +{
  2807. + path_get(path);
  2808. + return dentry_open(path, flags, current_cred());
  2809. +}
  2810. +
  2811. +static void ovl_put_super(struct super_block *sb)
  2812. +{
  2813. + struct ovl_fs *ufs = sb->s_fs_info;
  2814. +
  2815. + if (!(sb->s_flags & MS_RDONLY))
  2816. + mnt_drop_write(ufs->upper_mnt);
  2817. +
  2818. + mntput(ufs->upper_mnt);
  2819. + mntput(ufs->lower_mnt);
  2820. +
  2821. + kfree(ufs->config.lowerdir);
  2822. + kfree(ufs->config.upperdir);
  2823. + kfree(ufs);
  2824. +}
  2825. +
  2826. +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
  2827. +{
  2828. + int flags = *flagsp;
  2829. + struct ovl_fs *ufs = sb->s_fs_info;
  2830. +
  2831. + /* When remounting rw or ro, we need to adjust the write access to the
  2832. + * upper fs.
  2833. + */
  2834. + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
  2835. + /* No change to readonly status */
  2836. + return 0;
  2837. +
  2838. + if (flags & MS_RDONLY) {
  2839. + mnt_drop_write(ufs->upper_mnt);
  2840. + return 0;
  2841. + } else
  2842. + return mnt_want_write(ufs->upper_mnt);
  2843. +}
  2844. +
  2845. +/**
  2846. + * ovl_statfs
  2847. + * @sb: The overlayfs super block
  2848. + * @buf: The struct kstatfs to fill in with stats
  2849. + *
  2850. + * Get the filesystem statistics. As writes always target the upper layer
  2851. + * filesystem pass the statfs to the same filesystem.
  2852. + */
  2853. +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
  2854. +{
  2855. + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  2856. + struct dentry *root_dentry = dentry->d_sb->s_root;
  2857. + struct path path;
  2858. + int err;
  2859. +
  2860. + ovl_path_upper(root_dentry, &path);
  2861. +
  2862. + err = vfs_statfs(&path, buf);
  2863. + if (!err) {
  2864. + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
  2865. + buf->f_type = OVERLAYFS_SUPER_MAGIC;
  2866. + }
  2867. +
  2868. + return err;
  2869. +}
  2870. +
  2871. +/**
  2872. + * ovl_show_options
  2873. + *
  2874. + * Prints the mount options for a given superblock.
  2875. + * Returns zero; does not fail.
  2876. + */
  2877. +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
  2878. +{
  2879. + struct super_block *sb = dentry->d_sb;
  2880. + struct ovl_fs *ufs = sb->s_fs_info;
  2881. +
  2882. + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
  2883. + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
  2884. + return 0;
  2885. +}
  2886. +
  2887. +static const struct super_operations ovl_super_operations = {
  2888. + .put_super = ovl_put_super,
  2889. + .remount_fs = ovl_remount_fs,
  2890. + .statfs = ovl_statfs,
  2891. + .show_options = ovl_show_options,
  2892. +};
  2893. +
  2894. +enum {
  2895. + Opt_lowerdir,
  2896. + Opt_upperdir,
  2897. + Opt_err,
  2898. +};
  2899. +
  2900. +static const match_table_t ovl_tokens = {
  2901. + {Opt_lowerdir, "lowerdir=%s"},
  2902. + {Opt_upperdir, "upperdir=%s"},
  2903. + {Opt_err, NULL}
  2904. +};
  2905. +
  2906. +static int ovl_parse_opt(char *opt, struct ovl_config *config)
  2907. +{
  2908. + char *p;
  2909. +
  2910. + config->upperdir = NULL;
  2911. + config->lowerdir = NULL;
  2912. +
  2913. + while ((p = strsep(&opt, ",")) != NULL) {
  2914. + int token;
  2915. + substring_t args[MAX_OPT_ARGS];
  2916. +
  2917. + if (!*p)
  2918. + continue;
  2919. +
  2920. + token = match_token(p, ovl_tokens, args);
  2921. + switch (token) {
  2922. + case Opt_upperdir:
  2923. + kfree(config->upperdir);
  2924. + config->upperdir = match_strdup(&args[0]);
  2925. + if (!config->upperdir)
  2926. + return -ENOMEM;
  2927. + break;
  2928. +
  2929. + case Opt_lowerdir:
  2930. + kfree(config->lowerdir);
  2931. + config->lowerdir = match_strdup(&args[0]);
  2932. + if (!config->lowerdir)
  2933. + return -ENOMEM;
  2934. + break;
  2935. +
  2936. + default:
  2937. + return -EINVAL;
  2938. + }
  2939. + }
  2940. + return 0;
  2941. +}
  2942. +
  2943. +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
  2944. +{
  2945. + struct path lowerpath;
  2946. + struct path upperpath;
  2947. + struct inode *root_inode;
  2948. + struct dentry *root_dentry;
  2949. + struct ovl_entry *oe;
  2950. + struct ovl_fs *ufs;
  2951. + struct kstatfs statfs;
  2952. + int err;
  2953. +
  2954. + err = -ENOMEM;
  2955. + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
  2956. + if (!ufs)
  2957. + goto out;
  2958. +
  2959. + err = ovl_parse_opt((char *) data, &ufs->config);
  2960. + if (err)
  2961. + goto out_free_ufs;
  2962. +
  2963. + err = -EINVAL;
  2964. + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
  2965. + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
  2966. + goto out_free_config;
  2967. + }
  2968. +
  2969. + oe = ovl_alloc_entry();
  2970. + if (oe == NULL)
  2971. + goto out_free_config;
  2972. +
  2973. + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
  2974. + if (err)
  2975. + goto out_free_oe;
  2976. +
  2977. + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
  2978. + if (err)
  2979. + goto out_put_upperpath;
  2980. +
  2981. + err = -ENOTDIR;
  2982. + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
  2983. + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
  2984. + goto out_put_lowerpath;
  2985. +
  2986. + err = vfs_statfs(&lowerpath, &statfs);
  2987. + if (err) {
  2988. + printk(KERN_ERR "overlayfs: statfs failed on lowerpath\n");
  2989. + goto out_put_lowerpath;
  2990. + }
  2991. + ufs->lower_namelen = statfs.f_namelen;
  2992. +
  2993. + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
  2994. + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
  2995. +
  2996. + err = -EINVAL;
  2997. + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
  2998. + printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
  2999. + goto out_put_lowerpath;
  3000. + }
  3001. +
  3002. +
  3003. + ufs->upper_mnt = clone_private_mount(&upperpath);
  3004. + err = PTR_ERR(ufs->upper_mnt);
  3005. + if (IS_ERR(ufs->upper_mnt)) {
  3006. + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
  3007. + goto out_put_lowerpath;
  3008. + }
  3009. +
  3010. + ufs->lower_mnt = clone_private_mount(&lowerpath);
  3011. + err = PTR_ERR(ufs->lower_mnt);
  3012. + if (IS_ERR(ufs->lower_mnt)) {
  3013. + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
  3014. + goto out_put_upper_mnt;
  3015. + }
  3016. +
  3017. + /*
  3018. + * Make lower_mnt R/O. That way fchmod/fchown on lower file
  3019. + * will fail instead of modifying lower fs.
  3020. + */
  3021. + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
  3022. +
  3023. + /* If the upper fs is r/o, we mark overlayfs r/o too */
  3024. + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
  3025. + sb->s_flags |= MS_RDONLY;
  3026. +
  3027. + if (!(sb->s_flags & MS_RDONLY)) {
  3028. + err = mnt_want_write(ufs->upper_mnt);
  3029. + if (err)
  3030. + goto out_put_lower_mnt;
  3031. + }
  3032. +
  3033. + err = -ENOMEM;
  3034. + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
  3035. + if (!root_inode)
  3036. + goto out_drop_write;
  3037. +
  3038. + root_dentry = d_make_root(root_inode);
  3039. + if (!root_dentry)
  3040. + goto out_drop_write;
  3041. +
  3042. + mntput(upperpath.mnt);
  3043. + mntput(lowerpath.mnt);
  3044. +
  3045. + oe->__upperdentry = dget(upperpath.dentry);
  3046. + oe->lowerdentry = lowerpath.dentry;
  3047. +
  3048. + root_dentry->d_fsdata = oe;
  3049. + root_dentry->d_op = &ovl_dentry_operations;
  3050. +
  3051. + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
  3052. + sb->s_op = &ovl_super_operations;
  3053. + sb->s_root = root_dentry;
  3054. + sb->s_fs_info = ufs;
  3055. +
  3056. + return 0;
  3057. +
  3058. +out_drop_write:
  3059. + if (!(sb->s_flags & MS_RDONLY))
  3060. + mnt_drop_write(ufs->upper_mnt);
  3061. +out_put_lower_mnt:
  3062. + mntput(ufs->lower_mnt);
  3063. +out_put_upper_mnt:
  3064. + mntput(ufs->upper_mnt);
  3065. +out_put_lowerpath:
  3066. + path_put(&lowerpath);
  3067. +out_put_upperpath:
  3068. + path_put(&upperpath);
  3069. +out_free_oe:
  3070. + kfree(oe);
  3071. +out_free_config:
  3072. + kfree(ufs->config.lowerdir);
  3073. + kfree(ufs->config.upperdir);
  3074. +out_free_ufs:
  3075. + kfree(ufs);
  3076. +out:
  3077. + return err;
  3078. +}
  3079. +
  3080. +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
  3081. + const char *dev_name, void *raw_data)
  3082. +{
  3083. + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
  3084. +}
  3085. +
  3086. +static struct file_system_type ovl_fs_type = {
  3087. + .owner = THIS_MODULE,
  3088. + .name = "overlayfs",
  3089. + .mount = ovl_mount,
  3090. + .kill_sb = kill_anon_super,
  3091. +};
  3092. +
  3093. +static int __init ovl_init(void)
  3094. +{
  3095. + return register_filesystem(&ovl_fs_type);
  3096. +}
  3097. +
  3098. +static void __exit ovl_exit(void)
  3099. +{
  3100. + unregister_filesystem(&ovl_fs_type);
  3101. +}
  3102. +
  3103. +module_init(ovl_init);
  3104. +module_exit(ovl_exit);
  3105. --- a/fs/splice.c
  3106. +++ b/fs/splice.c
  3107. @@ -1310,6 +1310,7 @@ long do_splice_direct(struct file *in, l
  3108. return ret;
  3109. }
  3110. +EXPORT_SYMBOL(do_splice_direct);
  3111. static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
  3112. struct pipe_inode_info *opipe,
  3113. --- a/include/linux/fs.h
  3114. +++ b/include/linux/fs.h
  3115. @@ -244,6 +244,12 @@ struct iattr {
  3116. */
  3117. #include <linux/quota.h>
  3118. +/*
  3119. + * Maximum number of layers of fs stack. Needs to be limited to
  3120. + * prevent kernel stack overflow
  3121. + */
  3122. +#define FILESYSTEM_MAX_STACK_DEPTH 2
  3123. +
  3124. /**
  3125. * enum positive_aop_returns - aop return codes with specific semantics
  3126. *
  3127. @@ -1320,6 +1326,11 @@ struct super_block {
  3128. /* Being remounted read-only */
  3129. int s_readonly_remount;
  3130. +
  3131. + /*
  3132. + * Indicates how deep in a filesystem stack this SB is
  3133. + */
  3134. + int s_stack_depth;
  3135. };
  3136. /* superblock cache pruning functions */
  3137. @@ -1573,6 +1584,7 @@ struct inode_operations {
  3138. int (*atomic_open)(struct inode *, struct dentry *,
  3139. struct file *, unsigned open_flag,
  3140. umode_t create_mode, int *opened);
  3141. + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
  3142. } ____cacheline_aligned;
  3143. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  3144. @@ -2006,6 +2018,7 @@ extern struct file *file_open_name(struc
  3145. extern struct file *filp_open(const char *, int, umode_t);
  3146. extern struct file *file_open_root(struct dentry *, struct vfsmount *,
  3147. const char *, int);
  3148. +extern int vfs_open(const struct path *, struct file *, const struct cred *);
  3149. extern struct file * dentry_open(const struct path *, int, const struct cred *);
  3150. extern int filp_close(struct file *, fl_owner_t id);
  3151. @@ -2211,6 +2224,7 @@ extern sector_t bmap(struct inode *, sec
  3152. #endif
  3153. extern int notify_change(struct dentry *, struct iattr *);
  3154. extern int inode_permission(struct inode *, int);
  3155. +extern int __inode_permission(struct inode *, int);
  3156. extern int generic_permission(struct inode *, int);
  3157. static inline bool execute_ok(struct inode *inode)
  3158. --- a/include/linux/mount.h
  3159. +++ b/include/linux/mount.h
  3160. @@ -68,6 +68,9 @@ extern void mnt_pin(struct vfsmount *mnt
  3161. extern void mnt_unpin(struct vfsmount *mnt);
  3162. extern int __mnt_is_readonly(struct vfsmount *mnt);
  3163. +struct path;
  3164. +extern struct vfsmount *clone_private_mount(struct path *path);
  3165. +
  3166. struct file_system_type;
  3167. extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
  3168. int flags, const char *name,