100-overlayfs.patch 79 KB

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