fs.h 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_FS_H
  3. #define _LINUX_FS_H
  4. #include <linux/linkage.h>
  5. #include <linux/wait_bit.h>
  6. #include <linux/kdev_t.h>
  7. #include <linux/dcache.h>
  8. #include <linux/path.h>
  9. #include <linux/stat.h>
  10. #include <linux/cache.h>
  11. #include <linux/list.h>
  12. #include <linux/list_lru.h>
  13. #include <linux/llist.h>
  14. #include <linux/radix-tree.h>
  15. #include <linux/xarray.h>
  16. #include <linux/rbtree.h>
  17. #include <linux/init.h>
  18. #include <linux/pid.h>
  19. #include <linux/bug.h>
  20. #include <linux/mutex.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/mm_types.h>
  23. #include <linux/capability.h>
  24. #include <linux/semaphore.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/rculist_bl.h>
  27. #include <linux/atomic.h>
  28. #include <linux/shrinker.h>
  29. #include <linux/migrate_mode.h>
  30. #include <linux/uidgid.h>
  31. #include <linux/lockdep.h>
  32. #include <linux/percpu-rwsem.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/delayed_call.h>
  35. #include <linux/uuid.h>
  36. #include <linux/errseq.h>
  37. #include <linux/ioprio.h>
  38. #include <linux/fs_types.h>
  39. #include <linux/build_bug.h>
  40. #include <linux/stddef.h>
  41. #include <linux/mount.h>
  42. #include <linux/cred.h>
  43. #include <linux/mnt_idmapping.h>
  44. #include <linux/slab.h>
  45. #include <asm/byteorder.h>
  46. #include <uapi/linux/fs.h>
  47. struct backing_dev_info;
  48. struct bdi_writeback;
  49. struct bio;
  50. struct io_comp_batch;
  51. struct export_operations;
  52. struct fiemap_extent_info;
  53. struct hd_geometry;
  54. struct iovec;
  55. struct kiocb;
  56. struct kobject;
  57. struct pipe_inode_info;
  58. struct poll_table_struct;
  59. struct kstatfs;
  60. struct vm_area_struct;
  61. struct vfsmount;
  62. struct cred;
  63. struct swap_info_struct;
  64. struct seq_file;
  65. struct workqueue_struct;
  66. struct iov_iter;
  67. struct fscrypt_info;
  68. struct fscrypt_operations;
  69. struct fsverity_info;
  70. struct fsverity_operations;
  71. struct fs_context;
  72. struct fs_parameter_spec;
  73. struct fileattr;
  74. struct iomap_ops;
  75. extern void __init inode_init(void);
  76. extern void __init inode_init_early(void);
  77. extern void __init files_init(void);
  78. extern void __init files_maxfiles_init(void);
  79. extern unsigned long get_max_files(void);
  80. extern unsigned int sysctl_nr_open;
  81. typedef __kernel_rwf_t rwf_t;
  82. struct buffer_head;
  83. typedef int (get_block_t)(struct inode *inode, sector_t iblock,
  84. struct buffer_head *bh_result, int create);
  85. typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
  86. ssize_t bytes, void *private);
  87. #define MAY_EXEC 0x00000001
  88. #define MAY_WRITE 0x00000002
  89. #define MAY_READ 0x00000004
  90. #define MAY_APPEND 0x00000008
  91. #define MAY_ACCESS 0x00000010
  92. #define MAY_OPEN 0x00000020
  93. #define MAY_CHDIR 0x00000040
  94. /* called from RCU mode, don't block */
  95. #define MAY_NOT_BLOCK 0x00000080
  96. /*
  97. * flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
  98. * to O_WRONLY and O_RDWR via the strange trick in do_dentry_open()
  99. */
  100. /* file is open for reading */
  101. #define FMODE_READ ((__force fmode_t)0x1)
  102. /* file is open for writing */
  103. #define FMODE_WRITE ((__force fmode_t)0x2)
  104. /* file is seekable */
  105. #define FMODE_LSEEK ((__force fmode_t)0x4)
  106. /* file can be accessed using pread */
  107. #define FMODE_PREAD ((__force fmode_t)0x8)
  108. /* file can be accessed using pwrite */
  109. #define FMODE_PWRITE ((__force fmode_t)0x10)
  110. /* File is opened for execution with sys_execve / sys_uselib */
  111. #define FMODE_EXEC ((__force fmode_t)0x20)
  112. /* File is opened with O_NDELAY (only set for block devices) */
  113. #define FMODE_NDELAY ((__force fmode_t)0x40)
  114. /* File is opened with O_EXCL (only set for block devices) */
  115. #define FMODE_EXCL ((__force fmode_t)0x80)
  116. /* File is opened using open(.., 3, ..) and is writeable only for ioctls
  117. (specialy hack for floppy.c) */
  118. #define FMODE_WRITE_IOCTL ((__force fmode_t)0x100)
  119. /* 32bit hashes as llseek() offset (for directories) */
  120. #define FMODE_32BITHASH ((__force fmode_t)0x200)
  121. /* 64bit hashes as llseek() offset (for directories) */
  122. #define FMODE_64BITHASH ((__force fmode_t)0x400)
  123. /*
  124. * Don't update ctime and mtime.
  125. *
  126. * Currently a special hack for the XFS open_by_handle ioctl, but we'll
  127. * hopefully graduate it to a proper O_CMTIME flag supported by open(2) soon.
  128. */
  129. #define FMODE_NOCMTIME ((__force fmode_t)0x800)
  130. /* Expect random access pattern */
  131. #define FMODE_RANDOM ((__force fmode_t)0x1000)
  132. /* File is huge (eg. /dev/mem): treat loff_t as unsigned */
  133. #define FMODE_UNSIGNED_OFFSET ((__force fmode_t)0x2000)
  134. /* File is opened with O_PATH; almost nothing can be done with it */
  135. #define FMODE_PATH ((__force fmode_t)0x4000)
  136. /* File needs atomic accesses to f_pos */
  137. #define FMODE_ATOMIC_POS ((__force fmode_t)0x8000)
  138. /* Write access to underlying fs */
  139. #define FMODE_WRITER ((__force fmode_t)0x10000)
  140. /* Has read method(s) */
  141. #define FMODE_CAN_READ ((__force fmode_t)0x20000)
  142. /* Has write method(s) */
  143. #define FMODE_CAN_WRITE ((__force fmode_t)0x40000)
  144. #define FMODE_OPENED ((__force fmode_t)0x80000)
  145. #define FMODE_CREATED ((__force fmode_t)0x100000)
  146. /* File is stream-like */
  147. #define FMODE_STREAM ((__force fmode_t)0x200000)
  148. /* File supports DIRECT IO */
  149. #define FMODE_CAN_ODIRECT ((__force fmode_t)0x400000)
  150. #define FMODE_NOREUSE ((__force fmode_t)0x800000)
  151. /* File was opened by fanotify and shouldn't generate fanotify events */
  152. #define FMODE_NONOTIFY ((__force fmode_t)0x4000000)
  153. /* File is capable of returning -EAGAIN if I/O will block */
  154. #define FMODE_NOWAIT ((__force fmode_t)0x8000000)
  155. /* File represents mount that needs unmounting */
  156. #define FMODE_NEED_UNMOUNT ((__force fmode_t)0x10000000)
  157. /* File does not contribute to nr_files count */
  158. #define FMODE_NOACCOUNT ((__force fmode_t)0x20000000)
  159. /* File supports async buffered reads */
  160. #define FMODE_BUF_RASYNC ((__force fmode_t)0x40000000)
  161. /* File supports async nowait buffered writes */
  162. #define FMODE_BUF_WASYNC ((__force fmode_t)0x80000000)
  163. /*
  164. * Attribute flags. These should be or-ed together to figure out what
  165. * has been changed!
  166. */
  167. #define ATTR_MODE (1 << 0)
  168. #define ATTR_UID (1 << 1)
  169. #define ATTR_GID (1 << 2)
  170. #define ATTR_SIZE (1 << 3)
  171. #define ATTR_ATIME (1 << 4)
  172. #define ATTR_MTIME (1 << 5)
  173. #define ATTR_CTIME (1 << 6)
  174. #define ATTR_ATIME_SET (1 << 7)
  175. #define ATTR_MTIME_SET (1 << 8)
  176. #define ATTR_FORCE (1 << 9) /* Not a change, but a change it */
  177. #define ATTR_KILL_SUID (1 << 11)
  178. #define ATTR_KILL_SGID (1 << 12)
  179. #define ATTR_FILE (1 << 13)
  180. #define ATTR_KILL_PRIV (1 << 14)
  181. #define ATTR_OPEN (1 << 15) /* Truncating from open(O_TRUNC) */
  182. #define ATTR_TIMES_SET (1 << 16)
  183. #define ATTR_TOUCH (1 << 17)
  184. /*
  185. * Whiteout is represented by a char device. The following constants define the
  186. * mode and device number to use.
  187. */
  188. #define WHITEOUT_MODE 0
  189. #define WHITEOUT_DEV 0
  190. /*
  191. * This is the Inode Attributes structure, used for notify_change(). It
  192. * uses the above definitions as flags, to know which values have changed.
  193. * Also, in this manner, a Filesystem can look at only the values it cares
  194. * about. Basically, these are the attributes that the VFS layer can
  195. * request to change from the FS layer.
  196. *
  197. * Derek Atkins <[email protected]> 94-10-20
  198. */
  199. struct iattr {
  200. unsigned int ia_valid;
  201. umode_t ia_mode;
  202. /*
  203. * The two anonymous unions wrap structures with the same member.
  204. *
  205. * Filesystems raising FS_ALLOW_IDMAP need to use ia_vfs{g,u}id which
  206. * are a dedicated type requiring the filesystem to use the dedicated
  207. * helpers. Other filesystem can continue to use ia_{g,u}id until they
  208. * have been ported.
  209. *
  210. * They always contain the same value. In other words FS_ALLOW_IDMAP
  211. * pass down the same value on idmapped mounts as they would on regular
  212. * mounts.
  213. */
  214. union {
  215. kuid_t ia_uid;
  216. vfsuid_t ia_vfsuid;
  217. };
  218. union {
  219. kgid_t ia_gid;
  220. vfsgid_t ia_vfsgid;
  221. };
  222. loff_t ia_size;
  223. struct timespec64 ia_atime;
  224. struct timespec64 ia_mtime;
  225. struct timespec64 ia_ctime;
  226. /*
  227. * Not an attribute, but an auxiliary info for filesystems wanting to
  228. * implement an ftruncate() like method. NOTE: filesystem should
  229. * check for (ia_valid & ATTR_FILE), and not for (ia_file != NULL).
  230. */
  231. struct file *ia_file;
  232. };
  233. /*
  234. * Includes for diskquotas.
  235. */
  236. #include <linux/quota.h>
  237. /*
  238. * Maximum number of layers of fs stack. Needs to be limited to
  239. * prevent kernel stack overflow
  240. */
  241. #define FILESYSTEM_MAX_STACK_DEPTH 2
  242. /**
  243. * enum positive_aop_returns - aop return codes with specific semantics
  244. *
  245. * @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
  246. * completed, that the page is still locked, and
  247. * should be considered active. The VM uses this hint
  248. * to return the page to the active list -- it won't
  249. * be a candidate for writeback again in the near
  250. * future. Other callers must be careful to unlock
  251. * the page if they get this return. Returned by
  252. * writepage();
  253. *
  254. * @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
  255. * unlocked it and the page might have been truncated.
  256. * The caller should back up to acquiring a new page and
  257. * trying again. The aop will be taking reasonable
  258. * precautions not to livelock. If the caller held a page
  259. * reference, it should drop it before retrying. Returned
  260. * by read_folio().
  261. *
  262. * address_space_operation functions return these large constants to indicate
  263. * special semantics to the caller. These are much larger than the bytes in a
  264. * page to allow for functions that return the number of bytes operated on in a
  265. * given page.
  266. */
  267. enum positive_aop_returns {
  268. AOP_WRITEPAGE_ACTIVATE = 0x80000,
  269. AOP_TRUNCATED_PAGE = 0x80001,
  270. };
  271. /*
  272. * oh the beauties of C type declarations.
  273. */
  274. struct page;
  275. struct address_space;
  276. struct writeback_control;
  277. struct readahead_control;
  278. /*
  279. * Write life time hint values.
  280. * Stored in struct inode as u8.
  281. */
  282. enum rw_hint {
  283. WRITE_LIFE_NOT_SET = 0,
  284. WRITE_LIFE_NONE = RWH_WRITE_LIFE_NONE,
  285. WRITE_LIFE_SHORT = RWH_WRITE_LIFE_SHORT,
  286. WRITE_LIFE_MEDIUM = RWH_WRITE_LIFE_MEDIUM,
  287. WRITE_LIFE_LONG = RWH_WRITE_LIFE_LONG,
  288. WRITE_LIFE_EXTREME = RWH_WRITE_LIFE_EXTREME,
  289. };
  290. /* Match RWF_* bits to IOCB bits */
  291. #define IOCB_HIPRI (__force int) RWF_HIPRI
  292. #define IOCB_DSYNC (__force int) RWF_DSYNC
  293. #define IOCB_SYNC (__force int) RWF_SYNC
  294. #define IOCB_NOWAIT (__force int) RWF_NOWAIT
  295. #define IOCB_APPEND (__force int) RWF_APPEND
  296. /* non-RWF related bits - start at 16 */
  297. #define IOCB_EVENTFD (1 << 16)
  298. #define IOCB_DIRECT (1 << 17)
  299. #define IOCB_WRITE (1 << 18)
  300. /* iocb->ki_waitq is valid */
  301. #define IOCB_WAITQ (1 << 19)
  302. #define IOCB_NOIO (1 << 20)
  303. /* can use bio alloc cache */
  304. #define IOCB_ALLOC_CACHE (1 << 21)
  305. struct kiocb {
  306. struct file *ki_filp;
  307. loff_t ki_pos;
  308. void (*ki_complete)(struct kiocb *iocb, long ret);
  309. void *private;
  310. int ki_flags;
  311. u16 ki_ioprio; /* See linux/ioprio.h */
  312. struct wait_page_queue *ki_waitq; /* for async buffered IO */
  313. };
  314. static inline bool is_sync_kiocb(struct kiocb *kiocb)
  315. {
  316. return kiocb->ki_complete == NULL;
  317. }
  318. struct address_space_operations {
  319. int (*writepage)(struct page *page, struct writeback_control *wbc);
  320. int (*read_folio)(struct file *, struct folio *);
  321. /* Write back some dirty pages from this mapping. */
  322. int (*writepages)(struct address_space *, struct writeback_control *);
  323. /* Mark a folio dirty. Return true if this dirtied it */
  324. bool (*dirty_folio)(struct address_space *, struct folio *);
  325. void (*readahead)(struct readahead_control *);
  326. int (*write_begin)(struct file *, struct address_space *mapping,
  327. loff_t pos, unsigned len,
  328. struct page **pagep, void **fsdata);
  329. int (*write_end)(struct file *, struct address_space *mapping,
  330. loff_t pos, unsigned len, unsigned copied,
  331. struct page *page, void *fsdata);
  332. /* Unfortunately this kludge is needed for FIBMAP. Don't use it */
  333. sector_t (*bmap)(struct address_space *, sector_t);
  334. void (*invalidate_folio) (struct folio *, size_t offset, size_t len);
  335. bool (*release_folio)(struct folio *, gfp_t);
  336. void (*free_folio)(struct folio *folio);
  337. ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
  338. /*
  339. * migrate the contents of a folio to the specified target. If
  340. * migrate_mode is MIGRATE_ASYNC, it must not block.
  341. */
  342. int (*migrate_folio)(struct address_space *, struct folio *dst,
  343. struct folio *src, enum migrate_mode);
  344. int (*launder_folio)(struct folio *);
  345. bool (*is_partially_uptodate) (struct folio *, size_t from,
  346. size_t count);
  347. void (*is_dirty_writeback) (struct folio *, bool *dirty, bool *wb);
  348. int (*error_remove_page)(struct address_space *, struct page *);
  349. /* swapfile support */
  350. int (*swap_activate)(struct swap_info_struct *sis, struct file *file,
  351. sector_t *span);
  352. void (*swap_deactivate)(struct file *file);
  353. int (*swap_rw)(struct kiocb *iocb, struct iov_iter *iter);
  354. };
  355. extern const struct address_space_operations empty_aops;
  356. /**
  357. * struct address_space - Contents of a cacheable, mappable object.
  358. * @host: Owner, either the inode or the block_device.
  359. * @i_pages: Cached pages.
  360. * @invalidate_lock: Guards coherency between page cache contents and
  361. * file offset->disk block mappings in the filesystem during invalidates.
  362. * It is also used to block modification of page cache contents through
  363. * memory mappings.
  364. * @gfp_mask: Memory allocation flags to use for allocating pages.
  365. * @i_mmap_writable: Number of VM_SHARED mappings.
  366. * @nr_thps: Number of THPs in the pagecache (non-shmem only).
  367. * @i_mmap: Tree of private and shared mappings.
  368. * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
  369. * @nrpages: Number of page entries, protected by the i_pages lock.
  370. * @writeback_index: Writeback starts here.
  371. * @a_ops: Methods.
  372. * @flags: Error bits and flags (AS_*).
  373. * @wb_err: The most recent error which has occurred.
  374. * @private_lock: For use by the owner of the address_space.
  375. * @private_list: For use by the owner of the address_space.
  376. * @private_data: For use by the owner of the address_space.
  377. */
  378. struct address_space {
  379. struct inode *host;
  380. struct xarray i_pages;
  381. struct rw_semaphore invalidate_lock;
  382. gfp_t gfp_mask;
  383. atomic_t i_mmap_writable;
  384. #ifdef CONFIG_READ_ONLY_THP_FOR_FS
  385. /* number of thp, only for non-shmem files */
  386. atomic_t nr_thps;
  387. #endif
  388. struct rb_root_cached i_mmap;
  389. struct rw_semaphore i_mmap_rwsem;
  390. unsigned long nrpages;
  391. pgoff_t writeback_index;
  392. const struct address_space_operations *a_ops;
  393. unsigned long flags;
  394. errseq_t wb_err;
  395. spinlock_t private_lock;
  396. struct list_head private_list;
  397. void *private_data;
  398. } __attribute__((aligned(sizeof(long)))) __randomize_layout;
  399. /*
  400. * On most architectures that alignment is already the case; but
  401. * must be enforced here for CRIS, to let the least significant bit
  402. * of struct page's "mapping" pointer be used for PAGE_MAPPING_ANON.
  403. */
  404. /* XArray tags, for tagging dirty and writeback pages in the pagecache. */
  405. #define PAGECACHE_TAG_DIRTY XA_MARK_0
  406. #define PAGECACHE_TAG_WRITEBACK XA_MARK_1
  407. #define PAGECACHE_TAG_TOWRITE XA_MARK_2
  408. /*
  409. * Returns true if any of the pages in the mapping are marked with the tag.
  410. */
  411. static inline bool mapping_tagged(struct address_space *mapping, xa_mark_t tag)
  412. {
  413. return xa_marked(&mapping->i_pages, tag);
  414. }
  415. static inline void i_mmap_lock_write(struct address_space *mapping)
  416. {
  417. down_write(&mapping->i_mmap_rwsem);
  418. }
  419. static inline int i_mmap_trylock_write(struct address_space *mapping)
  420. {
  421. return down_write_trylock(&mapping->i_mmap_rwsem);
  422. }
  423. static inline void i_mmap_unlock_write(struct address_space *mapping)
  424. {
  425. up_write(&mapping->i_mmap_rwsem);
  426. }
  427. static inline int i_mmap_trylock_read(struct address_space *mapping)
  428. {
  429. return down_read_trylock(&mapping->i_mmap_rwsem);
  430. }
  431. static inline void i_mmap_lock_read(struct address_space *mapping)
  432. {
  433. down_read(&mapping->i_mmap_rwsem);
  434. }
  435. static inline void i_mmap_unlock_read(struct address_space *mapping)
  436. {
  437. up_read(&mapping->i_mmap_rwsem);
  438. }
  439. static inline void i_mmap_assert_locked(struct address_space *mapping)
  440. {
  441. lockdep_assert_held(&mapping->i_mmap_rwsem);
  442. }
  443. static inline void i_mmap_assert_write_locked(struct address_space *mapping)
  444. {
  445. lockdep_assert_held_write(&mapping->i_mmap_rwsem);
  446. }
  447. /*
  448. * Might pages of this file be mapped into userspace?
  449. */
  450. static inline int mapping_mapped(struct address_space *mapping)
  451. {
  452. return !RB_EMPTY_ROOT(&mapping->i_mmap.rb_root);
  453. }
  454. /*
  455. * Might pages of this file have been modified in userspace?
  456. * Note that i_mmap_writable counts all VM_SHARED vmas: do_mmap
  457. * marks vma as VM_SHARED if it is shared, and the file was opened for
  458. * writing i.e. vma may be mprotected writable even if now readonly.
  459. *
  460. * If i_mmap_writable is negative, no new writable mappings are allowed. You
  461. * can only deny writable mappings, if none exists right now.
  462. */
  463. static inline int mapping_writably_mapped(struct address_space *mapping)
  464. {
  465. return atomic_read(&mapping->i_mmap_writable) > 0;
  466. }
  467. static inline int mapping_map_writable(struct address_space *mapping)
  468. {
  469. return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
  470. 0 : -EPERM;
  471. }
  472. static inline void mapping_unmap_writable(struct address_space *mapping)
  473. {
  474. atomic_dec(&mapping->i_mmap_writable);
  475. }
  476. static inline int mapping_deny_writable(struct address_space *mapping)
  477. {
  478. return atomic_dec_unless_positive(&mapping->i_mmap_writable) ?
  479. 0 : -EBUSY;
  480. }
  481. static inline void mapping_allow_writable(struct address_space *mapping)
  482. {
  483. atomic_inc(&mapping->i_mmap_writable);
  484. }
  485. /*
  486. * Use sequence counter to get consistent i_size on 32-bit processors.
  487. */
  488. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  489. #include <linux/seqlock.h>
  490. #define __NEED_I_SIZE_ORDERED
  491. #define i_size_ordered_init(inode) seqcount_init(&inode->i_size_seqcount)
  492. #else
  493. #define i_size_ordered_init(inode) do { } while (0)
  494. #endif
  495. struct posix_acl;
  496. #define ACL_NOT_CACHED ((void *)(-1))
  497. /*
  498. * ACL_DONT_CACHE is for stacked filesystems, that rely on underlying fs to
  499. * cache the ACL. This also means that ->get_acl() can be called in RCU mode
  500. * with the LOOKUP_RCU flag.
  501. */
  502. #define ACL_DONT_CACHE ((void *)(-3))
  503. static inline struct posix_acl *
  504. uncached_acl_sentinel(struct task_struct *task)
  505. {
  506. return (void *)task + 1;
  507. }
  508. static inline bool
  509. is_uncached_acl(struct posix_acl *acl)
  510. {
  511. return (long)acl & 1;
  512. }
  513. #define IOP_FASTPERM 0x0001
  514. #define IOP_LOOKUP 0x0002
  515. #define IOP_NOFOLLOW 0x0004
  516. #define IOP_XATTR 0x0008
  517. #define IOP_DEFAULT_READLINK 0x0010
  518. struct fsnotify_mark_connector;
  519. /*
  520. * Keep mostly read-only and often accessed (especially for
  521. * the RCU path lookup and 'stat' data) fields at the beginning
  522. * of the 'struct inode'
  523. */
  524. struct inode {
  525. umode_t i_mode;
  526. unsigned short i_opflags;
  527. kuid_t i_uid;
  528. kgid_t i_gid;
  529. unsigned int i_flags;
  530. #ifdef CONFIG_FS_POSIX_ACL
  531. struct posix_acl *i_acl;
  532. struct posix_acl *i_default_acl;
  533. #endif
  534. const struct inode_operations *i_op;
  535. struct super_block *i_sb;
  536. struct address_space *i_mapping;
  537. #ifdef CONFIG_SECURITY
  538. void *i_security;
  539. #endif
  540. /* Stat data, not accessed from path walking */
  541. unsigned long i_ino;
  542. /*
  543. * Filesystems may only read i_nlink directly. They shall use the
  544. * following functions for modification:
  545. *
  546. * (set|clear|inc|drop)_nlink
  547. * inode_(inc|dec)_link_count
  548. */
  549. union {
  550. const unsigned int i_nlink;
  551. unsigned int __i_nlink;
  552. };
  553. dev_t i_rdev;
  554. loff_t i_size;
  555. struct timespec64 i_atime;
  556. struct timespec64 i_mtime;
  557. struct timespec64 i_ctime;
  558. spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
  559. unsigned short i_bytes;
  560. u8 i_blkbits;
  561. u8 i_write_hint;
  562. blkcnt_t i_blocks;
  563. #ifdef __NEED_I_SIZE_ORDERED
  564. seqcount_t i_size_seqcount;
  565. #endif
  566. /* Misc */
  567. unsigned long i_state;
  568. struct rw_semaphore i_rwsem;
  569. unsigned long dirtied_when; /* jiffies of first dirtying */
  570. unsigned long dirtied_time_when;
  571. struct hlist_node i_hash;
  572. struct list_head i_io_list; /* backing dev IO list */
  573. #ifdef CONFIG_CGROUP_WRITEBACK
  574. struct bdi_writeback *i_wb; /* the associated cgroup wb */
  575. /* foreign inode detection, see wbc_detach_inode() */
  576. int i_wb_frn_winner;
  577. u16 i_wb_frn_avg_time;
  578. u16 i_wb_frn_history;
  579. #endif
  580. struct list_head i_lru; /* inode LRU list */
  581. struct list_head i_sb_list;
  582. struct list_head i_wb_list; /* backing dev writeback list */
  583. union {
  584. struct hlist_head i_dentry;
  585. struct rcu_head i_rcu;
  586. };
  587. atomic64_t i_version;
  588. atomic64_t i_sequence; /* see futex */
  589. atomic_t i_count;
  590. atomic_t i_dio_count;
  591. atomic_t i_writecount;
  592. #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
  593. atomic_t i_readcount; /* struct files open RO */
  594. #endif
  595. union {
  596. const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
  597. void (*free_inode)(struct inode *);
  598. };
  599. struct file_lock_context *i_flctx;
  600. struct address_space i_data;
  601. struct list_head i_devices;
  602. union {
  603. struct pipe_inode_info *i_pipe;
  604. struct cdev *i_cdev;
  605. char *i_link;
  606. unsigned i_dir_seq;
  607. };
  608. __u32 i_generation;
  609. #ifdef CONFIG_FSNOTIFY
  610. __u32 i_fsnotify_mask; /* all events this inode cares about */
  611. struct fsnotify_mark_connector __rcu *i_fsnotify_marks;
  612. #endif
  613. #ifdef CONFIG_FS_ENCRYPTION
  614. struct fscrypt_info *i_crypt_info;
  615. #endif
  616. #ifdef CONFIG_FS_VERITY
  617. struct fsverity_info *i_verity_info;
  618. #endif
  619. void *i_private; /* fs or device private pointer */
  620. } __randomize_layout;
  621. struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode);
  622. static inline unsigned int i_blocksize(const struct inode *node)
  623. {
  624. return (1 << node->i_blkbits);
  625. }
  626. static inline int inode_unhashed(struct inode *inode)
  627. {
  628. return hlist_unhashed(&inode->i_hash);
  629. }
  630. /*
  631. * __mark_inode_dirty expects inodes to be hashed. Since we don't
  632. * want special inodes in the fileset inode space, we make them
  633. * appear hashed, but do not put on any lists. hlist_del()
  634. * will work fine and require no locking.
  635. */
  636. static inline void inode_fake_hash(struct inode *inode)
  637. {
  638. hlist_add_fake(&inode->i_hash);
  639. }
  640. /*
  641. * inode->i_mutex nesting subclasses for the lock validator:
  642. *
  643. * 0: the object of the current VFS operation
  644. * 1: parent
  645. * 2: child/target
  646. * 3: xattr
  647. * 4: second non-directory
  648. * 5: second parent (when locking independent directories in rename)
  649. *
  650. * I_MUTEX_NONDIR2 is for certain operations (such as rename) which lock two
  651. * non-directories at once.
  652. *
  653. * The locking order between these classes is
  654. * parent[2] -> child -> grandchild -> normal -> xattr -> second non-directory
  655. */
  656. enum inode_i_mutex_lock_class
  657. {
  658. I_MUTEX_NORMAL,
  659. I_MUTEX_PARENT,
  660. I_MUTEX_CHILD,
  661. I_MUTEX_XATTR,
  662. I_MUTEX_NONDIR2,
  663. I_MUTEX_PARENT2,
  664. };
  665. static inline void inode_lock(struct inode *inode)
  666. {
  667. down_write(&inode->i_rwsem);
  668. }
  669. static inline void inode_unlock(struct inode *inode)
  670. {
  671. up_write(&inode->i_rwsem);
  672. }
  673. static inline void inode_lock_shared(struct inode *inode)
  674. {
  675. down_read(&inode->i_rwsem);
  676. }
  677. static inline void inode_unlock_shared(struct inode *inode)
  678. {
  679. up_read(&inode->i_rwsem);
  680. }
  681. static inline int inode_trylock(struct inode *inode)
  682. {
  683. return down_write_trylock(&inode->i_rwsem);
  684. }
  685. static inline int inode_trylock_shared(struct inode *inode)
  686. {
  687. return down_read_trylock(&inode->i_rwsem);
  688. }
  689. static inline int inode_is_locked(struct inode *inode)
  690. {
  691. return rwsem_is_locked(&inode->i_rwsem);
  692. }
  693. static inline void inode_lock_nested(struct inode *inode, unsigned subclass)
  694. {
  695. down_write_nested(&inode->i_rwsem, subclass);
  696. }
  697. static inline void inode_lock_shared_nested(struct inode *inode, unsigned subclass)
  698. {
  699. down_read_nested(&inode->i_rwsem, subclass);
  700. }
  701. static inline void filemap_invalidate_lock(struct address_space *mapping)
  702. {
  703. down_write(&mapping->invalidate_lock);
  704. }
  705. static inline void filemap_invalidate_unlock(struct address_space *mapping)
  706. {
  707. up_write(&mapping->invalidate_lock);
  708. }
  709. static inline void filemap_invalidate_lock_shared(struct address_space *mapping)
  710. {
  711. down_read(&mapping->invalidate_lock);
  712. }
  713. static inline int filemap_invalidate_trylock_shared(
  714. struct address_space *mapping)
  715. {
  716. return down_read_trylock(&mapping->invalidate_lock);
  717. }
  718. static inline void filemap_invalidate_unlock_shared(
  719. struct address_space *mapping)
  720. {
  721. up_read(&mapping->invalidate_lock);
  722. }
  723. void lock_two_nondirectories(struct inode *, struct inode*);
  724. void unlock_two_nondirectories(struct inode *, struct inode*);
  725. void filemap_invalidate_lock_two(struct address_space *mapping1,
  726. struct address_space *mapping2);
  727. void filemap_invalidate_unlock_two(struct address_space *mapping1,
  728. struct address_space *mapping2);
  729. /*
  730. * NOTE: in a 32bit arch with a preemptable kernel and
  731. * an UP compile the i_size_read/write must be atomic
  732. * with respect to the local cpu (unlike with preempt disabled),
  733. * but they don't need to be atomic with respect to other cpus like in
  734. * true SMP (so they need either to either locally disable irq around
  735. * the read or for example on x86 they can be still implemented as a
  736. * cmpxchg8b without the need of the lock prefix). For SMP compiles
  737. * and 64bit archs it makes no difference if preempt is enabled or not.
  738. */
  739. static inline loff_t i_size_read(const struct inode *inode)
  740. {
  741. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  742. loff_t i_size;
  743. unsigned int seq;
  744. do {
  745. seq = read_seqcount_begin(&inode->i_size_seqcount);
  746. i_size = inode->i_size;
  747. } while (read_seqcount_retry(&inode->i_size_seqcount, seq));
  748. return i_size;
  749. #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
  750. loff_t i_size;
  751. preempt_disable();
  752. i_size = inode->i_size;
  753. preempt_enable();
  754. return i_size;
  755. #else
  756. return inode->i_size;
  757. #endif
  758. }
  759. /*
  760. * NOTE: unlike i_size_read(), i_size_write() does need locking around it
  761. * (normally i_mutex), otherwise on 32bit/SMP an update of i_size_seqcount
  762. * can be lost, resulting in subsequent i_size_read() calls spinning forever.
  763. */
  764. static inline void i_size_write(struct inode *inode, loff_t i_size)
  765. {
  766. #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
  767. preempt_disable();
  768. write_seqcount_begin(&inode->i_size_seqcount);
  769. inode->i_size = i_size;
  770. write_seqcount_end(&inode->i_size_seqcount);
  771. preempt_enable();
  772. #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
  773. preempt_disable();
  774. inode->i_size = i_size;
  775. preempt_enable();
  776. #else
  777. inode->i_size = i_size;
  778. #endif
  779. }
  780. static inline unsigned iminor(const struct inode *inode)
  781. {
  782. return MINOR(inode->i_rdev);
  783. }
  784. static inline unsigned imajor(const struct inode *inode)
  785. {
  786. return MAJOR(inode->i_rdev);
  787. }
  788. struct fown_struct {
  789. rwlock_t lock; /* protects pid, uid, euid fields */
  790. struct pid *pid; /* pid or -pgrp where SIGIO should be sent */
  791. enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
  792. kuid_t uid, euid; /* uid/euid of process setting the owner */
  793. int signum; /* posix.1b rt signal to be delivered on IO */
  794. };
  795. /**
  796. * struct file_ra_state - Track a file's readahead state.
  797. * @start: Where the most recent readahead started.
  798. * @size: Number of pages read in the most recent readahead.
  799. * @async_size: Numer of pages that were/are not needed immediately
  800. * and so were/are genuinely "ahead". Start next readahead when
  801. * the first of these pages is accessed.
  802. * @ra_pages: Maximum size of a readahead request, copied from the bdi.
  803. * @mmap_miss: How many mmap accesses missed in the page cache.
  804. * @prev_pos: The last byte in the most recent read request.
  805. *
  806. * When this structure is passed to ->readahead(), the "most recent"
  807. * readahead means the current readahead.
  808. */
  809. struct file_ra_state {
  810. pgoff_t start;
  811. unsigned int size;
  812. unsigned int async_size;
  813. unsigned int ra_pages;
  814. unsigned int mmap_miss;
  815. loff_t prev_pos;
  816. };
  817. /*
  818. * Check if @index falls in the readahead windows.
  819. */
  820. static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
  821. {
  822. return (index >= ra->start &&
  823. index < ra->start + ra->size);
  824. }
  825. struct file {
  826. union {
  827. struct llist_node f_llist;
  828. struct rcu_head f_rcuhead;
  829. unsigned int f_iocb_flags;
  830. };
  831. struct path f_path;
  832. struct inode *f_inode; /* cached value */
  833. const struct file_operations *f_op;
  834. /*
  835. * Protects f_ep, f_flags.
  836. * Must not be taken from IRQ context.
  837. */
  838. spinlock_t f_lock;
  839. atomic_long_t f_count;
  840. unsigned int f_flags;
  841. fmode_t f_mode;
  842. struct mutex f_pos_lock;
  843. loff_t f_pos;
  844. struct fown_struct f_owner;
  845. const struct cred *f_cred;
  846. struct file_ra_state f_ra;
  847. u64 f_version;
  848. #ifdef CONFIG_SECURITY
  849. void *f_security;
  850. #endif
  851. /* needed for tty driver, and maybe others */
  852. void *private_data;
  853. #ifdef CONFIG_EPOLL
  854. /* Used by fs/eventpoll.c to link all the hooks to this file */
  855. struct hlist_head *f_ep;
  856. #endif /* #ifdef CONFIG_EPOLL */
  857. struct address_space *f_mapping;
  858. errseq_t f_wb_err;
  859. errseq_t f_sb_err; /* for syncfs */
  860. ANDROID_KABI_RESERVE(1);
  861. ANDROID_KABI_RESERVE(2);
  862. } __randomize_layout
  863. __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
  864. struct file_handle {
  865. __u32 handle_bytes;
  866. int handle_type;
  867. /* file identifier */
  868. unsigned char f_handle[];
  869. };
  870. static inline struct file *get_file(struct file *f)
  871. {
  872. atomic_long_inc(&f->f_count);
  873. return f;
  874. }
  875. #define get_file_rcu(x) atomic_long_inc_not_zero(&(x)->f_count)
  876. #define file_count(x) atomic_long_read(&(x)->f_count)
  877. #define MAX_NON_LFS ((1UL<<31) - 1)
  878. /* Page cache limit. The filesystems should put that into their s_maxbytes
  879. limits, otherwise bad things can happen in VM. */
  880. #if BITS_PER_LONG==32
  881. #define MAX_LFS_FILESIZE ((loff_t)ULONG_MAX << PAGE_SHIFT)
  882. #elif BITS_PER_LONG==64
  883. #define MAX_LFS_FILESIZE ((loff_t)LLONG_MAX)
  884. #endif
  885. #define FL_POSIX 1
  886. #define FL_FLOCK 2
  887. #define FL_DELEG 4 /* NFSv4 delegation */
  888. #define FL_ACCESS 8 /* not trying to lock, just looking */
  889. #define FL_EXISTS 16 /* when unlocking, test for existence */
  890. #define FL_LEASE 32 /* lease held on this file */
  891. #define FL_CLOSE 64 /* unlock on close */
  892. #define FL_SLEEP 128 /* A blocking lock */
  893. #define FL_DOWNGRADE_PENDING 256 /* Lease is being downgraded */
  894. #define FL_UNLOCK_PENDING 512 /* Lease is being broken */
  895. #define FL_OFDLCK 1024 /* lock is "owned" by struct file */
  896. #define FL_LAYOUT 2048 /* outstanding pNFS layout */
  897. #define FL_RECLAIM 4096 /* reclaiming from a reboot server */
  898. #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
  899. /*
  900. * Special return value from posix_lock_file() and vfs_lock_file() for
  901. * asynchronous locking.
  902. */
  903. #define FILE_LOCK_DEFERRED 1
  904. /* legacy typedef, should eventually be removed */
  905. typedef void *fl_owner_t;
  906. struct file_lock;
  907. struct file_lock_operations {
  908. void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
  909. void (*fl_release_private)(struct file_lock *);
  910. };
  911. struct lock_manager_operations {
  912. void *lm_mod_owner;
  913. fl_owner_t (*lm_get_owner)(fl_owner_t);
  914. void (*lm_put_owner)(fl_owner_t);
  915. void (*lm_notify)(struct file_lock *); /* unblock callback */
  916. int (*lm_grant)(struct file_lock *, int);
  917. bool (*lm_break)(struct file_lock *);
  918. int (*lm_change)(struct file_lock *, int, struct list_head *);
  919. void (*lm_setup)(struct file_lock *, void **);
  920. bool (*lm_breaker_owns_lease)(struct file_lock *);
  921. bool (*lm_lock_expirable)(struct file_lock *cfl);
  922. void (*lm_expire_lock)(void);
  923. };
  924. struct lock_manager {
  925. struct list_head list;
  926. /*
  927. * NFSv4 and up also want opens blocked during the grace period;
  928. * NLM doesn't care:
  929. */
  930. bool block_opens;
  931. };
  932. struct net;
  933. void locks_start_grace(struct net *, struct lock_manager *);
  934. void locks_end_grace(struct lock_manager *);
  935. bool locks_in_grace(struct net *);
  936. bool opens_in_grace(struct net *);
  937. /* that will die - we need it for nfs_lock_info */
  938. #include <linux/nfs_fs_i.h>
  939. /*
  940. * struct file_lock represents a generic "file lock". It's used to represent
  941. * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
  942. * note that the same struct is used to represent both a request for a lock and
  943. * the lock itself, but the same object is never used for both.
  944. *
  945. * FIXME: should we create a separate "struct lock_request" to help distinguish
  946. * these two uses?
  947. *
  948. * The varous i_flctx lists are ordered by:
  949. *
  950. * 1) lock owner
  951. * 2) lock range start
  952. * 3) lock range end
  953. *
  954. * Obviously, the last two criteria only matter for POSIX locks.
  955. */
  956. struct file_lock {
  957. struct file_lock *fl_blocker; /* The lock, that is blocking us */
  958. struct list_head fl_list; /* link into file_lock_context */
  959. struct hlist_node fl_link; /* node in global lists */
  960. struct list_head fl_blocked_requests; /* list of requests with
  961. * ->fl_blocker pointing here
  962. */
  963. struct list_head fl_blocked_member; /* node in
  964. * ->fl_blocker->fl_blocked_requests
  965. */
  966. fl_owner_t fl_owner;
  967. unsigned int fl_flags;
  968. unsigned char fl_type;
  969. unsigned int fl_pid;
  970. int fl_link_cpu; /* what cpu's list is this on? */
  971. wait_queue_head_t fl_wait;
  972. struct file *fl_file;
  973. loff_t fl_start;
  974. loff_t fl_end;
  975. struct fasync_struct * fl_fasync; /* for lease break notifications */
  976. /* for lease breaks: */
  977. unsigned long fl_break_time;
  978. unsigned long fl_downgrade_time;
  979. const struct file_lock_operations *fl_ops; /* Callbacks for filesystems */
  980. const struct lock_manager_operations *fl_lmops; /* Callbacks for lockmanagers */
  981. union {
  982. struct nfs_lock_info nfs_fl;
  983. struct nfs4_lock_info nfs4_fl;
  984. struct {
  985. struct list_head link; /* link in AFS vnode's pending_locks list */
  986. int state; /* state of grant or error if -ve */
  987. unsigned int debug_id;
  988. } afs;
  989. } fl_u;
  990. } __randomize_layout;
  991. struct file_lock_context {
  992. spinlock_t flc_lock;
  993. struct list_head flc_flock;
  994. struct list_head flc_posix;
  995. struct list_head flc_lease;
  996. };
  997. /* The following constant reflects the upper bound of the file/locking space */
  998. #ifndef OFFSET_MAX
  999. #define INT_LIMIT(x) (~((x)1 << (sizeof(x)*8 - 1)))
  1000. #define OFFSET_MAX INT_LIMIT(loff_t)
  1001. #define OFFT_OFFSET_MAX INT_LIMIT(off_t)
  1002. #endif
  1003. extern void send_sigio(struct fown_struct *fown, int fd, int band);
  1004. #define locks_inode(f) file_inode(f)
  1005. #ifdef CONFIG_FILE_LOCKING
  1006. extern int fcntl_getlk(struct file *, unsigned int, struct flock *);
  1007. extern int fcntl_setlk(unsigned int, struct file *, unsigned int,
  1008. struct flock *);
  1009. #if BITS_PER_LONG == 32
  1010. extern int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
  1011. extern int fcntl_setlk64(unsigned int, struct file *, unsigned int,
  1012. struct flock64 *);
  1013. #endif
  1014. extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
  1015. extern int fcntl_getlease(struct file *filp);
  1016. /* fs/locks.c */
  1017. void locks_free_lock_context(struct inode *inode);
  1018. void locks_free_lock(struct file_lock *fl);
  1019. extern void locks_init_lock(struct file_lock *);
  1020. extern struct file_lock * locks_alloc_lock(void);
  1021. extern void locks_copy_lock(struct file_lock *, struct file_lock *);
  1022. extern void locks_copy_conflock(struct file_lock *, struct file_lock *);
  1023. extern void locks_remove_posix(struct file *, fl_owner_t);
  1024. extern void locks_remove_file(struct file *);
  1025. extern void locks_release_private(struct file_lock *);
  1026. extern void posix_test_lock(struct file *, struct file_lock *);
  1027. extern int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
  1028. extern int locks_delete_block(struct file_lock *);
  1029. extern int vfs_test_lock(struct file *, struct file_lock *);
  1030. extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
  1031. extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
  1032. bool vfs_inode_has_locks(struct inode *inode);
  1033. extern int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
  1034. extern int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
  1035. extern void lease_get_mtime(struct inode *, struct timespec64 *time);
  1036. extern int generic_setlease(struct file *, long, struct file_lock **, void **priv);
  1037. extern int vfs_setlease(struct file *, long, struct file_lock **, void **);
  1038. extern int lease_modify(struct file_lock *, int, struct list_head *);
  1039. struct notifier_block;
  1040. extern int lease_register_notifier(struct notifier_block *);
  1041. extern void lease_unregister_notifier(struct notifier_block *);
  1042. struct files_struct;
  1043. extern void show_fd_locks(struct seq_file *f,
  1044. struct file *filp, struct files_struct *files);
  1045. extern bool locks_owner_has_blockers(struct file_lock_context *flctx,
  1046. fl_owner_t owner);
  1047. #else /* !CONFIG_FILE_LOCKING */
  1048. static inline int fcntl_getlk(struct file *file, unsigned int cmd,
  1049. struct flock __user *user)
  1050. {
  1051. return -EINVAL;
  1052. }
  1053. static inline int fcntl_setlk(unsigned int fd, struct file *file,
  1054. unsigned int cmd, struct flock __user *user)
  1055. {
  1056. return -EACCES;
  1057. }
  1058. #if BITS_PER_LONG == 32
  1059. static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
  1060. struct flock64 *user)
  1061. {
  1062. return -EINVAL;
  1063. }
  1064. static inline int fcntl_setlk64(unsigned int fd, struct file *file,
  1065. unsigned int cmd, struct flock64 *user)
  1066. {
  1067. return -EACCES;
  1068. }
  1069. #endif
  1070. static inline int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
  1071. {
  1072. return -EINVAL;
  1073. }
  1074. static inline int fcntl_getlease(struct file *filp)
  1075. {
  1076. return F_UNLCK;
  1077. }
  1078. static inline void
  1079. locks_free_lock_context(struct inode *inode)
  1080. {
  1081. }
  1082. static inline void locks_init_lock(struct file_lock *fl)
  1083. {
  1084. return;
  1085. }
  1086. static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
  1087. {
  1088. return;
  1089. }
  1090. static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  1091. {
  1092. return;
  1093. }
  1094. static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
  1095. {
  1096. return;
  1097. }
  1098. static inline void locks_remove_file(struct file *filp)
  1099. {
  1100. return;
  1101. }
  1102. static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
  1103. {
  1104. return;
  1105. }
  1106. static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
  1107. struct file_lock *conflock)
  1108. {
  1109. return -ENOLCK;
  1110. }
  1111. static inline int locks_delete_block(struct file_lock *waiter)
  1112. {
  1113. return -ENOENT;
  1114. }
  1115. static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
  1116. {
  1117. return 0;
  1118. }
  1119. static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
  1120. struct file_lock *fl, struct file_lock *conf)
  1121. {
  1122. return -ENOLCK;
  1123. }
  1124. static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
  1125. {
  1126. return 0;
  1127. }
  1128. static inline bool vfs_inode_has_locks(struct inode *inode)
  1129. {
  1130. return false;
  1131. }
  1132. static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
  1133. {
  1134. return -ENOLCK;
  1135. }
  1136. static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
  1137. {
  1138. return 0;
  1139. }
  1140. static inline void lease_get_mtime(struct inode *inode,
  1141. struct timespec64 *time)
  1142. {
  1143. return;
  1144. }
  1145. static inline int generic_setlease(struct file *filp, long arg,
  1146. struct file_lock **flp, void **priv)
  1147. {
  1148. return -EINVAL;
  1149. }
  1150. static inline int vfs_setlease(struct file *filp, long arg,
  1151. struct file_lock **lease, void **priv)
  1152. {
  1153. return -EINVAL;
  1154. }
  1155. static inline int lease_modify(struct file_lock *fl, int arg,
  1156. struct list_head *dispose)
  1157. {
  1158. return -EINVAL;
  1159. }
  1160. struct files_struct;
  1161. static inline void show_fd_locks(struct seq_file *f,
  1162. struct file *filp, struct files_struct *files) {}
  1163. static inline bool locks_owner_has_blockers(struct file_lock_context *flctx,
  1164. fl_owner_t owner)
  1165. {
  1166. return false;
  1167. }
  1168. #endif /* !CONFIG_FILE_LOCKING */
  1169. static inline struct inode *file_inode(const struct file *f)
  1170. {
  1171. return f->f_inode;
  1172. }
  1173. static inline struct dentry *file_dentry(const struct file *file)
  1174. {
  1175. return d_real(file->f_path.dentry, file_inode(file));
  1176. }
  1177. static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
  1178. {
  1179. return locks_lock_inode_wait(locks_inode(filp), fl);
  1180. }
  1181. struct fasync_struct {
  1182. rwlock_t fa_lock;
  1183. int magic;
  1184. int fa_fd;
  1185. struct fasync_struct *fa_next; /* singly linked list */
  1186. struct file *fa_file;
  1187. struct rcu_head fa_rcu;
  1188. };
  1189. #define FASYNC_MAGIC 0x4601
  1190. /* SMP safe fasync helpers: */
  1191. extern int fasync_helper(int, struct file *, int, struct fasync_struct **);
  1192. extern struct fasync_struct *fasync_insert_entry(int, struct file *, struct fasync_struct **, struct fasync_struct *);
  1193. extern int fasync_remove_entry(struct file *, struct fasync_struct **);
  1194. extern struct fasync_struct *fasync_alloc(void);
  1195. extern void fasync_free(struct fasync_struct *);
  1196. /* can be called from interrupts */
  1197. extern void kill_fasync(struct fasync_struct **, int, int);
  1198. extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
  1199. extern int f_setown(struct file *filp, unsigned long arg, int force);
  1200. extern void f_delown(struct file *filp);
  1201. extern pid_t f_getown(struct file *filp);
  1202. extern int send_sigurg(struct fown_struct *fown);
  1203. /*
  1204. * sb->s_flags. Note that these mirror the equivalent MS_* flags where
  1205. * represented in both.
  1206. */
  1207. #define SB_RDONLY BIT(0) /* Mount read-only */
  1208. #define SB_NOSUID BIT(1) /* Ignore suid and sgid bits */
  1209. #define SB_NODEV BIT(2) /* Disallow access to device special files */
  1210. #define SB_NOEXEC BIT(3) /* Disallow program execution */
  1211. #define SB_SYNCHRONOUS BIT(4) /* Writes are synced at once */
  1212. #define SB_MANDLOCK BIT(6) /* Allow mandatory locks on an FS */
  1213. #define SB_DIRSYNC BIT(7) /* Directory modifications are synchronous */
  1214. #define SB_NOATIME BIT(10) /* Do not update access times. */
  1215. #define SB_NODIRATIME BIT(11) /* Do not update directory access times */
  1216. #define SB_SILENT BIT(15)
  1217. #define SB_POSIXACL BIT(16) /* VFS does not apply the umask */
  1218. #define SB_INLINECRYPT BIT(17) /* Use blk-crypto for encrypted files */
  1219. #define SB_KERNMOUNT BIT(22) /* this is a kern_mount call */
  1220. #define SB_I_VERSION BIT(23) /* Update inode I_version field */
  1221. #define SB_LAZYTIME BIT(25) /* Update the on-disk [acm]times lazily */
  1222. /* These sb flags are internal to the kernel */
  1223. #define SB_SUBMOUNT BIT(26)
  1224. #define SB_FORCE BIT(27)
  1225. #define SB_NOSEC BIT(28)
  1226. #define SB_BORN BIT(29)
  1227. #define SB_ACTIVE BIT(30)
  1228. #define SB_NOUSER BIT(31)
  1229. /* These flags relate to encoding and casefolding */
  1230. #define SB_ENC_STRICT_MODE_FL (1 << 0)
  1231. #define sb_has_strict_encoding(sb) \
  1232. (sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL)
  1233. /*
  1234. * Umount options
  1235. */
  1236. #define MNT_FORCE 0x00000001 /* Attempt to forcibily umount */
  1237. #define MNT_DETACH 0x00000002 /* Just detach from the tree */
  1238. #define MNT_EXPIRE 0x00000004 /* Mark for expiry */
  1239. #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
  1240. #define UMOUNT_UNUSED 0x80000000 /* Flag guaranteed to be unused */
  1241. /* sb->s_iflags */
  1242. #define SB_I_CGROUPWB 0x00000001 /* cgroup-aware writeback enabled */
  1243. #define SB_I_NOEXEC 0x00000002 /* Ignore executables on this fs */
  1244. #define SB_I_NODEV 0x00000004 /* Ignore devices on this fs */
  1245. #define SB_I_STABLE_WRITES 0x00000008 /* don't modify blks until WB is done */
  1246. /* sb->s_iflags to limit user namespace mounts */
  1247. #define SB_I_USERNS_VISIBLE 0x00000010 /* fstype already mounted */
  1248. #define SB_I_IMA_UNVERIFIABLE_SIGNATURE 0x00000020
  1249. #define SB_I_UNTRUSTED_MOUNTER 0x00000040
  1250. #define SB_I_SKIP_SYNC 0x00000100 /* Skip superblock at global sync */
  1251. #define SB_I_PERSB_BDI 0x00000200 /* has a per-sb bdi */
  1252. #define SB_I_TS_EXPIRY_WARNED 0x00000400 /* warned about timestamp range expiry */
  1253. #define SB_I_RETIRED 0x00000800 /* superblock shouldn't be reused */
  1254. /* Possible states of 'frozen' field */
  1255. enum {
  1256. SB_UNFROZEN = 0, /* FS is unfrozen */
  1257. SB_FREEZE_WRITE = 1, /* Writes, dir ops, ioctls frozen */
  1258. SB_FREEZE_PAGEFAULT = 2, /* Page faults stopped as well */
  1259. SB_FREEZE_FS = 3, /* For internal FS use (e.g. to stop
  1260. * internal threads if needed) */
  1261. SB_FREEZE_COMPLETE = 4, /* ->freeze_fs finished successfully */
  1262. };
  1263. #define SB_FREEZE_LEVELS (SB_FREEZE_COMPLETE - 1)
  1264. struct sb_writers {
  1265. int frozen; /* Is sb frozen? */
  1266. wait_queue_head_t wait_unfrozen; /* wait for thaw */
  1267. struct percpu_rw_semaphore rw_sem[SB_FREEZE_LEVELS];
  1268. };
  1269. struct super_block {
  1270. struct list_head s_list; /* Keep this first */
  1271. dev_t s_dev; /* search index; _not_ kdev_t */
  1272. unsigned char s_blocksize_bits;
  1273. unsigned long s_blocksize;
  1274. loff_t s_maxbytes; /* Max file size */
  1275. struct file_system_type *s_type;
  1276. const struct super_operations *s_op;
  1277. const struct dquot_operations *dq_op;
  1278. const struct quotactl_ops *s_qcop;
  1279. const struct export_operations *s_export_op;
  1280. unsigned long s_flags;
  1281. unsigned long s_iflags; /* internal SB_I_* flags */
  1282. unsigned long s_magic;
  1283. struct dentry *s_root;
  1284. struct rw_semaphore s_umount;
  1285. int s_count;
  1286. atomic_t s_active;
  1287. #ifdef CONFIG_SECURITY
  1288. void *s_security;
  1289. #endif
  1290. const struct xattr_handler **s_xattr;
  1291. #ifdef CONFIG_FS_ENCRYPTION
  1292. const struct fscrypt_operations *s_cop;
  1293. struct fscrypt_keyring *s_master_keys; /* master crypto keys in use */
  1294. #endif
  1295. #ifdef CONFIG_FS_VERITY
  1296. const struct fsverity_operations *s_vop;
  1297. #endif
  1298. #if IS_ENABLED(CONFIG_UNICODE)
  1299. struct unicode_map *s_encoding;
  1300. __u16 s_encoding_flags;
  1301. #endif
  1302. struct hlist_bl_head s_roots; /* alternate root dentries for NFS */
  1303. struct list_head s_mounts; /* list of mounts; _not_ for fs use */
  1304. struct block_device *s_bdev;
  1305. struct backing_dev_info *s_bdi;
  1306. struct mtd_info *s_mtd;
  1307. struct hlist_node s_instances;
  1308. unsigned int s_quota_types; /* Bitmask of supported quota types */
  1309. struct quota_info s_dquot; /* Diskquota specific options */
  1310. struct sb_writers s_writers;
  1311. /*
  1312. * Keep s_fs_info, s_time_gran, s_fsnotify_mask, and
  1313. * s_fsnotify_marks together for cache efficiency. They are frequently
  1314. * accessed and rarely modified.
  1315. */
  1316. void *s_fs_info; /* Filesystem private info */
  1317. /* Granularity of c/m/atime in ns (cannot be worse than a second) */
  1318. u32 s_time_gran;
  1319. /* Time limits for c/m/atime in seconds */
  1320. time64_t s_time_min;
  1321. time64_t s_time_max;
  1322. #ifdef CONFIG_FSNOTIFY
  1323. __u32 s_fsnotify_mask;
  1324. struct fsnotify_mark_connector __rcu *s_fsnotify_marks;
  1325. #endif
  1326. char s_id[32]; /* Informational name */
  1327. uuid_t s_uuid; /* UUID */
  1328. unsigned int s_max_links;
  1329. fmode_t s_mode;
  1330. /*
  1331. * The next field is for VFS *only*. No filesystems have any business
  1332. * even looking at it. You had been warned.
  1333. */
  1334. struct mutex s_vfs_rename_mutex; /* Kludge */
  1335. /*
  1336. * Filesystem subtype. If non-empty the filesystem type field
  1337. * in /proc/mounts will be "type.subtype"
  1338. */
  1339. const char *s_subtype;
  1340. const struct dentry_operations *s_d_op; /* default d_op for dentries */
  1341. /*
  1342. * Saved pool identifier for cleancache (-1 means none)
  1343. */
  1344. int cleancache_poolid;
  1345. struct shrinker s_shrink; /* per-sb shrinker handle */
  1346. /* Number of inodes with nlink == 0 but still referenced */
  1347. atomic_long_t s_remove_count;
  1348. /*
  1349. * Number of inode/mount/sb objects that are being watched, note that
  1350. * inodes objects are currently double-accounted.
  1351. */
  1352. atomic_long_t s_fsnotify_connectors;
  1353. /* Being remounted read-only */
  1354. int s_readonly_remount;
  1355. /* per-sb errseq_t for reporting writeback errors via syncfs */
  1356. errseq_t s_wb_err;
  1357. /* AIO completions deferred from interrupt context */
  1358. struct workqueue_struct *s_dio_done_wq;
  1359. struct hlist_head s_pins;
  1360. /*
  1361. * Owning user namespace and default context in which to
  1362. * interpret filesystem uids, gids, quotas, device nodes,
  1363. * xattrs and security labels.
  1364. */
  1365. struct user_namespace *s_user_ns;
  1366. /*
  1367. * The list_lru structure is essentially just a pointer to a table
  1368. * of per-node lru lists, each of which has its own spinlock.
  1369. * There is no need to put them into separate cachelines.
  1370. */
  1371. struct list_lru s_dentry_lru;
  1372. struct list_lru s_inode_lru;
  1373. struct rcu_head rcu;
  1374. struct work_struct destroy_work;
  1375. struct mutex s_sync_lock; /* sync serialisation lock */
  1376. /*
  1377. * Indicates how deep in a filesystem stack this SB is
  1378. */
  1379. int s_stack_depth;
  1380. /* s_inode_list_lock protects s_inodes */
  1381. spinlock_t s_inode_list_lock ____cacheline_aligned_in_smp;
  1382. struct list_head s_inodes; /* all inodes */
  1383. spinlock_t s_inode_wblist_lock;
  1384. struct list_head s_inodes_wb; /* writeback inodes */
  1385. } __randomize_layout;
  1386. static inline struct user_namespace *i_user_ns(const struct inode *inode)
  1387. {
  1388. return inode->i_sb->s_user_ns;
  1389. }
  1390. /* Helper functions so that in most cases filesystems will
  1391. * not need to deal directly with kuid_t and kgid_t and can
  1392. * instead deal with the raw numeric values that are stored
  1393. * in the filesystem.
  1394. */
  1395. static inline uid_t i_uid_read(const struct inode *inode)
  1396. {
  1397. return from_kuid(i_user_ns(inode), inode->i_uid);
  1398. }
  1399. static inline gid_t i_gid_read(const struct inode *inode)
  1400. {
  1401. return from_kgid(i_user_ns(inode), inode->i_gid);
  1402. }
  1403. static inline void i_uid_write(struct inode *inode, uid_t uid)
  1404. {
  1405. inode->i_uid = make_kuid(i_user_ns(inode), uid);
  1406. }
  1407. static inline void i_gid_write(struct inode *inode, gid_t gid)
  1408. {
  1409. inode->i_gid = make_kgid(i_user_ns(inode), gid);
  1410. }
  1411. /**
  1412. * i_uid_into_mnt - map an inode's i_uid down into a mnt_userns
  1413. * @mnt_userns: user namespace of the mount the inode was found from
  1414. * @inode: inode to map
  1415. *
  1416. * Note, this will eventually be removed completely in favor of the type-safe
  1417. * i_uid_into_vfsuid().
  1418. *
  1419. * Return: the inode's i_uid mapped down according to @mnt_userns.
  1420. * If the inode's i_uid has no mapping INVALID_UID is returned.
  1421. */
  1422. static inline kuid_t i_uid_into_mnt(struct user_namespace *mnt_userns,
  1423. const struct inode *inode)
  1424. {
  1425. return AS_KUIDT(make_vfsuid(mnt_userns, i_user_ns(inode), inode->i_uid));
  1426. }
  1427. /**
  1428. * i_uid_into_vfsuid - map an inode's i_uid down into a mnt_userns
  1429. * @mnt_userns: user namespace of the mount the inode was found from
  1430. * @inode: inode to map
  1431. *
  1432. * Return: whe inode's i_uid mapped down according to @mnt_userns.
  1433. * If the inode's i_uid has no mapping INVALID_VFSUID is returned.
  1434. */
  1435. static inline vfsuid_t i_uid_into_vfsuid(struct user_namespace *mnt_userns,
  1436. const struct inode *inode)
  1437. {
  1438. return make_vfsuid(mnt_userns, i_user_ns(inode), inode->i_uid);
  1439. }
  1440. /**
  1441. * i_uid_needs_update - check whether inode's i_uid needs to be updated
  1442. * @mnt_userns: user namespace of the mount the inode was found from
  1443. * @attr: the new attributes of @inode
  1444. * @inode: the inode to update
  1445. *
  1446. * Check whether the $inode's i_uid field needs to be updated taking idmapped
  1447. * mounts into account if the filesystem supports it.
  1448. *
  1449. * Return: true if @inode's i_uid field needs to be updated, false if not.
  1450. */
  1451. static inline bool i_uid_needs_update(struct user_namespace *mnt_userns,
  1452. const struct iattr *attr,
  1453. const struct inode *inode)
  1454. {
  1455. return ((attr->ia_valid & ATTR_UID) &&
  1456. !vfsuid_eq(attr->ia_vfsuid,
  1457. i_uid_into_vfsuid(mnt_userns, inode)));
  1458. }
  1459. /**
  1460. * i_uid_update - update @inode's i_uid field
  1461. * @mnt_userns: user namespace of the mount the inode was found from
  1462. * @attr: the new attributes of @inode
  1463. * @inode: the inode to update
  1464. *
  1465. * Safely update @inode's i_uid field translating the vfsuid of any idmapped
  1466. * mount into the filesystem kuid.
  1467. */
  1468. static inline void i_uid_update(struct user_namespace *mnt_userns,
  1469. const struct iattr *attr,
  1470. struct inode *inode)
  1471. {
  1472. if (attr->ia_valid & ATTR_UID)
  1473. inode->i_uid = from_vfsuid(mnt_userns, i_user_ns(inode),
  1474. attr->ia_vfsuid);
  1475. }
  1476. /**
  1477. * i_gid_into_mnt - map an inode's i_gid down into a mnt_userns
  1478. * @mnt_userns: user namespace of the mount the inode was found from
  1479. * @inode: inode to map
  1480. *
  1481. * Note, this will eventually be removed completely in favor of the type-safe
  1482. * i_gid_into_vfsgid().
  1483. *
  1484. * Return: the inode's i_gid mapped down according to @mnt_userns.
  1485. * If the inode's i_gid has no mapping INVALID_GID is returned.
  1486. */
  1487. static inline kgid_t i_gid_into_mnt(struct user_namespace *mnt_userns,
  1488. const struct inode *inode)
  1489. {
  1490. return AS_KGIDT(make_vfsgid(mnt_userns, i_user_ns(inode), inode->i_gid));
  1491. }
  1492. /**
  1493. * i_gid_into_vfsgid - map an inode's i_gid down into a mnt_userns
  1494. * @mnt_userns: user namespace of the mount the inode was found from
  1495. * @inode: inode to map
  1496. *
  1497. * Return: the inode's i_gid mapped down according to @mnt_userns.
  1498. * If the inode's i_gid has no mapping INVALID_VFSGID is returned.
  1499. */
  1500. static inline vfsgid_t i_gid_into_vfsgid(struct user_namespace *mnt_userns,
  1501. const struct inode *inode)
  1502. {
  1503. return make_vfsgid(mnt_userns, i_user_ns(inode), inode->i_gid);
  1504. }
  1505. /**
  1506. * i_gid_needs_update - check whether inode's i_gid needs to be updated
  1507. * @mnt_userns: user namespace of the mount the inode was found from
  1508. * @attr: the new attributes of @inode
  1509. * @inode: the inode to update
  1510. *
  1511. * Check whether the $inode's i_gid field needs to be updated taking idmapped
  1512. * mounts into account if the filesystem supports it.
  1513. *
  1514. * Return: true if @inode's i_gid field needs to be updated, false if not.
  1515. */
  1516. static inline bool i_gid_needs_update(struct user_namespace *mnt_userns,
  1517. const struct iattr *attr,
  1518. const struct inode *inode)
  1519. {
  1520. return ((attr->ia_valid & ATTR_GID) &&
  1521. !vfsgid_eq(attr->ia_vfsgid,
  1522. i_gid_into_vfsgid(mnt_userns, inode)));
  1523. }
  1524. /**
  1525. * i_gid_update - update @inode's i_gid field
  1526. * @mnt_userns: user namespace of the mount the inode was found from
  1527. * @attr: the new attributes of @inode
  1528. * @inode: the inode to update
  1529. *
  1530. * Safely update @inode's i_gid field translating the vfsgid of any idmapped
  1531. * mount into the filesystem kgid.
  1532. */
  1533. static inline void i_gid_update(struct user_namespace *mnt_userns,
  1534. const struct iattr *attr,
  1535. struct inode *inode)
  1536. {
  1537. if (attr->ia_valid & ATTR_GID)
  1538. inode->i_gid = from_vfsgid(mnt_userns, i_user_ns(inode),
  1539. attr->ia_vfsgid);
  1540. }
  1541. /**
  1542. * inode_fsuid_set - initialize inode's i_uid field with callers fsuid
  1543. * @inode: inode to initialize
  1544. * @mnt_userns: user namespace of the mount the inode was found from
  1545. *
  1546. * Initialize the i_uid field of @inode. If the inode was found/created via
  1547. * an idmapped mount map the caller's fsuid according to @mnt_users.
  1548. */
  1549. static inline void inode_fsuid_set(struct inode *inode,
  1550. struct user_namespace *mnt_userns)
  1551. {
  1552. inode->i_uid = mapped_fsuid(mnt_userns, i_user_ns(inode));
  1553. }
  1554. /**
  1555. * inode_fsgid_set - initialize inode's i_gid field with callers fsgid
  1556. * @inode: inode to initialize
  1557. * @mnt_userns: user namespace of the mount the inode was found from
  1558. *
  1559. * Initialize the i_gid field of @inode. If the inode was found/created via
  1560. * an idmapped mount map the caller's fsgid according to @mnt_users.
  1561. */
  1562. static inline void inode_fsgid_set(struct inode *inode,
  1563. struct user_namespace *mnt_userns)
  1564. {
  1565. inode->i_gid = mapped_fsgid(mnt_userns, i_user_ns(inode));
  1566. }
  1567. /**
  1568. * fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
  1569. * @sb: the superblock we want a mapping in
  1570. * @mnt_userns: user namespace of the relevant mount
  1571. *
  1572. * Check whether the caller's fsuid and fsgid have a valid mapping in the
  1573. * s_user_ns of the superblock @sb. If the caller is on an idmapped mount map
  1574. * the caller's fsuid and fsgid according to the @mnt_userns first.
  1575. *
  1576. * Return: true if fsuid and fsgid is mapped, false if not.
  1577. */
  1578. static inline bool fsuidgid_has_mapping(struct super_block *sb,
  1579. struct user_namespace *mnt_userns)
  1580. {
  1581. struct user_namespace *fs_userns = sb->s_user_ns;
  1582. kuid_t kuid;
  1583. kgid_t kgid;
  1584. kuid = mapped_fsuid(mnt_userns, fs_userns);
  1585. if (!uid_valid(kuid))
  1586. return false;
  1587. kgid = mapped_fsgid(mnt_userns, fs_userns);
  1588. if (!gid_valid(kgid))
  1589. return false;
  1590. return kuid_has_mapping(fs_userns, kuid) &&
  1591. kgid_has_mapping(fs_userns, kgid);
  1592. }
  1593. struct timespec64 current_time(struct inode *inode);
  1594. struct timespec64 inode_set_ctime_current(struct inode *inode);
  1595. /**
  1596. * inode_get_ctime - fetch the current ctime from the inode
  1597. * @inode: inode from which to fetch ctime
  1598. *
  1599. * Grab the current ctime from the inode and return it.
  1600. */
  1601. static inline struct timespec64 inode_get_ctime(const struct inode *inode)
  1602. {
  1603. return inode->i_ctime;
  1604. }
  1605. /**
  1606. * inode_set_ctime_to_ts - set the ctime in the inode
  1607. * @inode: inode in which to set the ctime
  1608. * @ts: value to set in the ctime field
  1609. *
  1610. * Set the ctime in @inode to @ts
  1611. */
  1612. static inline struct timespec64 inode_set_ctime_to_ts(struct inode *inode,
  1613. struct timespec64 ts)
  1614. {
  1615. inode->i_ctime = ts;
  1616. return ts;
  1617. }
  1618. /**
  1619. * inode_set_ctime - set the ctime in the inode
  1620. * @inode: inode in which to set the ctime
  1621. * @sec: tv_sec value to set
  1622. * @nsec: tv_nsec value to set
  1623. *
  1624. * Set the ctime in @inode to { @sec, @nsec }
  1625. */
  1626. static inline struct timespec64 inode_set_ctime(struct inode *inode,
  1627. time64_t sec, long nsec)
  1628. {
  1629. struct timespec64 ts = { .tv_sec = sec,
  1630. .tv_nsec = nsec };
  1631. return inode_set_ctime_to_ts(inode, ts);
  1632. }
  1633. /*
  1634. * Snapshotting support.
  1635. */
  1636. /*
  1637. * These are internal functions, please use sb_start_{write,pagefault,intwrite}
  1638. * instead.
  1639. */
  1640. static inline void __sb_end_write(struct super_block *sb, int level)
  1641. {
  1642. percpu_up_read(sb->s_writers.rw_sem + level-1);
  1643. }
  1644. static inline void __sb_start_write(struct super_block *sb, int level)
  1645. {
  1646. percpu_down_read(sb->s_writers.rw_sem + level - 1);
  1647. }
  1648. static inline bool __sb_start_write_trylock(struct super_block *sb, int level)
  1649. {
  1650. return percpu_down_read_trylock(sb->s_writers.rw_sem + level - 1);
  1651. }
  1652. #define __sb_writers_acquired(sb, lev) \
  1653. percpu_rwsem_acquire(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
  1654. #define __sb_writers_release(sb, lev) \
  1655. percpu_rwsem_release(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
  1656. static inline bool sb_write_started(const struct super_block *sb)
  1657. {
  1658. return lockdep_is_held_type(sb->s_writers.rw_sem + SB_FREEZE_WRITE - 1, 1);
  1659. }
  1660. /**
  1661. * sb_end_write - drop write access to a superblock
  1662. * @sb: the super we wrote to
  1663. *
  1664. * Decrement number of writers to the filesystem. Wake up possible waiters
  1665. * wanting to freeze the filesystem.
  1666. */
  1667. static inline void sb_end_write(struct super_block *sb)
  1668. {
  1669. __sb_end_write(sb, SB_FREEZE_WRITE);
  1670. }
  1671. /**
  1672. * sb_end_pagefault - drop write access to a superblock from a page fault
  1673. * @sb: the super we wrote to
  1674. *
  1675. * Decrement number of processes handling write page fault to the filesystem.
  1676. * Wake up possible waiters wanting to freeze the filesystem.
  1677. */
  1678. static inline void sb_end_pagefault(struct super_block *sb)
  1679. {
  1680. __sb_end_write(sb, SB_FREEZE_PAGEFAULT);
  1681. }
  1682. /**
  1683. * sb_end_intwrite - drop write access to a superblock for internal fs purposes
  1684. * @sb: the super we wrote to
  1685. *
  1686. * Decrement fs-internal number of writers to the filesystem. Wake up possible
  1687. * waiters wanting to freeze the filesystem.
  1688. */
  1689. static inline void sb_end_intwrite(struct super_block *sb)
  1690. {
  1691. __sb_end_write(sb, SB_FREEZE_FS);
  1692. }
  1693. /**
  1694. * sb_start_write - get write access to a superblock
  1695. * @sb: the super we write to
  1696. *
  1697. * When a process wants to write data or metadata to a file system (i.e. dirty
  1698. * a page or an inode), it should embed the operation in a sb_start_write() -
  1699. * sb_end_write() pair to get exclusion against file system freezing. This
  1700. * function increments number of writers preventing freezing. If the file
  1701. * system is already frozen, the function waits until the file system is
  1702. * thawed.
  1703. *
  1704. * Since freeze protection behaves as a lock, users have to preserve
  1705. * ordering of freeze protection and other filesystem locks. Generally,
  1706. * freeze protection should be the outermost lock. In particular, we have:
  1707. *
  1708. * sb_start_write
  1709. * -> i_mutex (write path, truncate, directory ops, ...)
  1710. * -> s_umount (freeze_super, thaw_super)
  1711. */
  1712. static inline void sb_start_write(struct super_block *sb)
  1713. {
  1714. __sb_start_write(sb, SB_FREEZE_WRITE);
  1715. }
  1716. static inline bool sb_start_write_trylock(struct super_block *sb)
  1717. {
  1718. return __sb_start_write_trylock(sb, SB_FREEZE_WRITE);
  1719. }
  1720. /**
  1721. * sb_start_pagefault - get write access to a superblock from a page fault
  1722. * @sb: the super we write to
  1723. *
  1724. * When a process starts handling write page fault, it should embed the
  1725. * operation into sb_start_pagefault() - sb_end_pagefault() pair to get
  1726. * exclusion against file system freezing. This is needed since the page fault
  1727. * is going to dirty a page. This function increments number of running page
  1728. * faults preventing freezing. If the file system is already frozen, the
  1729. * function waits until the file system is thawed.
  1730. *
  1731. * Since page fault freeze protection behaves as a lock, users have to preserve
  1732. * ordering of freeze protection and other filesystem locks. It is advised to
  1733. * put sb_start_pagefault() close to mmap_lock in lock ordering. Page fault
  1734. * handling code implies lock dependency:
  1735. *
  1736. * mmap_lock
  1737. * -> sb_start_pagefault
  1738. */
  1739. static inline void sb_start_pagefault(struct super_block *sb)
  1740. {
  1741. __sb_start_write(sb, SB_FREEZE_PAGEFAULT);
  1742. }
  1743. /**
  1744. * sb_start_intwrite - get write access to a superblock for internal fs purposes
  1745. * @sb: the super we write to
  1746. *
  1747. * This is the third level of protection against filesystem freezing. It is
  1748. * free for use by a filesystem. The only requirement is that it must rank
  1749. * below sb_start_pagefault.
  1750. *
  1751. * For example filesystem can call sb_start_intwrite() when starting a
  1752. * transaction which somewhat eases handling of freezing for internal sources
  1753. * of filesystem changes (internal fs threads, discarding preallocation on file
  1754. * close, etc.).
  1755. */
  1756. static inline void sb_start_intwrite(struct super_block *sb)
  1757. {
  1758. __sb_start_write(sb, SB_FREEZE_FS);
  1759. }
  1760. static inline bool sb_start_intwrite_trylock(struct super_block *sb)
  1761. {
  1762. return __sb_start_write_trylock(sb, SB_FREEZE_FS);
  1763. }
  1764. bool inode_owner_or_capable(struct user_namespace *mnt_userns,
  1765. const struct inode *inode);
  1766. /*
  1767. * VFS helper functions..
  1768. */
  1769. int vfs_create(struct user_namespace *, struct inode *,
  1770. struct dentry *, umode_t, bool);
  1771. int vfs_mkdir(struct user_namespace *, struct inode *,
  1772. struct dentry *, umode_t);
  1773. int vfs_mknod(struct user_namespace *, struct inode *, struct dentry *,
  1774. umode_t, dev_t);
  1775. int vfs_symlink(struct user_namespace *, struct inode *,
  1776. struct dentry *, const char *);
  1777. int vfs_link(struct dentry *, struct user_namespace *, struct inode *,
  1778. struct dentry *, struct inode **);
  1779. int vfs_rmdir(struct user_namespace *, struct inode *, struct dentry *);
  1780. int vfs_unlink(struct user_namespace *, struct inode *, struct dentry *,
  1781. struct inode **);
  1782. /**
  1783. * struct renamedata - contains all information required for renaming
  1784. * @old_mnt_userns: old user namespace of the mount the inode was found from
  1785. * @old_dir: parent of source
  1786. * @old_dentry: source
  1787. * @new_mnt_userns: new user namespace of the mount the inode was found from
  1788. * @new_dir: parent of destination
  1789. * @new_dentry: destination
  1790. * @delegated_inode: returns an inode needing a delegation break
  1791. * @flags: rename flags
  1792. */
  1793. struct renamedata {
  1794. struct user_namespace *old_mnt_userns;
  1795. struct inode *old_dir;
  1796. struct dentry *old_dentry;
  1797. struct user_namespace *new_mnt_userns;
  1798. struct inode *new_dir;
  1799. struct dentry *new_dentry;
  1800. struct inode **delegated_inode;
  1801. unsigned int flags;
  1802. } __randomize_layout;
  1803. int vfs_rename(struct renamedata *);
  1804. static inline int vfs_whiteout(struct user_namespace *mnt_userns,
  1805. struct inode *dir, struct dentry *dentry)
  1806. {
  1807. return vfs_mknod(mnt_userns, dir, dentry, S_IFCHR | WHITEOUT_MODE,
  1808. WHITEOUT_DEV);
  1809. }
  1810. struct file *vfs_tmpfile_open(struct user_namespace *mnt_userns,
  1811. const struct path *parentpath,
  1812. umode_t mode, int open_flag, const struct cred *cred);
  1813. int vfs_mkobj(struct dentry *, umode_t,
  1814. int (*f)(struct dentry *, umode_t, void *),
  1815. void *);
  1816. int vfs_fchown(struct file *file, uid_t user, gid_t group);
  1817. int vfs_fchmod(struct file *file, umode_t mode);
  1818. int vfs_utimes(const struct path *path, struct timespec64 *times);
  1819. extern long vfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  1820. #ifdef CONFIG_COMPAT
  1821. extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
  1822. unsigned long arg);
  1823. #else
  1824. #define compat_ptr_ioctl NULL
  1825. #endif
  1826. /*
  1827. * VFS file helper functions.
  1828. */
  1829. void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
  1830. const struct inode *dir, umode_t mode);
  1831. extern bool may_open_dev(const struct path *path);
  1832. umode_t mode_strip_sgid(struct user_namespace *mnt_userns,
  1833. const struct inode *dir, umode_t mode);
  1834. /*
  1835. * This is the "filldir" function type, used by readdir() to let
  1836. * the kernel specify what kind of dirent layout it wants to have.
  1837. * This allows the kernel to read directories into kernel space or
  1838. * to have different dirent layouts depending on the binary type.
  1839. * Return 'true' to keep going and 'false' if there are no more entries.
  1840. */
  1841. struct dir_context;
  1842. typedef bool (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,
  1843. unsigned);
  1844. struct dir_context {
  1845. filldir_t actor;
  1846. loff_t pos;
  1847. };
  1848. /*
  1849. * These flags let !MMU mmap() govern direct device mapping vs immediate
  1850. * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
  1851. *
  1852. * NOMMU_MAP_COPY: Copy can be mapped (MAP_PRIVATE)
  1853. * NOMMU_MAP_DIRECT: Can be mapped directly (MAP_SHARED)
  1854. * NOMMU_MAP_READ: Can be mapped for reading
  1855. * NOMMU_MAP_WRITE: Can be mapped for writing
  1856. * NOMMU_MAP_EXEC: Can be mapped for execution
  1857. */
  1858. #define NOMMU_MAP_COPY 0x00000001
  1859. #define NOMMU_MAP_DIRECT 0x00000008
  1860. #define NOMMU_MAP_READ VM_MAYREAD
  1861. #define NOMMU_MAP_WRITE VM_MAYWRITE
  1862. #define NOMMU_MAP_EXEC VM_MAYEXEC
  1863. #define NOMMU_VMFLAGS \
  1864. (NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC)
  1865. /*
  1866. * These flags control the behavior of the remap_file_range function pointer.
  1867. * If it is called with len == 0 that means "remap to end of source file".
  1868. * See Documentation/filesystems/vfs.rst for more details about this call.
  1869. *
  1870. * REMAP_FILE_DEDUP: only remap if contents identical (i.e. deduplicate)
  1871. * REMAP_FILE_CAN_SHORTEN: caller can handle a shortened request
  1872. */
  1873. #define REMAP_FILE_DEDUP (1 << 0)
  1874. #define REMAP_FILE_CAN_SHORTEN (1 << 1)
  1875. /*
  1876. * These flags signal that the caller is ok with altering various aspects of
  1877. * the behavior of the remap operation. The changes must be made by the
  1878. * implementation; the vfs remap helper functions can take advantage of them.
  1879. * Flags in this category exist to preserve the quirky behavior of the hoisted
  1880. * btrfs clone/dedupe ioctls.
  1881. */
  1882. #define REMAP_FILE_ADVISORY (REMAP_FILE_CAN_SHORTEN)
  1883. /*
  1884. * These flags control the behavior of vfs_copy_file_range().
  1885. * They are not available to the user via syscall.
  1886. *
  1887. * COPY_FILE_SPLICE: call splice direct instead of fs clone/copy ops
  1888. */
  1889. #define COPY_FILE_SPLICE (1 << 0)
  1890. struct iov_iter;
  1891. struct io_uring_cmd;
  1892. struct file_operations {
  1893. struct module *owner;
  1894. loff_t (*llseek) (struct file *, loff_t, int);
  1895. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  1896. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  1897. ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
  1898. ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
  1899. int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
  1900. unsigned int flags);
  1901. int (*iterate) (struct file *, struct dir_context *);
  1902. int (*iterate_shared) (struct file *, struct dir_context *);
  1903. __poll_t (*poll) (struct file *, struct poll_table_struct *);
  1904. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  1905. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  1906. int (*mmap) (struct file *, struct vm_area_struct *);
  1907. unsigned long mmap_supported_flags;
  1908. int (*open) (struct inode *, struct file *);
  1909. int (*flush) (struct file *, fl_owner_t id);
  1910. int (*release) (struct inode *, struct file *);
  1911. int (*fsync) (struct file *, loff_t, loff_t, int datasync);
  1912. int (*fasync) (int, struct file *, int);
  1913. int (*lock) (struct file *, int, struct file_lock *);
  1914. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  1915. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  1916. int (*check_flags)(int);
  1917. int (*flock) (struct file *, int, struct file_lock *);
  1918. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
  1919. ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
  1920. int (*setlease)(struct file *, long, struct file_lock **, void **);
  1921. long (*fallocate)(struct file *file, int mode, loff_t offset,
  1922. loff_t len);
  1923. void (*show_fdinfo)(struct seq_file *m, struct file *f);
  1924. #ifndef CONFIG_MMU
  1925. unsigned (*mmap_capabilities)(struct file *);
  1926. #endif
  1927. ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
  1928. loff_t, size_t, unsigned int);
  1929. loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
  1930. struct file *file_out, loff_t pos_out,
  1931. loff_t len, unsigned int remap_flags);
  1932. int (*fadvise)(struct file *, loff_t, loff_t, int);
  1933. int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
  1934. int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
  1935. unsigned int poll_flags);
  1936. } __randomize_layout;
  1937. struct inode_operations {
  1938. struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
  1939. const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
  1940. int (*permission) (struct user_namespace *, struct inode *, int);
  1941. struct posix_acl * (*get_acl)(struct inode *, int, bool);
  1942. int (*readlink) (struct dentry *, char __user *,int);
  1943. int (*create) (struct user_namespace *, struct inode *,struct dentry *,
  1944. umode_t, bool);
  1945. int (*link) (struct dentry *,struct inode *,struct dentry *);
  1946. int (*unlink) (struct inode *,struct dentry *);
  1947. int (*symlink) (struct user_namespace *, struct inode *,struct dentry *,
  1948. const char *);
  1949. int (*mkdir) (struct user_namespace *, struct inode *,struct dentry *,
  1950. umode_t);
  1951. int (*rmdir) (struct inode *,struct dentry *);
  1952. int (*mknod) (struct user_namespace *, struct inode *,struct dentry *,
  1953. umode_t,dev_t);
  1954. int (*rename) (struct user_namespace *, struct inode *, struct dentry *,
  1955. struct inode *, struct dentry *, unsigned int);
  1956. int (*setattr) (struct user_namespace *, struct dentry *,
  1957. struct iattr *);
  1958. int (*getattr) (struct user_namespace *, const struct path *,
  1959. struct kstat *, u32, unsigned int);
  1960. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  1961. int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
  1962. u64 len);
  1963. int (*update_time)(struct inode *, struct timespec64 *, int);
  1964. int (*atomic_open)(struct inode *, struct dentry *,
  1965. struct file *, unsigned open_flag,
  1966. umode_t create_mode);
  1967. int (*tmpfile) (struct user_namespace *, struct inode *,
  1968. struct file *, umode_t);
  1969. int (*set_acl)(struct user_namespace *, struct inode *,
  1970. struct posix_acl *, int);
  1971. int (*fileattr_set)(struct user_namespace *mnt_userns,
  1972. struct dentry *dentry, struct fileattr *fa);
  1973. int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa);
  1974. } ____cacheline_aligned;
  1975. static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,
  1976. struct iov_iter *iter)
  1977. {
  1978. return file->f_op->read_iter(kio, iter);
  1979. }
  1980. static inline ssize_t call_write_iter(struct file *file, struct kiocb *kio,
  1981. struct iov_iter *iter)
  1982. {
  1983. return file->f_op->write_iter(kio, iter);
  1984. }
  1985. static inline int call_mmap(struct file *file, struct vm_area_struct *vma)
  1986. {
  1987. return file->f_op->mmap(file, vma);
  1988. }
  1989. extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
  1990. extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
  1991. extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
  1992. loff_t, size_t, unsigned int);
  1993. extern ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
  1994. struct file *file_out, loff_t pos_out,
  1995. size_t len, unsigned int flags);
  1996. int __generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
  1997. struct file *file_out, loff_t pos_out,
  1998. loff_t *len, unsigned int remap_flags,
  1999. const struct iomap_ops *dax_read_ops);
  2000. int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
  2001. struct file *file_out, loff_t pos_out,
  2002. loff_t *count, unsigned int remap_flags);
  2003. extern loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
  2004. struct file *file_out, loff_t pos_out,
  2005. loff_t len, unsigned int remap_flags);
  2006. extern loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
  2007. struct file *file_out, loff_t pos_out,
  2008. loff_t len, unsigned int remap_flags);
  2009. extern int vfs_dedupe_file_range(struct file *file,
  2010. struct file_dedupe_range *same);
  2011. extern loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
  2012. struct file *dst_file, loff_t dst_pos,
  2013. loff_t len, unsigned int remap_flags);
  2014. struct super_operations {
  2015. struct inode *(*alloc_inode)(struct super_block *sb);
  2016. void (*destroy_inode)(struct inode *);
  2017. void (*free_inode)(struct inode *);
  2018. void (*dirty_inode) (struct inode *, int flags);
  2019. int (*write_inode) (struct inode *, struct writeback_control *wbc);
  2020. int (*drop_inode) (struct inode *);
  2021. void (*evict_inode) (struct inode *);
  2022. void (*put_super) (struct super_block *);
  2023. int (*sync_fs)(struct super_block *sb, int wait);
  2024. int (*freeze_super) (struct super_block *);
  2025. int (*freeze_fs) (struct super_block *);
  2026. int (*thaw_super) (struct super_block *);
  2027. int (*unfreeze_fs) (struct super_block *);
  2028. int (*statfs) (struct dentry *, struct kstatfs *);
  2029. int (*remount_fs) (struct super_block *, int *, char *);
  2030. void (*umount_begin) (struct super_block *);
  2031. int (*show_options)(struct seq_file *, struct dentry *);
  2032. int (*show_devname)(struct seq_file *, struct dentry *);
  2033. int (*show_path)(struct seq_file *, struct dentry *);
  2034. int (*show_stats)(struct seq_file *, struct dentry *);
  2035. #ifdef CONFIG_QUOTA
  2036. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  2037. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  2038. struct dquot **(*get_dquots)(struct inode *);
  2039. #endif
  2040. long (*nr_cached_objects)(struct super_block *,
  2041. struct shrink_control *);
  2042. long (*free_cached_objects)(struct super_block *,
  2043. struct shrink_control *);
  2044. };
  2045. /*
  2046. * Inode flags - they have no relation to superblock flags now
  2047. */
  2048. #define S_SYNC (1 << 0) /* Writes are synced at once */
  2049. #define S_NOATIME (1 << 1) /* Do not update access times */
  2050. #define S_APPEND (1 << 2) /* Append-only file */
  2051. #define S_IMMUTABLE (1 << 3) /* Immutable file */
  2052. #define S_DEAD (1 << 4) /* removed, but still open directory */
  2053. #define S_NOQUOTA (1 << 5) /* Inode is not counted to quota */
  2054. #define S_DIRSYNC (1 << 6) /* Directory modifications are synchronous */
  2055. #define S_NOCMTIME (1 << 7) /* Do not update file c/mtime */
  2056. #define S_SWAPFILE (1 << 8) /* Do not truncate: swapon got its bmaps */
  2057. #define S_PRIVATE (1 << 9) /* Inode is fs-internal */
  2058. #define S_IMA (1 << 10) /* Inode has an associated IMA struct */
  2059. #define S_AUTOMOUNT (1 << 11) /* Automount/referral quasi-directory */
  2060. #define S_NOSEC (1 << 12) /* no suid or xattr security attributes */
  2061. #ifdef CONFIG_FS_DAX
  2062. #define S_DAX (1 << 13) /* Direct Access, avoiding the page cache */
  2063. #else
  2064. #define S_DAX 0 /* Make all the DAX code disappear */
  2065. #endif
  2066. #define S_ENCRYPTED (1 << 14) /* Encrypted file (using fs/crypto/) */
  2067. #define S_CASEFOLD (1 << 15) /* Casefolded file */
  2068. #define S_VERITY (1 << 16) /* Verity file (using fs/verity/) */
  2069. #define S_KERNEL_FILE (1 << 17) /* File is in use by the kernel (eg. fs/cachefiles) */
  2070. /*
  2071. * Note that nosuid etc flags are inode-specific: setting some file-system
  2072. * flags just means all the inodes inherit those flags by default. It might be
  2073. * possible to override it selectively if you really wanted to with some
  2074. * ioctl() that is not currently implemented.
  2075. *
  2076. * Exception: SB_RDONLY is always applied to the entire file system.
  2077. *
  2078. * Unfortunately, it is possible to change a filesystems flags with it mounted
  2079. * with files in use. This means that all of the inodes will not have their
  2080. * i_flags updated. Hence, i_flags no longer inherit the superblock mount
  2081. * flags, so these have to be checked separately. -- [email protected]
  2082. */
  2083. #define __IS_FLG(inode, flg) ((inode)->i_sb->s_flags & (flg))
  2084. static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags & SB_RDONLY; }
  2085. #define IS_RDONLY(inode) sb_rdonly((inode)->i_sb)
  2086. #define IS_SYNC(inode) (__IS_FLG(inode, SB_SYNCHRONOUS) || \
  2087. ((inode)->i_flags & S_SYNC))
  2088. #define IS_DIRSYNC(inode) (__IS_FLG(inode, SB_SYNCHRONOUS|SB_DIRSYNC) || \
  2089. ((inode)->i_flags & (S_SYNC|S_DIRSYNC)))
  2090. #define IS_MANDLOCK(inode) __IS_FLG(inode, SB_MANDLOCK)
  2091. #define IS_NOATIME(inode) __IS_FLG(inode, SB_RDONLY|SB_NOATIME)
  2092. #define IS_I_VERSION(inode) __IS_FLG(inode, SB_I_VERSION)
  2093. #define IS_NOQUOTA(inode) ((inode)->i_flags & S_NOQUOTA)
  2094. #define IS_APPEND(inode) ((inode)->i_flags & S_APPEND)
  2095. #define IS_IMMUTABLE(inode) ((inode)->i_flags & S_IMMUTABLE)
  2096. #define IS_POSIXACL(inode) __IS_FLG(inode, SB_POSIXACL)
  2097. #define IS_DEADDIR(inode) ((inode)->i_flags & S_DEAD)
  2098. #define IS_NOCMTIME(inode) ((inode)->i_flags & S_NOCMTIME)
  2099. #define IS_SWAPFILE(inode) ((inode)->i_flags & S_SWAPFILE)
  2100. #define IS_PRIVATE(inode) ((inode)->i_flags & S_PRIVATE)
  2101. #define IS_IMA(inode) ((inode)->i_flags & S_IMA)
  2102. #define IS_AUTOMOUNT(inode) ((inode)->i_flags & S_AUTOMOUNT)
  2103. #define IS_NOSEC(inode) ((inode)->i_flags & S_NOSEC)
  2104. #define IS_DAX(inode) ((inode)->i_flags & S_DAX)
  2105. #define IS_ENCRYPTED(inode) ((inode)->i_flags & S_ENCRYPTED)
  2106. #define IS_CASEFOLDED(inode) ((inode)->i_flags & S_CASEFOLD)
  2107. #define IS_VERITY(inode) ((inode)->i_flags & S_VERITY)
  2108. #define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \
  2109. (inode)->i_rdev == WHITEOUT_DEV)
  2110. static inline bool HAS_UNMAPPED_ID(struct user_namespace *mnt_userns,
  2111. struct inode *inode)
  2112. {
  2113. return !vfsuid_valid(i_uid_into_vfsuid(mnt_userns, inode)) ||
  2114. !vfsgid_valid(i_gid_into_vfsgid(mnt_userns, inode));
  2115. }
  2116. static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
  2117. {
  2118. *kiocb = (struct kiocb) {
  2119. .ki_filp = filp,
  2120. .ki_flags = filp->f_iocb_flags,
  2121. .ki_ioprio = get_current_ioprio(),
  2122. };
  2123. }
  2124. static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
  2125. struct file *filp)
  2126. {
  2127. *kiocb = (struct kiocb) {
  2128. .ki_filp = filp,
  2129. .ki_flags = kiocb_src->ki_flags,
  2130. .ki_ioprio = kiocb_src->ki_ioprio,
  2131. .ki_pos = kiocb_src->ki_pos,
  2132. };
  2133. }
  2134. /*
  2135. * Inode state bits. Protected by inode->i_lock
  2136. *
  2137. * Four bits determine the dirty state of the inode: I_DIRTY_SYNC,
  2138. * I_DIRTY_DATASYNC, I_DIRTY_PAGES, and I_DIRTY_TIME.
  2139. *
  2140. * Four bits define the lifetime of an inode. Initially, inodes are I_NEW,
  2141. * until that flag is cleared. I_WILL_FREE, I_FREEING and I_CLEAR are set at
  2142. * various stages of removing an inode.
  2143. *
  2144. * Two bits are used for locking and completion notification, I_NEW and I_SYNC.
  2145. *
  2146. * I_DIRTY_SYNC Inode is dirty, but doesn't have to be written on
  2147. * fdatasync() (unless I_DIRTY_DATASYNC is also set).
  2148. * Timestamp updates are the usual cause.
  2149. * I_DIRTY_DATASYNC Data-related inode changes pending. We keep track of
  2150. * these changes separately from I_DIRTY_SYNC so that we
  2151. * don't have to write inode on fdatasync() when only
  2152. * e.g. the timestamps have changed.
  2153. * I_DIRTY_PAGES Inode has dirty pages. Inode itself may be clean.
  2154. * I_DIRTY_TIME The inode itself has dirty timestamps, and the
  2155. * lazytime mount option is enabled. We keep track of this
  2156. * separately from I_DIRTY_SYNC in order to implement
  2157. * lazytime. This gets cleared if I_DIRTY_INODE
  2158. * (I_DIRTY_SYNC and/or I_DIRTY_DATASYNC) gets set. But
  2159. * I_DIRTY_TIME can still be set if I_DIRTY_SYNC is already
  2160. * in place because writeback might already be in progress
  2161. * and we don't want to lose the time update
  2162. * I_NEW Serves as both a mutex and completion notification.
  2163. * New inodes set I_NEW. If two processes both create
  2164. * the same inode, one of them will release its inode and
  2165. * wait for I_NEW to be released before returning.
  2166. * Inodes in I_WILL_FREE, I_FREEING or I_CLEAR state can
  2167. * also cause waiting on I_NEW, without I_NEW actually
  2168. * being set. find_inode() uses this to prevent returning
  2169. * nearly-dead inodes.
  2170. * I_WILL_FREE Must be set when calling write_inode_now() if i_count
  2171. * is zero. I_FREEING must be set when I_WILL_FREE is
  2172. * cleared.
  2173. * I_FREEING Set when inode is about to be freed but still has dirty
  2174. * pages or buffers attached or the inode itself is still
  2175. * dirty.
  2176. * I_CLEAR Added by clear_inode(). In this state the inode is
  2177. * clean and can be destroyed. Inode keeps I_FREEING.
  2178. *
  2179. * Inodes that are I_WILL_FREE, I_FREEING or I_CLEAR are
  2180. * prohibited for many purposes. iget() must wait for
  2181. * the inode to be completely released, then create it
  2182. * anew. Other functions will just ignore such inodes,
  2183. * if appropriate. I_NEW is used for waiting.
  2184. *
  2185. * I_SYNC Writeback of inode is running. The bit is set during
  2186. * data writeback, and cleared with a wakeup on the bit
  2187. * address once it is done. The bit is also used to pin
  2188. * the inode in memory for flusher thread.
  2189. *
  2190. * I_REFERENCED Marks the inode as recently references on the LRU list.
  2191. *
  2192. * I_DIO_WAKEUP Never set. Only used as a key for wait_on_bit().
  2193. *
  2194. * I_WB_SWITCH Cgroup bdi_writeback switching in progress. Used to
  2195. * synchronize competing switching instances and to tell
  2196. * wb stat updates to grab the i_pages lock. See
  2197. * inode_switch_wbs_work_fn() for details.
  2198. *
  2199. * I_OVL_INUSE Used by overlayfs to get exclusive ownership on upper
  2200. * and work dirs among overlayfs mounts.
  2201. *
  2202. * I_CREATING New object's inode in the middle of setting up.
  2203. *
  2204. * I_DONTCACHE Evict inode as soon as it is not used anymore.
  2205. *
  2206. * I_SYNC_QUEUED Inode is queued in b_io or b_more_io writeback lists.
  2207. * Used to detect that mark_inode_dirty() should not move
  2208. * inode between dirty lists.
  2209. *
  2210. * I_PINNING_FSCACHE_WB Inode is pinning an fscache object for writeback.
  2211. *
  2212. * Q: What is the difference between I_WILL_FREE and I_FREEING?
  2213. */
  2214. #define I_DIRTY_SYNC (1 << 0)
  2215. #define I_DIRTY_DATASYNC (1 << 1)
  2216. #define I_DIRTY_PAGES (1 << 2)
  2217. #define __I_NEW 3
  2218. #define I_NEW (1 << __I_NEW)
  2219. #define I_WILL_FREE (1 << 4)
  2220. #define I_FREEING (1 << 5)
  2221. #define I_CLEAR (1 << 6)
  2222. #define __I_SYNC 7
  2223. #define I_SYNC (1 << __I_SYNC)
  2224. #define I_REFERENCED (1 << 8)
  2225. #define __I_DIO_WAKEUP 9
  2226. #define I_DIO_WAKEUP (1 << __I_DIO_WAKEUP)
  2227. #define I_LINKABLE (1 << 10)
  2228. #define I_DIRTY_TIME (1 << 11)
  2229. #define I_WB_SWITCH (1 << 13)
  2230. #define I_OVL_INUSE (1 << 14)
  2231. #define I_CREATING (1 << 15)
  2232. #define I_DONTCACHE (1 << 16)
  2233. #define I_SYNC_QUEUED (1 << 17)
  2234. #define I_PINNING_FSCACHE_WB (1 << 18)
  2235. #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
  2236. #define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
  2237. #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
  2238. extern void __mark_inode_dirty(struct inode *, int);
  2239. static inline void mark_inode_dirty(struct inode *inode)
  2240. {
  2241. __mark_inode_dirty(inode, I_DIRTY);
  2242. }
  2243. static inline void mark_inode_dirty_sync(struct inode *inode)
  2244. {
  2245. __mark_inode_dirty(inode, I_DIRTY_SYNC);
  2246. }
  2247. /*
  2248. * Returns true if the given inode itself only has dirty timestamps (its pages
  2249. * may still be dirty) and isn't currently being allocated or freed.
  2250. * Filesystems should call this if when writing an inode when lazytime is
  2251. * enabled, they want to opportunistically write the timestamps of other inodes
  2252. * located very nearby on-disk, e.g. in the same inode block. This returns true
  2253. * if the given inode is in need of such an opportunistic update. Requires
  2254. * i_lock, or at least later re-checking under i_lock.
  2255. */
  2256. static inline bool inode_is_dirtytime_only(struct inode *inode)
  2257. {
  2258. return (inode->i_state & (I_DIRTY_TIME | I_NEW |
  2259. I_FREEING | I_WILL_FREE)) == I_DIRTY_TIME;
  2260. }
  2261. extern void inc_nlink(struct inode *inode);
  2262. extern void drop_nlink(struct inode *inode);
  2263. extern void clear_nlink(struct inode *inode);
  2264. extern void set_nlink(struct inode *inode, unsigned int nlink);
  2265. static inline void inode_inc_link_count(struct inode *inode)
  2266. {
  2267. inc_nlink(inode);
  2268. mark_inode_dirty(inode);
  2269. }
  2270. static inline void inode_dec_link_count(struct inode *inode)
  2271. {
  2272. drop_nlink(inode);
  2273. mark_inode_dirty(inode);
  2274. }
  2275. enum file_time_flags {
  2276. S_ATIME = 1,
  2277. S_MTIME = 2,
  2278. S_CTIME = 4,
  2279. S_VERSION = 8,
  2280. };
  2281. extern bool atime_needs_update(const struct path *, struct inode *);
  2282. extern void touch_atime(const struct path *);
  2283. int inode_update_time(struct inode *inode, struct timespec64 *time, int flags);
  2284. static inline void file_accessed(struct file *file)
  2285. {
  2286. if (!(file->f_flags & O_NOATIME))
  2287. touch_atime(&file->f_path);
  2288. }
  2289. extern int file_modified(struct file *file);
  2290. int kiocb_modified(struct kiocb *iocb);
  2291. int sync_inode_metadata(struct inode *inode, int wait);
  2292. struct file_system_type {
  2293. const char *name;
  2294. int fs_flags;
  2295. #define FS_REQUIRES_DEV 1
  2296. #define FS_BINARY_MOUNTDATA 2
  2297. #define FS_HAS_SUBTYPE 4
  2298. #define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
  2299. #define FS_DISALLOW_NOTIFY_PERM 16 /* Disable fanotify permission events */
  2300. #define FS_ALLOW_IDMAP 32 /* FS has been updated to handle vfs idmappings. */
  2301. #define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
  2302. int (*init_fs_context)(struct fs_context *);
  2303. const struct fs_parameter_spec *parameters;
  2304. struct dentry *(*mount) (struct file_system_type *, int,
  2305. const char *, void *);
  2306. void (*kill_sb) (struct super_block *);
  2307. struct module *owner;
  2308. struct file_system_type * next;
  2309. struct hlist_head fs_supers;
  2310. struct lock_class_key s_lock_key;
  2311. struct lock_class_key s_umount_key;
  2312. struct lock_class_key s_vfs_rename_key;
  2313. struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
  2314. struct lock_class_key i_lock_key;
  2315. struct lock_class_key i_mutex_key;
  2316. struct lock_class_key invalidate_lock_key;
  2317. struct lock_class_key i_mutex_dir_key;
  2318. };
  2319. #define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
  2320. extern struct dentry *mount_bdev(struct file_system_type *fs_type,
  2321. int flags, const char *dev_name, void *data,
  2322. int (*fill_super)(struct super_block *, void *, int));
  2323. extern struct dentry *mount_single(struct file_system_type *fs_type,
  2324. int flags, void *data,
  2325. int (*fill_super)(struct super_block *, void *, int));
  2326. extern struct dentry *mount_nodev(struct file_system_type *fs_type,
  2327. int flags, void *data,
  2328. int (*fill_super)(struct super_block *, void *, int));
  2329. extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
  2330. void retire_super(struct super_block *sb);
  2331. void generic_shutdown_super(struct super_block *sb);
  2332. void kill_block_super(struct super_block *sb);
  2333. void kill_anon_super(struct super_block *sb);
  2334. void kill_litter_super(struct super_block *sb);
  2335. void deactivate_super(struct super_block *sb);
  2336. void deactivate_locked_super(struct super_block *sb);
  2337. int set_anon_super(struct super_block *s, void *data);
  2338. int set_anon_super_fc(struct super_block *s, struct fs_context *fc);
  2339. int get_anon_bdev(dev_t *);
  2340. void free_anon_bdev(dev_t);
  2341. struct super_block *sget_fc(struct fs_context *fc,
  2342. int (*test)(struct super_block *, struct fs_context *),
  2343. int (*set)(struct super_block *, struct fs_context *));
  2344. struct super_block *sget(struct file_system_type *type,
  2345. int (*test)(struct super_block *,void *),
  2346. int (*set)(struct super_block *,void *),
  2347. int flags, void *data);
  2348. /* Alas, no aliases. Too much hassle with bringing module.h everywhere */
  2349. #define fops_get(fops) \
  2350. (((fops) && try_module_get((fops)->owner) ? (fops) : NULL))
  2351. #define fops_put(fops) \
  2352. do { if (fops) module_put((fops)->owner); } while(0)
  2353. /*
  2354. * This one is to be used *ONLY* from ->open() instances.
  2355. * fops must be non-NULL, pinned down *and* module dependencies
  2356. * should be sufficient to pin the caller down as well.
  2357. */
  2358. #define replace_fops(f, fops) \
  2359. do { \
  2360. struct file *__file = (f); \
  2361. fops_put(__file->f_op); \
  2362. BUG_ON(!(__file->f_op = (fops))); \
  2363. } while(0)
  2364. extern int register_filesystem(struct file_system_type *);
  2365. extern int unregister_filesystem(struct file_system_type *);
  2366. extern int vfs_statfs(const struct path *, struct kstatfs *);
  2367. extern int user_statfs(const char __user *, struct kstatfs *);
  2368. extern int fd_statfs(int, struct kstatfs *);
  2369. extern int freeze_super(struct super_block *super);
  2370. extern int thaw_super(struct super_block *super);
  2371. extern __printf(2, 3)
  2372. int super_setup_bdi_name(struct super_block *sb, char *fmt, ...);
  2373. extern int super_setup_bdi(struct super_block *sb);
  2374. extern int current_umask(void);
  2375. extern void ihold(struct inode * inode);
  2376. extern void iput(struct inode *);
  2377. extern int generic_update_time(struct inode *, struct timespec64 *, int);
  2378. /* /sys/fs */
  2379. extern struct kobject *fs_kobj;
  2380. #define MAX_RW_COUNT (INT_MAX & PAGE_MASK)
  2381. #ifdef CONFIG_FILE_LOCKING
  2382. static inline int break_lease(struct inode *inode, unsigned int mode)
  2383. {
  2384. /*
  2385. * Since this check is lockless, we must ensure that any refcounts
  2386. * taken are done before checking i_flctx->flc_lease. Otherwise, we
  2387. * could end up racing with tasks trying to set a new lease on this
  2388. * file.
  2389. */
  2390. smp_mb();
  2391. if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
  2392. return __break_lease(inode, mode, FL_LEASE);
  2393. return 0;
  2394. }
  2395. static inline int break_deleg(struct inode *inode, unsigned int mode)
  2396. {
  2397. /*
  2398. * Since this check is lockless, we must ensure that any refcounts
  2399. * taken are done before checking i_flctx->flc_lease. Otherwise, we
  2400. * could end up racing with tasks trying to set a new lease on this
  2401. * file.
  2402. */
  2403. smp_mb();
  2404. if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
  2405. return __break_lease(inode, mode, FL_DELEG);
  2406. return 0;
  2407. }
  2408. static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
  2409. {
  2410. int ret;
  2411. ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
  2412. if (ret == -EWOULDBLOCK && delegated_inode) {
  2413. *delegated_inode = inode;
  2414. ihold(inode);
  2415. }
  2416. return ret;
  2417. }
  2418. static inline int break_deleg_wait(struct inode **delegated_inode)
  2419. {
  2420. int ret;
  2421. ret = break_deleg(*delegated_inode, O_WRONLY);
  2422. iput(*delegated_inode);
  2423. *delegated_inode = NULL;
  2424. return ret;
  2425. }
  2426. static inline int break_layout(struct inode *inode, bool wait)
  2427. {
  2428. smp_mb();
  2429. if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
  2430. return __break_lease(inode,
  2431. wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
  2432. FL_LAYOUT);
  2433. return 0;
  2434. }
  2435. #else /* !CONFIG_FILE_LOCKING */
  2436. static inline int break_lease(struct inode *inode, unsigned int mode)
  2437. {
  2438. return 0;
  2439. }
  2440. static inline int break_deleg(struct inode *inode, unsigned int mode)
  2441. {
  2442. return 0;
  2443. }
  2444. static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
  2445. {
  2446. return 0;
  2447. }
  2448. static inline int break_deleg_wait(struct inode **delegated_inode)
  2449. {
  2450. BUG();
  2451. return 0;
  2452. }
  2453. static inline int break_layout(struct inode *inode, bool wait)
  2454. {
  2455. return 0;
  2456. }
  2457. #endif /* CONFIG_FILE_LOCKING */
  2458. /* fs/open.c */
  2459. struct audit_names;
  2460. struct filename {
  2461. const char *name; /* pointer to actual string */
  2462. const __user char *uptr; /* original userland pointer */
  2463. int refcnt;
  2464. struct audit_names *aname;
  2465. const char iname[];
  2466. };
  2467. static_assert(offsetof(struct filename, iname) % sizeof(long) == 0);
  2468. static inline struct user_namespace *file_mnt_user_ns(struct file *file)
  2469. {
  2470. return mnt_user_ns(file->f_path.mnt);
  2471. }
  2472. /**
  2473. * is_idmapped_mnt - check whether a mount is mapped
  2474. * @mnt: the mount to check
  2475. *
  2476. * If @mnt has an idmapping attached different from the
  2477. * filesystem's idmapping then @mnt is mapped.
  2478. *
  2479. * Return: true if mount is mapped, false if not.
  2480. */
  2481. static inline bool is_idmapped_mnt(const struct vfsmount *mnt)
  2482. {
  2483. return mnt_user_ns(mnt) != mnt->mnt_sb->s_user_ns;
  2484. }
  2485. extern long vfs_truncate(const struct path *, loff_t);
  2486. int do_truncate(struct user_namespace *, struct dentry *, loff_t start,
  2487. unsigned int time_attrs, struct file *filp);
  2488. extern int vfs_fallocate(struct file *file, int mode, loff_t offset,
  2489. loff_t len);
  2490. extern long do_sys_open(int dfd, const char __user *filename, int flags,
  2491. umode_t mode);
  2492. extern struct file *file_open_name(struct filename *, int, umode_t);
  2493. extern struct file *filp_open(const char *, int, umode_t);
  2494. extern struct file *filp_open_block(const char *, int, umode_t);
  2495. extern struct file *file_open_root(const struct path *,
  2496. const char *, int, umode_t);
  2497. static inline struct file *file_open_root_mnt(struct vfsmount *mnt,
  2498. const char *name, int flags, umode_t mode)
  2499. {
  2500. return file_open_root(&(struct path){.mnt = mnt, .dentry = mnt->mnt_root},
  2501. name, flags, mode);
  2502. }
  2503. extern struct file * dentry_open(const struct path *, int, const struct cred *);
  2504. extern struct file *dentry_create(const struct path *path, int flags,
  2505. umode_t mode, const struct cred *cred);
  2506. extern struct file * open_with_fake_path(const struct path *, int,
  2507. struct inode*, const struct cred *);
  2508. static inline struct file *file_clone_open(struct file *file)
  2509. {
  2510. return dentry_open(&file->f_path, file->f_flags, file->f_cred);
  2511. }
  2512. extern int filp_close(struct file *, fl_owner_t id);
  2513. extern struct filename *getname_flags(const char __user *, int, int *);
  2514. extern struct filename *getname_uflags(const char __user *, int);
  2515. extern struct filename *getname(const char __user *);
  2516. extern struct filename *getname_kernel(const char *);
  2517. extern void putname(struct filename *name);
  2518. extern int finish_open(struct file *file, struct dentry *dentry,
  2519. int (*open)(struct inode *, struct file *));
  2520. extern int finish_no_open(struct file *file, struct dentry *dentry);
  2521. /* Helper for the simple case when original dentry is used */
  2522. static inline int finish_open_simple(struct file *file, int error)
  2523. {
  2524. if (error)
  2525. return error;
  2526. return finish_open(file, file->f_path.dentry, NULL);
  2527. }
  2528. /* fs/dcache.c */
  2529. extern void __init vfs_caches_init_early(void);
  2530. extern void __init vfs_caches_init(void);
  2531. extern struct kmem_cache *names_cachep;
  2532. #define __getname() kmem_cache_alloc(names_cachep, GFP_KERNEL)
  2533. #define __putname(name) kmem_cache_free(names_cachep, (void *)(name))
  2534. extern struct super_block *blockdev_superblock;
  2535. static inline bool sb_is_blkdev_sb(struct super_block *sb)
  2536. {
  2537. return IS_ENABLED(CONFIG_BLOCK) && sb == blockdev_superblock;
  2538. }
  2539. void emergency_thaw_all(void);
  2540. extern int sync_filesystem(struct super_block *);
  2541. extern const struct file_operations def_blk_fops;
  2542. extern const struct file_operations def_chr_fops;
  2543. /* fs/char_dev.c */
  2544. #define CHRDEV_MAJOR_MAX 512
  2545. /* Marks the bottom of the first segment of free char majors */
  2546. #define CHRDEV_MAJOR_DYN_END 234
  2547. /* Marks the top and bottom of the second segment of free char majors */
  2548. #define CHRDEV_MAJOR_DYN_EXT_START 511
  2549. #define CHRDEV_MAJOR_DYN_EXT_END 384
  2550. extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
  2551. extern int register_chrdev_region(dev_t, unsigned, const char *);
  2552. extern int __register_chrdev(unsigned int major, unsigned int baseminor,
  2553. unsigned int count, const char *name,
  2554. const struct file_operations *fops);
  2555. extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,
  2556. unsigned int count, const char *name);
  2557. extern void unregister_chrdev_region(dev_t, unsigned);
  2558. extern void chrdev_show(struct seq_file *,off_t);
  2559. static inline int register_chrdev(unsigned int major, const char *name,
  2560. const struct file_operations *fops)
  2561. {
  2562. return __register_chrdev(major, 0, 256, name, fops);
  2563. }
  2564. static inline void unregister_chrdev(unsigned int major, const char *name)
  2565. {
  2566. __unregister_chrdev(major, 0, 256, name);
  2567. }
  2568. extern void init_special_inode(struct inode *, umode_t, dev_t);
  2569. /* Invalid inode operations -- fs/bad_inode.c */
  2570. extern void make_bad_inode(struct inode *);
  2571. extern bool is_bad_inode(struct inode *);
  2572. extern int __must_check file_fdatawait_range(struct file *file, loff_t lstart,
  2573. loff_t lend);
  2574. extern int __must_check file_check_and_advance_wb_err(struct file *file);
  2575. extern int __must_check file_write_and_wait_range(struct file *file,
  2576. loff_t start, loff_t end);
  2577. static inline int file_write_and_wait(struct file *file)
  2578. {
  2579. return file_write_and_wait_range(file, 0, LLONG_MAX);
  2580. }
  2581. extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
  2582. int datasync);
  2583. extern int vfs_fsync(struct file *file, int datasync);
  2584. extern int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
  2585. unsigned int flags);
  2586. static inline bool iocb_is_dsync(const struct kiocb *iocb)
  2587. {
  2588. return (iocb->ki_flags & IOCB_DSYNC) ||
  2589. IS_SYNC(iocb->ki_filp->f_mapping->host);
  2590. }
  2591. /*
  2592. * Sync the bytes written if this was a synchronous write. Expect ki_pos
  2593. * to already be updated for the write, and will return either the amount
  2594. * of bytes passed in, or an error if syncing the file failed.
  2595. */
  2596. static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
  2597. {
  2598. if (iocb_is_dsync(iocb)) {
  2599. int ret = vfs_fsync_range(iocb->ki_filp,
  2600. iocb->ki_pos - count, iocb->ki_pos - 1,
  2601. (iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
  2602. if (ret)
  2603. return ret;
  2604. }
  2605. return count;
  2606. }
  2607. extern void emergency_sync(void);
  2608. extern void emergency_remount(void);
  2609. #ifdef CONFIG_BLOCK
  2610. extern int bmap(struct inode *inode, sector_t *block);
  2611. #else
  2612. static inline int bmap(struct inode *inode, sector_t *block)
  2613. {
  2614. return -EINVAL;
  2615. }
  2616. #endif
  2617. int notify_change(struct user_namespace *, struct dentry *,
  2618. struct iattr *, struct inode **);
  2619. int inode_permission(struct user_namespace *, struct inode *, int);
  2620. int generic_permission(struct user_namespace *, struct inode *, int);
  2621. static inline int file_permission(struct file *file, int mask)
  2622. {
  2623. return inode_permission(file_mnt_user_ns(file),
  2624. file_inode(file), mask);
  2625. }
  2626. static inline int path_permission(const struct path *path, int mask)
  2627. {
  2628. return inode_permission(mnt_user_ns(path->mnt),
  2629. d_inode(path->dentry), mask);
  2630. }
  2631. int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
  2632. struct inode *inode);
  2633. static inline bool execute_ok(struct inode *inode)
  2634. {
  2635. return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
  2636. }
  2637. static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
  2638. {
  2639. return (inode->i_mode ^ mode) & S_IFMT;
  2640. }
  2641. static inline void file_start_write(struct file *file)
  2642. {
  2643. if (!S_ISREG(file_inode(file)->i_mode))
  2644. return;
  2645. sb_start_write(file_inode(file)->i_sb);
  2646. }
  2647. static inline bool file_start_write_trylock(struct file *file)
  2648. {
  2649. if (!S_ISREG(file_inode(file)->i_mode))
  2650. return true;
  2651. return sb_start_write_trylock(file_inode(file)->i_sb);
  2652. }
  2653. static inline void file_end_write(struct file *file)
  2654. {
  2655. if (!S_ISREG(file_inode(file)->i_mode))
  2656. return;
  2657. __sb_end_write(file_inode(file)->i_sb, SB_FREEZE_WRITE);
  2658. }
  2659. /*
  2660. * This is used for regular files where some users -- especially the
  2661. * currently executed binary in a process, previously handled via
  2662. * VM_DENYWRITE -- cannot handle concurrent write (and maybe mmap
  2663. * read-write shared) accesses.
  2664. *
  2665. * get_write_access() gets write permission for a file.
  2666. * put_write_access() releases this write permission.
  2667. * deny_write_access() denies write access to a file.
  2668. * allow_write_access() re-enables write access to a file.
  2669. *
  2670. * The i_writecount field of an inode can have the following values:
  2671. * 0: no write access, no denied write access
  2672. * < 0: (-i_writecount) users that denied write access to the file.
  2673. * > 0: (i_writecount) users that have write access to the file.
  2674. *
  2675. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  2676. * except for the cases where we don't hold i_writecount yet. Then we need to
  2677. * use {get,deny}_write_access() - these functions check the sign and refuse
  2678. * to do the change if sign is wrong.
  2679. */
  2680. static inline int get_write_access(struct inode *inode)
  2681. {
  2682. return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBSY;
  2683. }
  2684. static inline int deny_write_access(struct file *file)
  2685. {
  2686. struct inode *inode = file_inode(file);
  2687. return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
  2688. }
  2689. static inline void put_write_access(struct inode * inode)
  2690. {
  2691. atomic_dec(&inode->i_writecount);
  2692. }
  2693. static inline void allow_write_access(struct file *file)
  2694. {
  2695. if (file)
  2696. atomic_inc(&file_inode(file)->i_writecount);
  2697. }
  2698. static inline bool inode_is_open_for_write(const struct inode *inode)
  2699. {
  2700. return atomic_read(&inode->i_writecount) > 0;
  2701. }
  2702. #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
  2703. static inline void i_readcount_dec(struct inode *inode)
  2704. {
  2705. BUG_ON(!atomic_read(&inode->i_readcount));
  2706. atomic_dec(&inode->i_readcount);
  2707. }
  2708. static inline void i_readcount_inc(struct inode *inode)
  2709. {
  2710. atomic_inc(&inode->i_readcount);
  2711. }
  2712. #else
  2713. static inline void i_readcount_dec(struct inode *inode)
  2714. {
  2715. return;
  2716. }
  2717. static inline void i_readcount_inc(struct inode *inode)
  2718. {
  2719. return;
  2720. }
  2721. #endif
  2722. extern int do_pipe_flags(int *, int);
  2723. extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
  2724. ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
  2725. extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
  2726. extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
  2727. extern struct file * open_exec(const char *);
  2728. /* fs/dcache.c -- generic fs support functions */
  2729. extern bool is_subdir(struct dentry *, struct dentry *);
  2730. extern bool path_is_under(const struct path *, const struct path *);
  2731. extern char *file_path(struct file *, char *, int);
  2732. #include <linux/err.h>
  2733. /* needed for stackable file system support */
  2734. extern loff_t default_llseek(struct file *file, loff_t offset, int whence);
  2735. extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
  2736. extern int inode_init_always(struct super_block *, struct inode *);
  2737. extern void inode_init_once(struct inode *);
  2738. extern void address_space_init_once(struct address_space *mapping);
  2739. extern struct inode * igrab(struct inode *);
  2740. extern ino_t iunique(struct super_block *, ino_t);
  2741. extern int inode_needs_sync(struct inode *inode);
  2742. extern int generic_delete_inode(struct inode *inode);
  2743. static inline int generic_drop_inode(struct inode *inode)
  2744. {
  2745. return !inode->i_nlink || inode_unhashed(inode);
  2746. }
  2747. extern void d_mark_dontcache(struct inode *inode);
  2748. extern struct inode *ilookup5_nowait(struct super_block *sb,
  2749. unsigned long hashval, int (*test)(struct inode *, void *),
  2750. void *data);
  2751. extern struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  2752. int (*test)(struct inode *, void *), void *data);
  2753. extern struct inode *ilookup(struct super_block *sb, unsigned long ino);
  2754. extern struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
  2755. int (*test)(struct inode *, void *),
  2756. int (*set)(struct inode *, void *),
  2757. void *data);
  2758. extern struct inode * iget5_locked(struct super_block *, unsigned long, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *);
  2759. extern struct inode * iget_locked(struct super_block *, unsigned long);
  2760. extern struct inode *find_inode_nowait(struct super_block *,
  2761. unsigned long,
  2762. int (*match)(struct inode *,
  2763. unsigned long, void *),
  2764. void *data);
  2765. extern struct inode *find_inode_rcu(struct super_block *, unsigned long,
  2766. int (*)(struct inode *, void *), void *);
  2767. extern struct inode *find_inode_by_ino_rcu(struct super_block *, unsigned long);
  2768. extern int insert_inode_locked4(struct inode *, unsigned long, int (*test)(struct inode *, void *), void *);
  2769. extern int insert_inode_locked(struct inode *);
  2770. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2771. extern void lockdep_annotate_inode_mutex_key(struct inode *inode);
  2772. #else
  2773. static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
  2774. #endif
  2775. extern void unlock_new_inode(struct inode *);
  2776. extern void discard_new_inode(struct inode *);
  2777. extern unsigned int get_next_ino(void);
  2778. extern void evict_inodes(struct super_block *sb);
  2779. void dump_mapping(const struct address_space *);
  2780. /*
  2781. * Userspace may rely on the the inode number being non-zero. For example, glibc
  2782. * simply ignores files with zero i_ino in unlink() and other places.
  2783. *
  2784. * As an additional complication, if userspace was compiled with
  2785. * _FILE_OFFSET_BITS=32 on a 64-bit kernel we'll only end up reading out the
  2786. * lower 32 bits, so we need to check that those aren't zero explicitly. With
  2787. * _FILE_OFFSET_BITS=64, this may cause some harmless false-negatives, but
  2788. * better safe than sorry.
  2789. */
  2790. static inline bool is_zero_ino(ino_t ino)
  2791. {
  2792. return (u32)ino == 0;
  2793. }
  2794. extern void __iget(struct inode * inode);
  2795. extern void iget_failed(struct inode *);
  2796. extern void clear_inode(struct inode *);
  2797. extern void __destroy_inode(struct inode *);
  2798. extern struct inode *new_inode_pseudo(struct super_block *sb);
  2799. extern struct inode *new_inode(struct super_block *sb);
  2800. extern void free_inode_nonrcu(struct inode *inode);
  2801. extern int setattr_should_drop_suidgid(struct user_namespace *, struct inode *);
  2802. extern int file_remove_privs(struct file *);
  2803. int setattr_should_drop_sgid(struct user_namespace *mnt_userns,
  2804. const struct inode *inode);
  2805. /*
  2806. * This must be used for allocating filesystems specific inodes to set
  2807. * up the inode reclaim context correctly.
  2808. */
  2809. static inline void *
  2810. alloc_inode_sb(struct super_block *sb, struct kmem_cache *cache, gfp_t gfp)
  2811. {
  2812. return kmem_cache_alloc_lru(cache, &sb->s_inode_lru, gfp);
  2813. }
  2814. extern void __insert_inode_hash(struct inode *, unsigned long hashval);
  2815. static inline void insert_inode_hash(struct inode *inode)
  2816. {
  2817. __insert_inode_hash(inode, inode->i_ino);
  2818. }
  2819. extern void __remove_inode_hash(struct inode *);
  2820. static inline void remove_inode_hash(struct inode *inode)
  2821. {
  2822. if (!inode_unhashed(inode) && !hlist_fake(&inode->i_hash))
  2823. __remove_inode_hash(inode);
  2824. }
  2825. extern void inode_sb_list_add(struct inode *inode);
  2826. extern void inode_add_lru(struct inode *inode);
  2827. extern int sb_set_blocksize(struct super_block *, int);
  2828. extern int sb_min_blocksize(struct super_block *, int);
  2829. extern int generic_file_mmap(struct file *, struct vm_area_struct *);
  2830. extern int generic_file_readonly_mmap(struct file *, struct vm_area_struct *);
  2831. extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
  2832. int generic_write_checks_count(struct kiocb *iocb, loff_t *count);
  2833. extern int generic_write_check_limits(struct file *file, loff_t pos,
  2834. loff_t *count);
  2835. extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
  2836. ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *to,
  2837. ssize_t already_read);
  2838. extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
  2839. extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
  2840. extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
  2841. extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
  2842. ssize_t generic_perform_write(struct kiocb *, struct iov_iter *);
  2843. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
  2844. rwf_t flags);
  2845. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
  2846. rwf_t flags);
  2847. ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
  2848. struct iov_iter *iter);
  2849. ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
  2850. struct iov_iter *iter);
  2851. /* fs/splice.c */
  2852. extern ssize_t generic_file_splice_read(struct file *, loff_t *,
  2853. struct pipe_inode_info *, size_t, unsigned int);
  2854. extern ssize_t iter_file_splice_write(struct pipe_inode_info *,
  2855. struct file *, loff_t *, size_t, unsigned int);
  2856. extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
  2857. struct file *out, loff_t *, size_t len, unsigned int flags);
  2858. extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
  2859. loff_t *opos, size_t len, unsigned int flags);
  2860. extern void
  2861. file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
  2862. extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
  2863. #define no_llseek NULL
  2864. extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
  2865. extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
  2866. extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
  2867. int whence, loff_t maxsize, loff_t eof);
  2868. extern loff_t fixed_size_llseek(struct file *file, loff_t offset,
  2869. int whence, loff_t size);
  2870. extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t);
  2871. extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
  2872. int rw_verify_area(int, struct file *, const loff_t *, size_t);
  2873. extern int generic_file_open(struct inode * inode, struct file * filp);
  2874. extern int nonseekable_open(struct inode * inode, struct file * filp);
  2875. extern int stream_open(struct inode * inode, struct file * filp);
  2876. #ifdef CONFIG_BLOCK
  2877. typedef void (dio_submit_t)(struct bio *bio, struct inode *inode,
  2878. loff_t file_offset);
  2879. enum {
  2880. /* need locking between buffered and direct access */
  2881. DIO_LOCKING = 0x01,
  2882. /* filesystem does not support filling holes */
  2883. DIO_SKIP_HOLES = 0x02,
  2884. };
  2885. ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
  2886. struct block_device *bdev, struct iov_iter *iter,
  2887. get_block_t get_block,
  2888. dio_iodone_t end_io, dio_submit_t submit_io,
  2889. int flags);
  2890. static inline ssize_t blockdev_direct_IO(struct kiocb *iocb,
  2891. struct inode *inode,
  2892. struct iov_iter *iter,
  2893. get_block_t get_block)
  2894. {
  2895. return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
  2896. get_block, NULL, NULL, DIO_LOCKING | DIO_SKIP_HOLES);
  2897. }
  2898. #endif
  2899. void inode_dio_wait(struct inode *inode);
  2900. /**
  2901. * inode_dio_begin - signal start of a direct I/O requests
  2902. * @inode: inode the direct I/O happens on
  2903. *
  2904. * This is called once we've finished processing a direct I/O request,
  2905. * and is used to wake up callers waiting for direct I/O to be quiesced.
  2906. */
  2907. static inline void inode_dio_begin(struct inode *inode)
  2908. {
  2909. atomic_inc(&inode->i_dio_count);
  2910. }
  2911. /**
  2912. * inode_dio_end - signal finish of a direct I/O requests
  2913. * @inode: inode the direct I/O happens on
  2914. *
  2915. * This is called once we've finished processing a direct I/O request,
  2916. * and is used to wake up callers waiting for direct I/O to be quiesced.
  2917. */
  2918. static inline void inode_dio_end(struct inode *inode)
  2919. {
  2920. if (atomic_dec_and_test(&inode->i_dio_count))
  2921. wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
  2922. }
  2923. /*
  2924. * Warn about a page cache invalidation failure diring a direct I/O write.
  2925. */
  2926. void dio_warn_stale_pagecache(struct file *filp);
  2927. extern void inode_set_flags(struct inode *inode, unsigned int flags,
  2928. unsigned int mask);
  2929. extern const struct file_operations generic_ro_fops;
  2930. #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
  2931. extern int readlink_copy(char __user *, int, const char *);
  2932. extern int page_readlink(struct dentry *, char __user *, int);
  2933. extern const char *page_get_link(struct dentry *, struct inode *,
  2934. struct delayed_call *);
  2935. extern void page_put_link(void *);
  2936. extern int page_symlink(struct inode *inode, const char *symname, int len);
  2937. extern const struct inode_operations page_symlink_inode_operations;
  2938. extern void kfree_link(void *);
  2939. void generic_fillattr(struct user_namespace *, struct inode *, struct kstat *);
  2940. void generic_fill_statx_attr(struct inode *inode, struct kstat *stat);
  2941. extern int vfs_getattr_nosec(const struct path *, struct kstat *, u32, unsigned int);
  2942. extern int vfs_getattr(const struct path *, struct kstat *, u32, unsigned int);
  2943. void __inode_add_bytes(struct inode *inode, loff_t bytes);
  2944. void inode_add_bytes(struct inode *inode, loff_t bytes);
  2945. void __inode_sub_bytes(struct inode *inode, loff_t bytes);
  2946. void inode_sub_bytes(struct inode *inode, loff_t bytes);
  2947. static inline loff_t __inode_get_bytes(struct inode *inode)
  2948. {
  2949. return (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
  2950. }
  2951. loff_t inode_get_bytes(struct inode *inode);
  2952. void inode_set_bytes(struct inode *inode, loff_t bytes);
  2953. const char *simple_get_link(struct dentry *, struct inode *,
  2954. struct delayed_call *);
  2955. extern const struct inode_operations simple_symlink_inode_operations;
  2956. extern int iterate_dir(struct file *, struct dir_context *);
  2957. int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
  2958. int flags);
  2959. int vfs_fstat(int fd, struct kstat *stat);
  2960. static inline int vfs_stat(const char __user *filename, struct kstat *stat)
  2961. {
  2962. return vfs_fstatat(AT_FDCWD, filename, stat, 0);
  2963. }
  2964. static inline int vfs_lstat(const char __user *name, struct kstat *stat)
  2965. {
  2966. return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
  2967. }
  2968. extern const char *vfs_get_link(struct dentry *, struct delayed_call *);
  2969. extern int vfs_readlink(struct dentry *, char __user *, int);
  2970. extern struct file_system_type *get_filesystem(struct file_system_type *fs);
  2971. extern void put_filesystem(struct file_system_type *fs);
  2972. extern struct file_system_type *get_fs_type(const char *name);
  2973. extern struct super_block *get_super(struct block_device *);
  2974. extern struct super_block *get_active_super(struct block_device *bdev);
  2975. extern void drop_super(struct super_block *sb);
  2976. extern void drop_super_exclusive(struct super_block *sb);
  2977. extern void iterate_supers(void (*)(struct super_block *, void *), void *);
  2978. extern void iterate_supers_type(struct file_system_type *,
  2979. void (*)(struct super_block *, void *), void *);
  2980. extern int dcache_dir_open(struct inode *, struct file *);
  2981. extern int dcache_dir_close(struct inode *, struct file *);
  2982. extern loff_t dcache_dir_lseek(struct file *, loff_t, int);
  2983. extern int dcache_readdir(struct file *, struct dir_context *);
  2984. extern int simple_setattr(struct user_namespace *, struct dentry *,
  2985. struct iattr *);
  2986. extern int simple_getattr(struct user_namespace *, const struct path *,
  2987. struct kstat *, u32, unsigned int);
  2988. extern int simple_statfs(struct dentry *, struct kstatfs *);
  2989. extern int simple_open(struct inode *inode, struct file *file);
  2990. extern int simple_link(struct dentry *, struct inode *, struct dentry *);
  2991. extern int simple_unlink(struct inode *, struct dentry *);
  2992. extern int simple_rmdir(struct inode *, struct dentry *);
  2993. extern int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
  2994. struct inode *new_dir, struct dentry *new_dentry);
  2995. extern int simple_rename(struct user_namespace *, struct inode *,
  2996. struct dentry *, struct inode *, struct dentry *,
  2997. unsigned int);
  2998. extern void simple_recursive_removal(struct dentry *,
  2999. void (*callback)(struct dentry *));
  3000. extern int noop_fsync(struct file *, loff_t, loff_t, int);
  3001. extern ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
  3002. extern int simple_empty(struct dentry *);
  3003. extern int simple_write_begin(struct file *file, struct address_space *mapping,
  3004. loff_t pos, unsigned len,
  3005. struct page **pagep, void **fsdata);
  3006. extern const struct address_space_operations ram_aops;
  3007. extern int always_delete_dentry(const struct dentry *);
  3008. extern struct inode *alloc_anon_inode(struct super_block *);
  3009. extern int simple_nosetlease(struct file *, long, struct file_lock **, void **);
  3010. extern const struct dentry_operations simple_dentry_operations;
  3011. extern struct dentry *simple_lookup(struct inode *, struct dentry *, unsigned int flags);
  3012. extern ssize_t generic_read_dir(struct file *, char __user *, size_t, loff_t *);
  3013. extern const struct file_operations simple_dir_operations;
  3014. extern const struct inode_operations simple_dir_inode_operations;
  3015. extern void make_empty_dir_inode(struct inode *inode);
  3016. extern bool is_empty_dir_inode(struct inode *inode);
  3017. struct tree_descr { const char *name; const struct file_operations *ops; int mode; };
  3018. struct dentry *d_alloc_name(struct dentry *, const char *);
  3019. extern int simple_fill_super(struct super_block *, unsigned long,
  3020. const struct tree_descr *);
  3021. extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
  3022. extern void simple_release_fs(struct vfsmount **mount, int *count);
  3023. extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
  3024. loff_t *ppos, const void *from, size_t available);
  3025. extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
  3026. const void __user *from, size_t count);
  3027. extern int __generic_file_fsync(struct file *, loff_t, loff_t, int);
  3028. extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
  3029. extern int generic_check_addressable(unsigned, u64);
  3030. extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry);
  3031. int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
  3032. unsigned int ia_valid);
  3033. int setattr_prepare(struct user_namespace *, struct dentry *, struct iattr *);
  3034. extern int inode_newsize_ok(const struct inode *, loff_t offset);
  3035. void setattr_copy(struct user_namespace *, struct inode *inode,
  3036. const struct iattr *attr);
  3037. extern int file_update_time(struct file *file);
  3038. static inline bool vma_is_dax(const struct vm_area_struct *vma)
  3039. {
  3040. return vma->vm_file && IS_DAX(vma->vm_file->f_mapping->host);
  3041. }
  3042. static inline bool vma_is_fsdax(struct vm_area_struct *vma)
  3043. {
  3044. struct inode *inode;
  3045. if (!IS_ENABLED(CONFIG_FS_DAX) || !vma->vm_file)
  3046. return false;
  3047. if (!vma_is_dax(vma))
  3048. return false;
  3049. inode = file_inode(vma->vm_file);
  3050. if (S_ISCHR(inode->i_mode))
  3051. return false; /* device-dax */
  3052. return true;
  3053. }
  3054. static inline int iocb_flags(struct file *file)
  3055. {
  3056. int res = 0;
  3057. if (file->f_flags & O_APPEND)
  3058. res |= IOCB_APPEND;
  3059. if (file->f_flags & O_DIRECT)
  3060. res |= IOCB_DIRECT;
  3061. if (file->f_flags & O_DSYNC)
  3062. res |= IOCB_DSYNC;
  3063. if (file->f_flags & __O_SYNC)
  3064. res |= IOCB_SYNC;
  3065. return res;
  3066. }
  3067. static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
  3068. {
  3069. int kiocb_flags = 0;
  3070. /* make sure there's no overlap between RWF and private IOCB flags */
  3071. BUILD_BUG_ON((__force int) RWF_SUPPORTED & IOCB_EVENTFD);
  3072. if (!flags)
  3073. return 0;
  3074. if (unlikely(flags & ~RWF_SUPPORTED))
  3075. return -EOPNOTSUPP;
  3076. if (flags & RWF_NOWAIT) {
  3077. if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
  3078. return -EOPNOTSUPP;
  3079. kiocb_flags |= IOCB_NOIO;
  3080. }
  3081. kiocb_flags |= (__force int) (flags & RWF_SUPPORTED);
  3082. if (flags & RWF_SYNC)
  3083. kiocb_flags |= IOCB_DSYNC;
  3084. ki->ki_flags |= kiocb_flags;
  3085. return 0;
  3086. }
  3087. static inline rwf_t iocb_to_rw_flags(int ifl, int iocb_mask)
  3088. {
  3089. return ifl & iocb_mask;
  3090. }
  3091. static inline ino_t parent_ino(struct dentry *dentry)
  3092. {
  3093. ino_t res;
  3094. /*
  3095. * Don't strictly need d_lock here? If the parent ino could change
  3096. * then surely we'd have a deeper race in the caller?
  3097. */
  3098. spin_lock(&dentry->d_lock);
  3099. res = dentry->d_parent->d_inode->i_ino;
  3100. spin_unlock(&dentry->d_lock);
  3101. return res;
  3102. }
  3103. /* Transaction based IO helpers */
  3104. /*
  3105. * An argresp is stored in an allocated page and holds the
  3106. * size of the argument or response, along with its content
  3107. */
  3108. struct simple_transaction_argresp {
  3109. ssize_t size;
  3110. char data[];
  3111. };
  3112. #define SIMPLE_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct simple_transaction_argresp))
  3113. char *simple_transaction_get(struct file *file, const char __user *buf,
  3114. size_t size);
  3115. ssize_t simple_transaction_read(struct file *file, char __user *buf,
  3116. size_t size, loff_t *pos);
  3117. int simple_transaction_release(struct inode *inode, struct file *file);
  3118. void simple_transaction_set(struct file *file, size_t n);
  3119. /*
  3120. * simple attribute files
  3121. *
  3122. * These attributes behave similar to those in sysfs:
  3123. *
  3124. * Writing to an attribute immediately sets a value, an open file can be
  3125. * written to multiple times.
  3126. *
  3127. * Reading from an attribute creates a buffer from the value that might get
  3128. * read with multiple read calls. When the attribute has been read
  3129. * completely, no further read calls are possible until the file is opened
  3130. * again.
  3131. *
  3132. * All attributes contain a text representation of a numeric value
  3133. * that are accessed with the get() and set() functions.
  3134. */
  3135. #define DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
  3136. static int __fops ## _open(struct inode *inode, struct file *file) \
  3137. { \
  3138. __simple_attr_check_format(__fmt, 0ull); \
  3139. return simple_attr_open(inode, file, __get, __set, __fmt); \
  3140. } \
  3141. static const struct file_operations __fops = { \
  3142. .owner = THIS_MODULE, \
  3143. .open = __fops ## _open, \
  3144. .release = simple_attr_release, \
  3145. .read = simple_attr_read, \
  3146. .write = (__is_signed) ? simple_attr_write_signed : simple_attr_write, \
  3147. .llseek = generic_file_llseek, \
  3148. }
  3149. #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
  3150. DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
  3151. #define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
  3152. DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
  3153. static inline __printf(1, 2)
  3154. void __simple_attr_check_format(const char *fmt, ...)
  3155. {
  3156. /* don't do anything, just let the compiler check the arguments; */
  3157. }
  3158. int simple_attr_open(struct inode *inode, struct file *file,
  3159. int (*get)(void *, u64 *), int (*set)(void *, u64),
  3160. const char *fmt);
  3161. int simple_attr_release(struct inode *inode, struct file *file);
  3162. ssize_t simple_attr_read(struct file *file, char __user *buf,
  3163. size_t len, loff_t *ppos);
  3164. ssize_t simple_attr_write(struct file *file, const char __user *buf,
  3165. size_t len, loff_t *ppos);
  3166. ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
  3167. size_t len, loff_t *ppos);
  3168. struct ctl_table;
  3169. int __init list_bdev_fs_names(char *buf, size_t size);
  3170. #define __FMODE_EXEC ((__force int) FMODE_EXEC)
  3171. #define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY)
  3172. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  3173. #define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \
  3174. (flag & __FMODE_NONOTIFY)))
  3175. static inline bool is_sxid(umode_t mode)
  3176. {
  3177. return mode & (S_ISUID | S_ISGID);
  3178. }
  3179. static inline int check_sticky(struct user_namespace *mnt_userns,
  3180. struct inode *dir, struct inode *inode)
  3181. {
  3182. if (!(dir->i_mode & S_ISVTX))
  3183. return 0;
  3184. return __check_sticky(mnt_userns, dir, inode);
  3185. }
  3186. static inline void inode_has_no_xattr(struct inode *inode)
  3187. {
  3188. if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & SB_NOSEC))
  3189. inode->i_flags |= S_NOSEC;
  3190. }
  3191. static inline bool is_root_inode(struct inode *inode)
  3192. {
  3193. return inode == inode->i_sb->s_root->d_inode;
  3194. }
  3195. static inline bool dir_emit(struct dir_context *ctx,
  3196. const char *name, int namelen,
  3197. u64 ino, unsigned type)
  3198. {
  3199. return ctx->actor(ctx, name, namelen, ctx->pos, ino, type);
  3200. }
  3201. static inline bool dir_emit_dot(struct file *file, struct dir_context *ctx)
  3202. {
  3203. return ctx->actor(ctx, ".", 1, ctx->pos,
  3204. file->f_path.dentry->d_inode->i_ino, DT_DIR);
  3205. }
  3206. static inline bool dir_emit_dotdot(struct file *file, struct dir_context *ctx)
  3207. {
  3208. return ctx->actor(ctx, "..", 2, ctx->pos,
  3209. parent_ino(file->f_path.dentry), DT_DIR);
  3210. }
  3211. static inline bool dir_emit_dots(struct file *file, struct dir_context *ctx)
  3212. {
  3213. if (ctx->pos == 0) {
  3214. if (!dir_emit_dot(file, ctx))
  3215. return false;
  3216. ctx->pos = 1;
  3217. }
  3218. if (ctx->pos == 1) {
  3219. if (!dir_emit_dotdot(file, ctx))
  3220. return false;
  3221. ctx->pos = 2;
  3222. }
  3223. return true;
  3224. }
  3225. static inline bool dir_relax(struct inode *inode)
  3226. {
  3227. inode_unlock(inode);
  3228. inode_lock(inode);
  3229. return !IS_DEADDIR(inode);
  3230. }
  3231. static inline bool dir_relax_shared(struct inode *inode)
  3232. {
  3233. inode_unlock_shared(inode);
  3234. inode_lock_shared(inode);
  3235. return !IS_DEADDIR(inode);
  3236. }
  3237. extern bool path_noexec(const struct path *path);
  3238. extern void inode_nohighmem(struct inode *inode);
  3239. /* mm/fadvise.c */
  3240. extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
  3241. int advice);
  3242. extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
  3243. int advice);
  3244. #endif /* _LINUX_FS_H */