shmem.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/ramfs.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/file.h>
  30. #include <linux/mm.h>
  31. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/aio.h>
  34. static struct vfsmount *shm_mnt;
  35. #ifdef CONFIG_SHMEM
  36. /*
  37. * This virtual memory filesystem is heavily based on the ramfs. It
  38. * extends ramfs by the ability to use swap and honor resource limits
  39. * which makes it a completely usable filesystem.
  40. */
  41. #include <linux/xattr.h>
  42. #include <linux/exportfs.h>
  43. #include <linux/posix_acl.h>
  44. #include <linux/posix_acl_xattr.h>
  45. #include <linux/mman.h>
  46. #include <linux/string.h>
  47. #include <linux/slab.h>
  48. #include <linux/backing-dev.h>
  49. #include <linux/shmem_fs.h>
  50. #include <linux/writeback.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/pagevec.h>
  53. #include <linux/percpu_counter.h>
  54. #include <linux/falloc.h>
  55. #include <linux/splice.h>
  56. #include <linux/security.h>
  57. #include <linux/swapops.h>
  58. #include <linux/mempolicy.h>
  59. #include <linux/namei.h>
  60. #include <linux/ctype.h>
  61. #include <linux/migrate.h>
  62. #include <linux/highmem.h>
  63. #include <linux/seq_file.h>
  64. #include <linux/magic.h>
  65. #include <asm/uaccess.h>
  66. #include <asm/pgtable.h>
  67. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  68. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  69. /* Pretend that each entry is of this size in directory's i_size */
  70. #define BOGO_DIRENT_SIZE 20
  71. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  72. #define SHORT_SYMLINK_LEN 128
  73. /*
  74. * shmem_fallocate communicates with shmem_fault or shmem_writepage via
  75. * inode->i_private (with i_mutex making sure that it has only one user at
  76. * a time): we would prefer not to enlarge the shmem inode just for that.
  77. */
  78. struct shmem_falloc {
  79. wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
  80. pgoff_t start; /* start of range currently being fallocated */
  81. pgoff_t next; /* the next page offset to be fallocated */
  82. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  83. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  84. };
  85. /* Flag allocation requirements to shmem_getpage */
  86. enum sgp_type {
  87. SGP_READ, /* don't exceed i_size, don't allocate page */
  88. SGP_CACHE, /* don't exceed i_size, may allocate page */
  89. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  90. SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
  91. SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
  92. };
  93. #ifdef CONFIG_TMPFS
  94. static unsigned long shmem_default_max_blocks(void)
  95. {
  96. return totalram_pages / 2;
  97. }
  98. static unsigned long shmem_default_max_inodes(void)
  99. {
  100. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  101. }
  102. #endif
  103. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  104. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  105. struct shmem_inode_info *info, pgoff_t index);
  106. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  107. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  108. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  109. struct page **pagep, enum sgp_type sgp, int *fault_type)
  110. {
  111. return shmem_getpage_gfp(inode, index, pagep, sgp,
  112. mapping_gfp_mask(inode->i_mapping), fault_type);
  113. }
  114. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  115. {
  116. return sb->s_fs_info;
  117. }
  118. /*
  119. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  120. * for shared memory and for shared anonymous (/dev/zero) mappings
  121. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  122. * consistent with the pre-accounting of private mappings ...
  123. */
  124. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  125. {
  126. return (flags & VM_NORESERVE) ?
  127. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  128. }
  129. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  130. {
  131. if (!(flags & VM_NORESERVE))
  132. vm_unacct_memory(VM_ACCT(size));
  133. }
  134. /*
  135. * ... whereas tmpfs objects are accounted incrementally as
  136. * pages are allocated, in order to allow huge sparse files.
  137. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  138. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  139. */
  140. static inline int shmem_acct_block(unsigned long flags)
  141. {
  142. return (flags & VM_NORESERVE) ?
  143. security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  144. }
  145. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  146. {
  147. if (flags & VM_NORESERVE)
  148. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  149. }
  150. static const struct super_operations shmem_ops;
  151. static const struct address_space_operations shmem_aops;
  152. static const struct file_operations shmem_file_operations;
  153. static const struct inode_operations shmem_inode_operations;
  154. static const struct inode_operations shmem_dir_inode_operations;
  155. static const struct inode_operations shmem_special_inode_operations;
  156. static const struct vm_operations_struct shmem_vm_ops;
  157. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  158. .ra_pages = 0, /* No readahead */
  159. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  160. };
  161. static LIST_HEAD(shmem_swaplist);
  162. static DEFINE_MUTEX(shmem_swaplist_mutex);
  163. static int shmem_reserve_inode(struct super_block *sb)
  164. {
  165. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  166. if (sbinfo->max_inodes) {
  167. spin_lock(&sbinfo->stat_lock);
  168. if (!sbinfo->free_inodes) {
  169. spin_unlock(&sbinfo->stat_lock);
  170. return -ENOSPC;
  171. }
  172. sbinfo->free_inodes--;
  173. spin_unlock(&sbinfo->stat_lock);
  174. }
  175. return 0;
  176. }
  177. static void shmem_free_inode(struct super_block *sb)
  178. {
  179. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  180. if (sbinfo->max_inodes) {
  181. spin_lock(&sbinfo->stat_lock);
  182. sbinfo->free_inodes++;
  183. spin_unlock(&sbinfo->stat_lock);
  184. }
  185. }
  186. /**
  187. * shmem_recalc_inode - recalculate the block usage of an inode
  188. * @inode: inode to recalc
  189. *
  190. * We have to calculate the free blocks since the mm can drop
  191. * undirtied hole pages behind our back.
  192. *
  193. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  194. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  195. *
  196. * It has to be called with the spinlock held.
  197. */
  198. static void shmem_recalc_inode(struct inode *inode)
  199. {
  200. struct shmem_inode_info *info = SHMEM_I(inode);
  201. long freed;
  202. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  203. if (freed > 0) {
  204. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  205. if (sbinfo->max_blocks)
  206. percpu_counter_add(&sbinfo->used_blocks, -freed);
  207. info->alloced -= freed;
  208. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  209. shmem_unacct_blocks(info->flags, freed);
  210. }
  211. }
  212. /*
  213. * Replace item expected in radix tree by a new item, while holding tree lock.
  214. */
  215. static int shmem_radix_tree_replace(struct address_space *mapping,
  216. pgoff_t index, void *expected, void *replacement)
  217. {
  218. void **pslot;
  219. void *item;
  220. VM_BUG_ON(!expected);
  221. VM_BUG_ON(!replacement);
  222. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  223. if (!pslot)
  224. return -ENOENT;
  225. item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
  226. if (item != expected)
  227. return -ENOENT;
  228. radix_tree_replace_slot(pslot, replacement);
  229. return 0;
  230. }
  231. /*
  232. * Sometimes, before we decide whether to proceed or to fail, we must check
  233. * that an entry was not already brought back from swap by a racing thread.
  234. *
  235. * Checking page is not enough: by the time a SwapCache page is locked, it
  236. * might be reused, and again be SwapCache, using the same swap as before.
  237. */
  238. static bool shmem_confirm_swap(struct address_space *mapping,
  239. pgoff_t index, swp_entry_t swap)
  240. {
  241. void *item;
  242. rcu_read_lock();
  243. item = radix_tree_lookup(&mapping->page_tree, index);
  244. rcu_read_unlock();
  245. return item == swp_to_radix_entry(swap);
  246. }
  247. /*
  248. * Like add_to_page_cache_locked, but error if expected item has gone.
  249. */
  250. static int shmem_add_to_page_cache(struct page *page,
  251. struct address_space *mapping,
  252. pgoff_t index, gfp_t gfp, void *expected)
  253. {
  254. int error;
  255. VM_BUG_ON_PAGE(!PageLocked(page), page);
  256. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  257. page_cache_get(page);
  258. page->mapping = mapping;
  259. page->index = index;
  260. spin_lock_irq(&mapping->tree_lock);
  261. if (!expected)
  262. error = radix_tree_insert(&mapping->page_tree, index, page);
  263. else
  264. error = shmem_radix_tree_replace(mapping, index, expected,
  265. page);
  266. if (!error) {
  267. mapping->nrpages++;
  268. __inc_zone_page_state(page, NR_FILE_PAGES);
  269. __inc_zone_page_state(page, NR_SHMEM);
  270. spin_unlock_irq(&mapping->tree_lock);
  271. } else {
  272. page->mapping = NULL;
  273. spin_unlock_irq(&mapping->tree_lock);
  274. page_cache_release(page);
  275. }
  276. return error;
  277. }
  278. /*
  279. * Like delete_from_page_cache, but substitutes swap for page.
  280. */
  281. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  282. {
  283. struct address_space *mapping = page->mapping;
  284. int error;
  285. spin_lock_irq(&mapping->tree_lock);
  286. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  287. page->mapping = NULL;
  288. mapping->nrpages--;
  289. __dec_zone_page_state(page, NR_FILE_PAGES);
  290. __dec_zone_page_state(page, NR_SHMEM);
  291. spin_unlock_irq(&mapping->tree_lock);
  292. page_cache_release(page);
  293. BUG_ON(error);
  294. }
  295. /*
  296. * Remove swap entry from radix tree, free the swap and its page cache.
  297. */
  298. static int shmem_free_swap(struct address_space *mapping,
  299. pgoff_t index, void *radswap)
  300. {
  301. void *old;
  302. spin_lock_irq(&mapping->tree_lock);
  303. old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
  304. spin_unlock_irq(&mapping->tree_lock);
  305. if (old != radswap)
  306. return -ENOENT;
  307. free_swap_and_cache(radix_to_swp_entry(radswap));
  308. return 0;
  309. }
  310. /*
  311. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  312. */
  313. void shmem_unlock_mapping(struct address_space *mapping)
  314. {
  315. struct pagevec pvec;
  316. pgoff_t indices[PAGEVEC_SIZE];
  317. pgoff_t index = 0;
  318. pagevec_init(&pvec, 0);
  319. /*
  320. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  321. */
  322. while (!mapping_unevictable(mapping)) {
  323. /*
  324. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  325. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  326. */
  327. pvec.nr = find_get_entries(mapping, index,
  328. PAGEVEC_SIZE, pvec.pages, indices);
  329. if (!pvec.nr)
  330. break;
  331. index = indices[pvec.nr - 1] + 1;
  332. pagevec_remove_exceptionals(&pvec);
  333. check_move_unevictable_pages(pvec.pages, pvec.nr);
  334. pagevec_release(&pvec);
  335. cond_resched();
  336. }
  337. }
  338. /*
  339. * Remove range of pages and swap entries from radix tree, and free them.
  340. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  341. */
  342. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  343. bool unfalloc)
  344. {
  345. struct address_space *mapping = inode->i_mapping;
  346. struct shmem_inode_info *info = SHMEM_I(inode);
  347. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  348. pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
  349. unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
  350. unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
  351. struct pagevec pvec;
  352. pgoff_t indices[PAGEVEC_SIZE];
  353. long nr_swaps_freed = 0;
  354. pgoff_t index;
  355. int i;
  356. if (lend == -1)
  357. end = -1; /* unsigned, so actually very big */
  358. pagevec_init(&pvec, 0);
  359. index = start;
  360. while (index < end) {
  361. pvec.nr = find_get_entries(mapping, index,
  362. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  363. pvec.pages, indices);
  364. if (!pvec.nr)
  365. break;
  366. mem_cgroup_uncharge_start();
  367. for (i = 0; i < pagevec_count(&pvec); i++) {
  368. struct page *page = pvec.pages[i];
  369. index = indices[i];
  370. if (index >= end)
  371. break;
  372. if (radix_tree_exceptional_entry(page)) {
  373. if (unfalloc)
  374. continue;
  375. nr_swaps_freed += !shmem_free_swap(mapping,
  376. index, page);
  377. continue;
  378. }
  379. if (!trylock_page(page))
  380. continue;
  381. if (!unfalloc || !PageUptodate(page)) {
  382. if (page->mapping == mapping) {
  383. VM_BUG_ON_PAGE(PageWriteback(page), page);
  384. truncate_inode_page(mapping, page);
  385. }
  386. }
  387. unlock_page(page);
  388. }
  389. pagevec_remove_exceptionals(&pvec);
  390. pagevec_release(&pvec);
  391. mem_cgroup_uncharge_end();
  392. cond_resched();
  393. index++;
  394. }
  395. if (partial_start) {
  396. struct page *page = NULL;
  397. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  398. if (page) {
  399. unsigned int top = PAGE_CACHE_SIZE;
  400. if (start > end) {
  401. top = partial_end;
  402. partial_end = 0;
  403. }
  404. zero_user_segment(page, partial_start, top);
  405. set_page_dirty(page);
  406. unlock_page(page);
  407. page_cache_release(page);
  408. }
  409. }
  410. if (partial_end) {
  411. struct page *page = NULL;
  412. shmem_getpage(inode, end, &page, SGP_READ, NULL);
  413. if (page) {
  414. zero_user_segment(page, 0, partial_end);
  415. set_page_dirty(page);
  416. unlock_page(page);
  417. page_cache_release(page);
  418. }
  419. }
  420. if (start >= end)
  421. return;
  422. index = start;
  423. while (index < end) {
  424. cond_resched();
  425. pvec.nr = find_get_entries(mapping, index,
  426. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  427. pvec.pages, indices);
  428. if (!pvec.nr) {
  429. /* If all gone or hole-punch or unfalloc, we're done */
  430. if (index == start || end != -1)
  431. break;
  432. /* But if truncating, restart to make sure all gone */
  433. index = start;
  434. continue;
  435. }
  436. mem_cgroup_uncharge_start();
  437. for (i = 0; i < pagevec_count(&pvec); i++) {
  438. struct page *page = pvec.pages[i];
  439. index = indices[i];
  440. if (index >= end)
  441. break;
  442. if (radix_tree_exceptional_entry(page)) {
  443. if (unfalloc)
  444. continue;
  445. if (shmem_free_swap(mapping, index, page)) {
  446. /* Swap was replaced by page: retry */
  447. index--;
  448. break;
  449. }
  450. nr_swaps_freed++;
  451. continue;
  452. }
  453. lock_page(page);
  454. if (!unfalloc || !PageUptodate(page)) {
  455. if (page->mapping == mapping) {
  456. VM_BUG_ON_PAGE(PageWriteback(page), page);
  457. truncate_inode_page(mapping, page);
  458. } else {
  459. /* Page was replaced by swap: retry */
  460. unlock_page(page);
  461. index--;
  462. break;
  463. }
  464. }
  465. unlock_page(page);
  466. }
  467. pagevec_remove_exceptionals(&pvec);
  468. pagevec_release(&pvec);
  469. mem_cgroup_uncharge_end();
  470. index++;
  471. }
  472. spin_lock(&info->lock);
  473. info->swapped -= nr_swaps_freed;
  474. shmem_recalc_inode(inode);
  475. spin_unlock(&info->lock);
  476. }
  477. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  478. {
  479. shmem_undo_range(inode, lstart, lend, false);
  480. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  481. }
  482. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  483. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  484. {
  485. struct inode *inode = dentry->d_inode;
  486. int error;
  487. error = inode_change_ok(inode, attr);
  488. if (error)
  489. return error;
  490. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  491. loff_t oldsize = inode->i_size;
  492. loff_t newsize = attr->ia_size;
  493. if (newsize != oldsize) {
  494. i_size_write(inode, newsize);
  495. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  496. }
  497. if (newsize < oldsize) {
  498. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  499. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  500. shmem_truncate_range(inode, newsize, (loff_t)-1);
  501. /* unmap again to remove racily COWed private pages */
  502. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  503. }
  504. }
  505. setattr_copy(inode, attr);
  506. if (attr->ia_valid & ATTR_MODE)
  507. error = posix_acl_chmod(inode, inode->i_mode);
  508. return error;
  509. }
  510. static void shmem_evict_inode(struct inode *inode)
  511. {
  512. struct shmem_inode_info *info = SHMEM_I(inode);
  513. if (inode->i_mapping->a_ops == &shmem_aops) {
  514. shmem_unacct_size(info->flags, inode->i_size);
  515. inode->i_size = 0;
  516. shmem_truncate_range(inode, 0, (loff_t)-1);
  517. if (!list_empty(&info->swaplist)) {
  518. mutex_lock(&shmem_swaplist_mutex);
  519. list_del_init(&info->swaplist);
  520. mutex_unlock(&shmem_swaplist_mutex);
  521. }
  522. } else
  523. kfree(info->symlink);
  524. simple_xattrs_free(&info->xattrs);
  525. WARN_ON(inode->i_blocks);
  526. shmem_free_inode(inode->i_sb);
  527. clear_inode(inode);
  528. }
  529. /*
  530. * If swap found in inode, free it and move page from swapcache to filecache.
  531. */
  532. static int shmem_unuse_inode(struct shmem_inode_info *info,
  533. swp_entry_t swap, struct page **pagep)
  534. {
  535. struct address_space *mapping = info->vfs_inode.i_mapping;
  536. void *radswap;
  537. pgoff_t index;
  538. gfp_t gfp;
  539. int error = 0;
  540. radswap = swp_to_radix_entry(swap);
  541. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  542. if (index == -1)
  543. return 0;
  544. /*
  545. * Move _head_ to start search for next from here.
  546. * But be careful: shmem_evict_inode checks list_empty without taking
  547. * mutex, and there's an instant in list_move_tail when info->swaplist
  548. * would appear empty, if it were the only one on shmem_swaplist.
  549. */
  550. if (shmem_swaplist.next != &info->swaplist)
  551. list_move_tail(&shmem_swaplist, &info->swaplist);
  552. gfp = mapping_gfp_mask(mapping);
  553. if (shmem_should_replace_page(*pagep, gfp)) {
  554. mutex_unlock(&shmem_swaplist_mutex);
  555. error = shmem_replace_page(pagep, gfp, info, index);
  556. mutex_lock(&shmem_swaplist_mutex);
  557. /*
  558. * We needed to drop mutex to make that restrictive page
  559. * allocation, but the inode might have been freed while we
  560. * dropped it: although a racing shmem_evict_inode() cannot
  561. * complete without emptying the radix_tree, our page lock
  562. * on this swapcache page is not enough to prevent that -
  563. * free_swap_and_cache() of our swap entry will only
  564. * trylock_page(), removing swap from radix_tree whatever.
  565. *
  566. * We must not proceed to shmem_add_to_page_cache() if the
  567. * inode has been freed, but of course we cannot rely on
  568. * inode or mapping or info to check that. However, we can
  569. * safely check if our swap entry is still in use (and here
  570. * it can't have got reused for another page): if it's still
  571. * in use, then the inode cannot have been freed yet, and we
  572. * can safely proceed (if it's no longer in use, that tells
  573. * nothing about the inode, but we don't need to unuse swap).
  574. */
  575. if (!page_swapcount(*pagep))
  576. error = -ENOENT;
  577. }
  578. /*
  579. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  580. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  581. * beneath us (pagelock doesn't help until the page is in pagecache).
  582. */
  583. if (!error)
  584. error = shmem_add_to_page_cache(*pagep, mapping, index,
  585. GFP_NOWAIT, radswap);
  586. if (error != -ENOMEM) {
  587. /*
  588. * Truncation and eviction use free_swap_and_cache(), which
  589. * only does trylock page: if we raced, best clean up here.
  590. */
  591. delete_from_swap_cache(*pagep);
  592. set_page_dirty(*pagep);
  593. if (!error) {
  594. spin_lock(&info->lock);
  595. info->swapped--;
  596. spin_unlock(&info->lock);
  597. swap_free(swap);
  598. }
  599. error = 1; /* not an error, but entry was found */
  600. }
  601. return error;
  602. }
  603. /*
  604. * Search through swapped inodes to find and replace swap by page.
  605. */
  606. int shmem_unuse(swp_entry_t swap, struct page *page)
  607. {
  608. struct list_head *this, *next;
  609. struct shmem_inode_info *info;
  610. int found = 0;
  611. int error = 0;
  612. /*
  613. * There's a faint possibility that swap page was replaced before
  614. * caller locked it: caller will come back later with the right page.
  615. */
  616. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  617. goto out;
  618. /*
  619. * Charge page using GFP_KERNEL while we can wait, before taking
  620. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  621. * Charged back to the user (not to caller) when swap account is used.
  622. */
  623. error = mem_cgroup_charge_file(page, current->mm, GFP_KERNEL);
  624. if (error)
  625. goto out;
  626. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  627. mutex_lock(&shmem_swaplist_mutex);
  628. list_for_each_safe(this, next, &shmem_swaplist) {
  629. info = list_entry(this, struct shmem_inode_info, swaplist);
  630. if (info->swapped)
  631. found = shmem_unuse_inode(info, swap, &page);
  632. else
  633. list_del_init(&info->swaplist);
  634. cond_resched();
  635. if (found)
  636. break;
  637. }
  638. mutex_unlock(&shmem_swaplist_mutex);
  639. if (found < 0)
  640. error = found;
  641. out:
  642. unlock_page(page);
  643. page_cache_release(page);
  644. return error;
  645. }
  646. /*
  647. * Move the page from the page cache to the swap cache.
  648. */
  649. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  650. {
  651. struct shmem_inode_info *info;
  652. struct address_space *mapping;
  653. struct inode *inode;
  654. swp_entry_t swap;
  655. pgoff_t index;
  656. BUG_ON(!PageLocked(page));
  657. mapping = page->mapping;
  658. index = page->index;
  659. inode = mapping->host;
  660. info = SHMEM_I(inode);
  661. if (info->flags & VM_LOCKED)
  662. goto redirty;
  663. if (!total_swap_pages)
  664. goto redirty;
  665. /*
  666. * shmem_backing_dev_info's capabilities prevent regular writeback or
  667. * sync from ever calling shmem_writepage; but a stacking filesystem
  668. * might use ->writepage of its underlying filesystem, in which case
  669. * tmpfs should write out to swap only in response to memory pressure,
  670. * and not for the writeback threads or sync.
  671. */
  672. if (!wbc->for_reclaim) {
  673. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  674. goto redirty;
  675. }
  676. /*
  677. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  678. * value into swapfile.c, the only way we can correctly account for a
  679. * fallocated page arriving here is now to initialize it and write it.
  680. *
  681. * That's okay for a page already fallocated earlier, but if we have
  682. * not yet completed the fallocation, then (a) we want to keep track
  683. * of this page in case we have to undo it, and (b) it may not be a
  684. * good idea to continue anyway, once we're pushing into swap. So
  685. * reactivate the page, and let shmem_fallocate() quit when too many.
  686. */
  687. if (!PageUptodate(page)) {
  688. if (inode->i_private) {
  689. struct shmem_falloc *shmem_falloc;
  690. spin_lock(&inode->i_lock);
  691. shmem_falloc = inode->i_private;
  692. if (shmem_falloc &&
  693. !shmem_falloc->waitq &&
  694. index >= shmem_falloc->start &&
  695. index < shmem_falloc->next)
  696. shmem_falloc->nr_unswapped++;
  697. else
  698. shmem_falloc = NULL;
  699. spin_unlock(&inode->i_lock);
  700. if (shmem_falloc)
  701. goto redirty;
  702. }
  703. clear_highpage(page);
  704. flush_dcache_page(page);
  705. SetPageUptodate(page);
  706. }
  707. swap = get_swap_page();
  708. if (!swap.val)
  709. goto redirty;
  710. /*
  711. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  712. * if it's not already there. Do it now before the page is
  713. * moved to swap cache, when its pagelock no longer protects
  714. * the inode from eviction. But don't unlock the mutex until
  715. * we've incremented swapped, because shmem_unuse_inode() will
  716. * prune a !swapped inode from the swaplist under this mutex.
  717. */
  718. mutex_lock(&shmem_swaplist_mutex);
  719. if (list_empty(&info->swaplist))
  720. list_add_tail(&info->swaplist, &shmem_swaplist);
  721. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  722. swap_shmem_alloc(swap);
  723. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  724. spin_lock(&info->lock);
  725. info->swapped++;
  726. shmem_recalc_inode(inode);
  727. spin_unlock(&info->lock);
  728. mutex_unlock(&shmem_swaplist_mutex);
  729. BUG_ON(page_mapped(page));
  730. swap_writepage(page, wbc);
  731. return 0;
  732. }
  733. mutex_unlock(&shmem_swaplist_mutex);
  734. swapcache_free(swap, NULL);
  735. redirty:
  736. set_page_dirty(page);
  737. if (wbc->for_reclaim)
  738. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  739. unlock_page(page);
  740. return 0;
  741. }
  742. #ifdef CONFIG_NUMA
  743. #ifdef CONFIG_TMPFS
  744. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  745. {
  746. char buffer[64];
  747. if (!mpol || mpol->mode == MPOL_DEFAULT)
  748. return; /* show nothing */
  749. mpol_to_str(buffer, sizeof(buffer), mpol);
  750. seq_printf(seq, ",mpol=%s", buffer);
  751. }
  752. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  753. {
  754. struct mempolicy *mpol = NULL;
  755. if (sbinfo->mpol) {
  756. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  757. mpol = sbinfo->mpol;
  758. mpol_get(mpol);
  759. spin_unlock(&sbinfo->stat_lock);
  760. }
  761. return mpol;
  762. }
  763. #endif /* CONFIG_TMPFS */
  764. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  765. struct shmem_inode_info *info, pgoff_t index)
  766. {
  767. struct vm_area_struct pvma;
  768. struct page *page;
  769. /* Create a pseudo vma that just contains the policy */
  770. pvma.vm_start = 0;
  771. /* Bias interleave by inode number to distribute better across nodes */
  772. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  773. pvma.vm_ops = NULL;
  774. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  775. page = swapin_readahead(swap, gfp, &pvma, 0);
  776. /* Drop reference taken by mpol_shared_policy_lookup() */
  777. mpol_cond_put(pvma.vm_policy);
  778. return page;
  779. }
  780. static struct page *shmem_alloc_page(gfp_t gfp,
  781. struct shmem_inode_info *info, pgoff_t index)
  782. {
  783. struct vm_area_struct pvma;
  784. struct page *page;
  785. /* Create a pseudo vma that just contains the policy */
  786. pvma.vm_start = 0;
  787. /* Bias interleave by inode number to distribute better across nodes */
  788. pvma.vm_pgoff = index + info->vfs_inode.i_ino;
  789. pvma.vm_ops = NULL;
  790. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  791. page = alloc_page_vma(gfp, &pvma, 0);
  792. /* Drop reference taken by mpol_shared_policy_lookup() */
  793. mpol_cond_put(pvma.vm_policy);
  794. return page;
  795. }
  796. #else /* !CONFIG_NUMA */
  797. #ifdef CONFIG_TMPFS
  798. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  799. {
  800. }
  801. #endif /* CONFIG_TMPFS */
  802. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  803. struct shmem_inode_info *info, pgoff_t index)
  804. {
  805. return swapin_readahead(swap, gfp, NULL, 0);
  806. }
  807. static inline struct page *shmem_alloc_page(gfp_t gfp,
  808. struct shmem_inode_info *info, pgoff_t index)
  809. {
  810. return alloc_page(gfp);
  811. }
  812. #endif /* CONFIG_NUMA */
  813. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  814. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  815. {
  816. return NULL;
  817. }
  818. #endif
  819. /*
  820. * When a page is moved from swapcache to shmem filecache (either by the
  821. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  822. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  823. * ignorance of the mapping it belongs to. If that mapping has special
  824. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  825. * we may need to copy to a suitable page before moving to filecache.
  826. *
  827. * In a future release, this may well be extended to respect cpuset and
  828. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  829. * but for now it is a simple matter of zone.
  830. */
  831. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  832. {
  833. return page_zonenum(page) > gfp_zone(gfp);
  834. }
  835. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  836. struct shmem_inode_info *info, pgoff_t index)
  837. {
  838. struct page *oldpage, *newpage;
  839. struct address_space *swap_mapping;
  840. pgoff_t swap_index;
  841. int error;
  842. oldpage = *pagep;
  843. swap_index = page_private(oldpage);
  844. swap_mapping = page_mapping(oldpage);
  845. /*
  846. * We have arrived here because our zones are constrained, so don't
  847. * limit chance of success by further cpuset and node constraints.
  848. */
  849. gfp &= ~GFP_CONSTRAINT_MASK;
  850. newpage = shmem_alloc_page(gfp, info, index);
  851. if (!newpage)
  852. return -ENOMEM;
  853. page_cache_get(newpage);
  854. copy_highpage(newpage, oldpage);
  855. flush_dcache_page(newpage);
  856. __set_page_locked(newpage);
  857. SetPageUptodate(newpage);
  858. SetPageSwapBacked(newpage);
  859. set_page_private(newpage, swap_index);
  860. SetPageSwapCache(newpage);
  861. /*
  862. * Our caller will very soon move newpage out of swapcache, but it's
  863. * a nice clean interface for us to replace oldpage by newpage there.
  864. */
  865. spin_lock_irq(&swap_mapping->tree_lock);
  866. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  867. newpage);
  868. if (!error) {
  869. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  870. __dec_zone_page_state(oldpage, NR_FILE_PAGES);
  871. }
  872. spin_unlock_irq(&swap_mapping->tree_lock);
  873. if (unlikely(error)) {
  874. /*
  875. * Is this possible? I think not, now that our callers check
  876. * both PageSwapCache and page_private after getting page lock;
  877. * but be defensive. Reverse old to newpage for clear and free.
  878. */
  879. oldpage = newpage;
  880. } else {
  881. mem_cgroup_replace_page_cache(oldpage, newpage);
  882. lru_cache_add_anon(newpage);
  883. *pagep = newpage;
  884. }
  885. ClearPageSwapCache(oldpage);
  886. set_page_private(oldpage, 0);
  887. unlock_page(oldpage);
  888. page_cache_release(oldpage);
  889. page_cache_release(oldpage);
  890. return error;
  891. }
  892. /*
  893. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  894. *
  895. * If we allocate a new one we do not mark it dirty. That's up to the
  896. * vm. If we swap it in we mark it dirty since we also free the swap
  897. * entry since a page cannot live in both the swap and page cache
  898. */
  899. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  900. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  901. {
  902. struct address_space *mapping = inode->i_mapping;
  903. struct shmem_inode_info *info;
  904. struct shmem_sb_info *sbinfo;
  905. struct page *page;
  906. swp_entry_t swap;
  907. int error;
  908. int once = 0;
  909. int alloced = 0;
  910. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  911. return -EFBIG;
  912. repeat:
  913. swap.val = 0;
  914. page = find_lock_entry(mapping, index);
  915. if (radix_tree_exceptional_entry(page)) {
  916. swap = radix_to_swp_entry(page);
  917. page = NULL;
  918. }
  919. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  920. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  921. error = -EINVAL;
  922. goto failed;
  923. }
  924. if (page && sgp == SGP_WRITE)
  925. mark_page_accessed(page);
  926. /* fallocated page? */
  927. if (page && !PageUptodate(page)) {
  928. if (sgp != SGP_READ)
  929. goto clear;
  930. unlock_page(page);
  931. page_cache_release(page);
  932. page = NULL;
  933. }
  934. if (page || (sgp == SGP_READ && !swap.val)) {
  935. *pagep = page;
  936. return 0;
  937. }
  938. /*
  939. * Fast cache lookup did not find it:
  940. * bring it back from swap or allocate.
  941. */
  942. info = SHMEM_I(inode);
  943. sbinfo = SHMEM_SB(inode->i_sb);
  944. if (swap.val) {
  945. /* Look it up and read it in.. */
  946. page = lookup_swap_cache(swap);
  947. if (!page) {
  948. /* here we actually do the io */
  949. if (fault_type)
  950. *fault_type |= VM_FAULT_MAJOR;
  951. page = shmem_swapin(swap, gfp, info, index);
  952. if (!page) {
  953. error = -ENOMEM;
  954. goto failed;
  955. }
  956. }
  957. /* We have to do this with page locked to prevent races */
  958. lock_page(page);
  959. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  960. !shmem_confirm_swap(mapping, index, swap)) {
  961. error = -EEXIST; /* try again */
  962. goto unlock;
  963. }
  964. if (!PageUptodate(page)) {
  965. error = -EIO;
  966. goto failed;
  967. }
  968. wait_on_page_writeback(page);
  969. if (shmem_should_replace_page(page, gfp)) {
  970. error = shmem_replace_page(&page, gfp, info, index);
  971. if (error)
  972. goto failed;
  973. }
  974. error = mem_cgroup_charge_file(page, current->mm,
  975. gfp & GFP_RECLAIM_MASK);
  976. if (!error) {
  977. error = shmem_add_to_page_cache(page, mapping, index,
  978. gfp, swp_to_radix_entry(swap));
  979. /*
  980. * We already confirmed swap under page lock, and make
  981. * no memory allocation here, so usually no possibility
  982. * of error; but free_swap_and_cache() only trylocks a
  983. * page, so it is just possible that the entry has been
  984. * truncated or holepunched since swap was confirmed.
  985. * shmem_undo_range() will have done some of the
  986. * unaccounting, now delete_from_swap_cache() will do
  987. * the rest (including mem_cgroup_uncharge_swapcache).
  988. * Reset swap.val? No, leave it so "failed" goes back to
  989. * "repeat": reading a hole and writing should succeed.
  990. */
  991. if (error)
  992. delete_from_swap_cache(page);
  993. }
  994. if (error)
  995. goto failed;
  996. spin_lock(&info->lock);
  997. info->swapped--;
  998. shmem_recalc_inode(inode);
  999. spin_unlock(&info->lock);
  1000. if (sgp == SGP_WRITE)
  1001. mark_page_accessed(page);
  1002. delete_from_swap_cache(page);
  1003. set_page_dirty(page);
  1004. swap_free(swap);
  1005. } else {
  1006. if (shmem_acct_block(info->flags)) {
  1007. error = -ENOSPC;
  1008. goto failed;
  1009. }
  1010. if (sbinfo->max_blocks) {
  1011. if (percpu_counter_compare(&sbinfo->used_blocks,
  1012. sbinfo->max_blocks) >= 0) {
  1013. error = -ENOSPC;
  1014. goto unacct;
  1015. }
  1016. percpu_counter_inc(&sbinfo->used_blocks);
  1017. }
  1018. page = shmem_alloc_page(gfp, info, index);
  1019. if (!page) {
  1020. error = -ENOMEM;
  1021. goto decused;
  1022. }
  1023. __SetPageSwapBacked(page);
  1024. __set_page_locked(page);
  1025. if (sgp == SGP_WRITE)
  1026. init_page_accessed(page);
  1027. error = mem_cgroup_charge_file(page, current->mm,
  1028. gfp & GFP_RECLAIM_MASK);
  1029. if (error)
  1030. goto decused;
  1031. error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
  1032. if (!error) {
  1033. error = shmem_add_to_page_cache(page, mapping, index,
  1034. gfp, NULL);
  1035. radix_tree_preload_end();
  1036. }
  1037. if (error) {
  1038. mem_cgroup_uncharge_cache_page(page);
  1039. goto decused;
  1040. }
  1041. lru_cache_add_anon(page);
  1042. spin_lock(&info->lock);
  1043. info->alloced++;
  1044. inode->i_blocks += BLOCKS_PER_PAGE;
  1045. shmem_recalc_inode(inode);
  1046. spin_unlock(&info->lock);
  1047. alloced = true;
  1048. /*
  1049. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1050. */
  1051. if (sgp == SGP_FALLOC)
  1052. sgp = SGP_WRITE;
  1053. clear:
  1054. /*
  1055. * Let SGP_WRITE caller clear ends if write does not fill page;
  1056. * but SGP_FALLOC on a page fallocated earlier must initialize
  1057. * it now, lest undo on failure cancel our earlier guarantee.
  1058. */
  1059. if (sgp != SGP_WRITE) {
  1060. clear_highpage(page);
  1061. flush_dcache_page(page);
  1062. SetPageUptodate(page);
  1063. }
  1064. if (sgp == SGP_DIRTY)
  1065. set_page_dirty(page);
  1066. }
  1067. /* Perhaps the file has been truncated since we checked */
  1068. if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
  1069. ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
  1070. error = -EINVAL;
  1071. if (alloced)
  1072. goto trunc;
  1073. else
  1074. goto failed;
  1075. }
  1076. *pagep = page;
  1077. return 0;
  1078. /*
  1079. * Error recovery.
  1080. */
  1081. trunc:
  1082. info = SHMEM_I(inode);
  1083. ClearPageDirty(page);
  1084. delete_from_page_cache(page);
  1085. spin_lock(&info->lock);
  1086. info->alloced--;
  1087. inode->i_blocks -= BLOCKS_PER_PAGE;
  1088. spin_unlock(&info->lock);
  1089. decused:
  1090. sbinfo = SHMEM_SB(inode->i_sb);
  1091. if (sbinfo->max_blocks)
  1092. percpu_counter_add(&sbinfo->used_blocks, -1);
  1093. unacct:
  1094. shmem_unacct_blocks(info->flags, 1);
  1095. failed:
  1096. if (swap.val && error != -EINVAL &&
  1097. !shmem_confirm_swap(mapping, index, swap))
  1098. error = -EEXIST;
  1099. unlock:
  1100. if (page) {
  1101. unlock_page(page);
  1102. page_cache_release(page);
  1103. }
  1104. if (error == -ENOSPC && !once++) {
  1105. info = SHMEM_I(inode);
  1106. spin_lock(&info->lock);
  1107. shmem_recalc_inode(inode);
  1108. spin_unlock(&info->lock);
  1109. goto repeat;
  1110. }
  1111. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1112. goto repeat;
  1113. return error;
  1114. }
  1115. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1116. {
  1117. struct inode *inode = file_inode(vma->vm_file);
  1118. int error;
  1119. int ret = VM_FAULT_LOCKED;
  1120. /*
  1121. * Trinity finds that probing a hole which tmpfs is punching can
  1122. * prevent the hole-punch from ever completing: which in turn
  1123. * locks writers out with its hold on i_mutex. So refrain from
  1124. * faulting pages into the hole while it's being punched. Although
  1125. * shmem_undo_range() does remove the additions, it may be unable to
  1126. * keep up, as each new page needs its own unmap_mapping_range() call,
  1127. * and the i_mmap tree grows ever slower to scan if new vmas are added.
  1128. *
  1129. * It does not matter if we sometimes reach this check just before the
  1130. * hole-punch begins, so that one fault then races with the punch:
  1131. * we just need to make racing faults a rare case.
  1132. *
  1133. * The implementation below would be much simpler if we just used a
  1134. * standard mutex or completion: but we cannot take i_mutex in fault,
  1135. * and bloating every shmem inode for this unlikely case would be sad.
  1136. */
  1137. if (unlikely(inode->i_private)) {
  1138. struct shmem_falloc *shmem_falloc;
  1139. spin_lock(&inode->i_lock);
  1140. shmem_falloc = inode->i_private;
  1141. if (shmem_falloc &&
  1142. shmem_falloc->waitq &&
  1143. vmf->pgoff >= shmem_falloc->start &&
  1144. vmf->pgoff < shmem_falloc->next) {
  1145. wait_queue_head_t *shmem_falloc_waitq;
  1146. DEFINE_WAIT(shmem_fault_wait);
  1147. ret = VM_FAULT_NOPAGE;
  1148. if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
  1149. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  1150. /* It's polite to up mmap_sem if we can */
  1151. up_read(&vma->vm_mm->mmap_sem);
  1152. ret = VM_FAULT_RETRY;
  1153. }
  1154. shmem_falloc_waitq = shmem_falloc->waitq;
  1155. prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
  1156. TASK_UNINTERRUPTIBLE);
  1157. spin_unlock(&inode->i_lock);
  1158. schedule();
  1159. /*
  1160. * shmem_falloc_waitq points into the shmem_fallocate()
  1161. * stack of the hole-punching task: shmem_falloc_waitq
  1162. * is usually invalid by the time we reach here, but
  1163. * finish_wait() does not dereference it in that case;
  1164. * though i_lock needed lest racing with wake_up_all().
  1165. */
  1166. spin_lock(&inode->i_lock);
  1167. finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
  1168. spin_unlock(&inode->i_lock);
  1169. return ret;
  1170. }
  1171. spin_unlock(&inode->i_lock);
  1172. }
  1173. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  1174. if (error)
  1175. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1176. if (ret & VM_FAULT_MAJOR) {
  1177. count_vm_event(PGMAJFAULT);
  1178. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1179. }
  1180. return ret;
  1181. }
  1182. #ifdef CONFIG_NUMA
  1183. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1184. {
  1185. struct inode *inode = file_inode(vma->vm_file);
  1186. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1187. }
  1188. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1189. unsigned long addr)
  1190. {
  1191. struct inode *inode = file_inode(vma->vm_file);
  1192. pgoff_t index;
  1193. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1194. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1195. }
  1196. #endif
  1197. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1198. {
  1199. struct inode *inode = file_inode(file);
  1200. struct shmem_inode_info *info = SHMEM_I(inode);
  1201. int retval = -ENOMEM;
  1202. spin_lock(&info->lock);
  1203. if (lock && !(info->flags & VM_LOCKED)) {
  1204. if (!user_shm_lock(inode->i_size, user))
  1205. goto out_nomem;
  1206. info->flags |= VM_LOCKED;
  1207. mapping_set_unevictable(file->f_mapping);
  1208. }
  1209. if (!lock && (info->flags & VM_LOCKED) && user) {
  1210. user_shm_unlock(inode->i_size, user);
  1211. info->flags &= ~VM_LOCKED;
  1212. mapping_clear_unevictable(file->f_mapping);
  1213. }
  1214. retval = 0;
  1215. out_nomem:
  1216. spin_unlock(&info->lock);
  1217. return retval;
  1218. }
  1219. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1220. {
  1221. file_accessed(file);
  1222. vma->vm_ops = &shmem_vm_ops;
  1223. return 0;
  1224. }
  1225. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1226. umode_t mode, dev_t dev, unsigned long flags)
  1227. {
  1228. struct inode *inode;
  1229. struct shmem_inode_info *info;
  1230. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1231. if (shmem_reserve_inode(sb))
  1232. return NULL;
  1233. inode = new_inode(sb);
  1234. if (inode) {
  1235. inode->i_ino = get_next_ino();
  1236. inode_init_owner(inode, dir, mode);
  1237. inode->i_blocks = 0;
  1238. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1239. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1240. inode->i_generation = get_seconds();
  1241. info = SHMEM_I(inode);
  1242. memset(info, 0, (char *)inode - (char *)info);
  1243. spin_lock_init(&info->lock);
  1244. info->flags = flags & VM_NORESERVE;
  1245. INIT_LIST_HEAD(&info->swaplist);
  1246. simple_xattrs_init(&info->xattrs);
  1247. cache_no_acl(inode);
  1248. switch (mode & S_IFMT) {
  1249. default:
  1250. inode->i_op = &shmem_special_inode_operations;
  1251. init_special_inode(inode, mode, dev);
  1252. break;
  1253. case S_IFREG:
  1254. inode->i_mapping->a_ops = &shmem_aops;
  1255. inode->i_op = &shmem_inode_operations;
  1256. inode->i_fop = &shmem_file_operations;
  1257. mpol_shared_policy_init(&info->policy,
  1258. shmem_get_sbmpol(sbinfo));
  1259. break;
  1260. case S_IFDIR:
  1261. inc_nlink(inode);
  1262. /* Some things misbehave if size == 0 on a directory */
  1263. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1264. inode->i_op = &shmem_dir_inode_operations;
  1265. inode->i_fop = &simple_dir_operations;
  1266. break;
  1267. case S_IFLNK:
  1268. /*
  1269. * Must not load anything in the rbtree,
  1270. * mpol_free_shared_policy will not be called.
  1271. */
  1272. mpol_shared_policy_init(&info->policy, NULL);
  1273. break;
  1274. }
  1275. } else
  1276. shmem_free_inode(sb);
  1277. return inode;
  1278. }
  1279. bool shmem_mapping(struct address_space *mapping)
  1280. {
  1281. return mapping->backing_dev_info == &shmem_backing_dev_info;
  1282. }
  1283. #ifdef CONFIG_TMPFS
  1284. static const struct inode_operations shmem_symlink_inode_operations;
  1285. static const struct inode_operations shmem_short_symlink_operations;
  1286. #ifdef CONFIG_TMPFS_XATTR
  1287. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1288. #else
  1289. #define shmem_initxattrs NULL
  1290. #endif
  1291. static int
  1292. shmem_write_begin(struct file *file, struct address_space *mapping,
  1293. loff_t pos, unsigned len, unsigned flags,
  1294. struct page **pagep, void **fsdata)
  1295. {
  1296. struct inode *inode = mapping->host;
  1297. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1298. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1299. }
  1300. static int
  1301. shmem_write_end(struct file *file, struct address_space *mapping,
  1302. loff_t pos, unsigned len, unsigned copied,
  1303. struct page *page, void *fsdata)
  1304. {
  1305. struct inode *inode = mapping->host;
  1306. if (pos + copied > inode->i_size)
  1307. i_size_write(inode, pos + copied);
  1308. if (!PageUptodate(page)) {
  1309. if (copied < PAGE_CACHE_SIZE) {
  1310. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1311. zero_user_segments(page, 0, from,
  1312. from + copied, PAGE_CACHE_SIZE);
  1313. }
  1314. SetPageUptodate(page);
  1315. }
  1316. set_page_dirty(page);
  1317. unlock_page(page);
  1318. page_cache_release(page);
  1319. return copied;
  1320. }
  1321. static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1322. {
  1323. struct file *file = iocb->ki_filp;
  1324. struct inode *inode = file_inode(file);
  1325. struct address_space *mapping = inode->i_mapping;
  1326. pgoff_t index;
  1327. unsigned long offset;
  1328. enum sgp_type sgp = SGP_READ;
  1329. int error = 0;
  1330. ssize_t retval = 0;
  1331. loff_t *ppos = &iocb->ki_pos;
  1332. /*
  1333. * Might this read be for a stacking filesystem? Then when reading
  1334. * holes of a sparse file, we actually need to allocate those pages,
  1335. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1336. */
  1337. if (segment_eq(get_fs(), KERNEL_DS))
  1338. sgp = SGP_DIRTY;
  1339. index = *ppos >> PAGE_CACHE_SHIFT;
  1340. offset = *ppos & ~PAGE_CACHE_MASK;
  1341. for (;;) {
  1342. struct page *page = NULL;
  1343. pgoff_t end_index;
  1344. unsigned long nr, ret;
  1345. loff_t i_size = i_size_read(inode);
  1346. end_index = i_size >> PAGE_CACHE_SHIFT;
  1347. if (index > end_index)
  1348. break;
  1349. if (index == end_index) {
  1350. nr = i_size & ~PAGE_CACHE_MASK;
  1351. if (nr <= offset)
  1352. break;
  1353. }
  1354. error = shmem_getpage(inode, index, &page, sgp, NULL);
  1355. if (error) {
  1356. if (error == -EINVAL)
  1357. error = 0;
  1358. break;
  1359. }
  1360. if (page)
  1361. unlock_page(page);
  1362. /*
  1363. * We must evaluate after, since reads (unlike writes)
  1364. * are called without i_mutex protection against truncate
  1365. */
  1366. nr = PAGE_CACHE_SIZE;
  1367. i_size = i_size_read(inode);
  1368. end_index = i_size >> PAGE_CACHE_SHIFT;
  1369. if (index == end_index) {
  1370. nr = i_size & ~PAGE_CACHE_MASK;
  1371. if (nr <= offset) {
  1372. if (page)
  1373. page_cache_release(page);
  1374. break;
  1375. }
  1376. }
  1377. nr -= offset;
  1378. if (page) {
  1379. /*
  1380. * If users can be writing to this page using arbitrary
  1381. * virtual addresses, take care about potential aliasing
  1382. * before reading the page on the kernel side.
  1383. */
  1384. if (mapping_writably_mapped(mapping))
  1385. flush_dcache_page(page);
  1386. /*
  1387. * Mark the page accessed if we read the beginning.
  1388. */
  1389. if (!offset)
  1390. mark_page_accessed(page);
  1391. } else {
  1392. page = ZERO_PAGE(0);
  1393. page_cache_get(page);
  1394. }
  1395. /*
  1396. * Ok, we have the page, and it's up-to-date, so
  1397. * now we can copy it to user space...
  1398. */
  1399. ret = copy_page_to_iter(page, offset, nr, to);
  1400. retval += ret;
  1401. offset += ret;
  1402. index += offset >> PAGE_CACHE_SHIFT;
  1403. offset &= ~PAGE_CACHE_MASK;
  1404. page_cache_release(page);
  1405. if (!iov_iter_count(to))
  1406. break;
  1407. if (ret < nr) {
  1408. error = -EFAULT;
  1409. break;
  1410. }
  1411. cond_resched();
  1412. }
  1413. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1414. file_accessed(file);
  1415. return retval ? retval : error;
  1416. }
  1417. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1418. struct pipe_inode_info *pipe, size_t len,
  1419. unsigned int flags)
  1420. {
  1421. struct address_space *mapping = in->f_mapping;
  1422. struct inode *inode = mapping->host;
  1423. unsigned int loff, nr_pages, req_pages;
  1424. struct page *pages[PIPE_DEF_BUFFERS];
  1425. struct partial_page partial[PIPE_DEF_BUFFERS];
  1426. struct page *page;
  1427. pgoff_t index, end_index;
  1428. loff_t isize, left;
  1429. int error, page_nr;
  1430. struct splice_pipe_desc spd = {
  1431. .pages = pages,
  1432. .partial = partial,
  1433. .nr_pages_max = PIPE_DEF_BUFFERS,
  1434. .flags = flags,
  1435. .ops = &page_cache_pipe_buf_ops,
  1436. .spd_release = spd_release_page,
  1437. };
  1438. isize = i_size_read(inode);
  1439. if (unlikely(*ppos >= isize))
  1440. return 0;
  1441. left = isize - *ppos;
  1442. if (unlikely(left < len))
  1443. len = left;
  1444. if (splice_grow_spd(pipe, &spd))
  1445. return -ENOMEM;
  1446. index = *ppos >> PAGE_CACHE_SHIFT;
  1447. loff = *ppos & ~PAGE_CACHE_MASK;
  1448. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1449. nr_pages = min(req_pages, spd.nr_pages_max);
  1450. spd.nr_pages = find_get_pages_contig(mapping, index,
  1451. nr_pages, spd.pages);
  1452. index += spd.nr_pages;
  1453. error = 0;
  1454. while (spd.nr_pages < nr_pages) {
  1455. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1456. if (error)
  1457. break;
  1458. unlock_page(page);
  1459. spd.pages[spd.nr_pages++] = page;
  1460. index++;
  1461. }
  1462. index = *ppos >> PAGE_CACHE_SHIFT;
  1463. nr_pages = spd.nr_pages;
  1464. spd.nr_pages = 0;
  1465. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1466. unsigned int this_len;
  1467. if (!len)
  1468. break;
  1469. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1470. page = spd.pages[page_nr];
  1471. if (!PageUptodate(page) || page->mapping != mapping) {
  1472. error = shmem_getpage(inode, index, &page,
  1473. SGP_CACHE, NULL);
  1474. if (error)
  1475. break;
  1476. unlock_page(page);
  1477. page_cache_release(spd.pages[page_nr]);
  1478. spd.pages[page_nr] = page;
  1479. }
  1480. isize = i_size_read(inode);
  1481. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1482. if (unlikely(!isize || index > end_index))
  1483. break;
  1484. if (end_index == index) {
  1485. unsigned int plen;
  1486. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1487. if (plen <= loff)
  1488. break;
  1489. this_len = min(this_len, plen - loff);
  1490. len = this_len;
  1491. }
  1492. spd.partial[page_nr].offset = loff;
  1493. spd.partial[page_nr].len = this_len;
  1494. len -= this_len;
  1495. loff = 0;
  1496. spd.nr_pages++;
  1497. index++;
  1498. }
  1499. while (page_nr < nr_pages)
  1500. page_cache_release(spd.pages[page_nr++]);
  1501. if (spd.nr_pages)
  1502. error = splice_to_pipe(pipe, &spd);
  1503. splice_shrink_spd(&spd);
  1504. if (error > 0) {
  1505. *ppos += error;
  1506. file_accessed(in);
  1507. }
  1508. return error;
  1509. }
  1510. /*
  1511. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  1512. */
  1513. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  1514. pgoff_t index, pgoff_t end, int whence)
  1515. {
  1516. struct page *page;
  1517. struct pagevec pvec;
  1518. pgoff_t indices[PAGEVEC_SIZE];
  1519. bool done = false;
  1520. int i;
  1521. pagevec_init(&pvec, 0);
  1522. pvec.nr = 1; /* start small: we may be there already */
  1523. while (!done) {
  1524. pvec.nr = find_get_entries(mapping, index,
  1525. pvec.nr, pvec.pages, indices);
  1526. if (!pvec.nr) {
  1527. if (whence == SEEK_DATA)
  1528. index = end;
  1529. break;
  1530. }
  1531. for (i = 0; i < pvec.nr; i++, index++) {
  1532. if (index < indices[i]) {
  1533. if (whence == SEEK_HOLE) {
  1534. done = true;
  1535. break;
  1536. }
  1537. index = indices[i];
  1538. }
  1539. page = pvec.pages[i];
  1540. if (page && !radix_tree_exceptional_entry(page)) {
  1541. if (!PageUptodate(page))
  1542. page = NULL;
  1543. }
  1544. if (index >= end ||
  1545. (page && whence == SEEK_DATA) ||
  1546. (!page && whence == SEEK_HOLE)) {
  1547. done = true;
  1548. break;
  1549. }
  1550. }
  1551. pagevec_remove_exceptionals(&pvec);
  1552. pagevec_release(&pvec);
  1553. pvec.nr = PAGEVEC_SIZE;
  1554. cond_resched();
  1555. }
  1556. return index;
  1557. }
  1558. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
  1559. {
  1560. struct address_space *mapping = file->f_mapping;
  1561. struct inode *inode = mapping->host;
  1562. pgoff_t start, end;
  1563. loff_t new_offset;
  1564. if (whence != SEEK_DATA && whence != SEEK_HOLE)
  1565. return generic_file_llseek_size(file, offset, whence,
  1566. MAX_LFS_FILESIZE, i_size_read(inode));
  1567. mutex_lock(&inode->i_mutex);
  1568. /* We're holding i_mutex so we can access i_size directly */
  1569. if (offset < 0)
  1570. offset = -EINVAL;
  1571. else if (offset >= inode->i_size)
  1572. offset = -ENXIO;
  1573. else {
  1574. start = offset >> PAGE_CACHE_SHIFT;
  1575. end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1576. new_offset = shmem_seek_hole_data(mapping, start, end, whence);
  1577. new_offset <<= PAGE_CACHE_SHIFT;
  1578. if (new_offset > offset) {
  1579. if (new_offset < inode->i_size)
  1580. offset = new_offset;
  1581. else if (whence == SEEK_DATA)
  1582. offset = -ENXIO;
  1583. else
  1584. offset = inode->i_size;
  1585. }
  1586. }
  1587. if (offset >= 0)
  1588. offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
  1589. mutex_unlock(&inode->i_mutex);
  1590. return offset;
  1591. }
  1592. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  1593. loff_t len)
  1594. {
  1595. struct inode *inode = file_inode(file);
  1596. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  1597. struct shmem_falloc shmem_falloc;
  1598. pgoff_t start, index, end;
  1599. int error;
  1600. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1601. return -EOPNOTSUPP;
  1602. mutex_lock(&inode->i_mutex);
  1603. if (mode & FALLOC_FL_PUNCH_HOLE) {
  1604. struct address_space *mapping = file->f_mapping;
  1605. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  1606. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  1607. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
  1608. shmem_falloc.waitq = &shmem_falloc_waitq;
  1609. shmem_falloc.start = unmap_start >> PAGE_SHIFT;
  1610. shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  1611. spin_lock(&inode->i_lock);
  1612. inode->i_private = &shmem_falloc;
  1613. spin_unlock(&inode->i_lock);
  1614. if ((u64)unmap_end > (u64)unmap_start)
  1615. unmap_mapping_range(mapping, unmap_start,
  1616. 1 + unmap_end - unmap_start, 0);
  1617. shmem_truncate_range(inode, offset, offset + len - 1);
  1618. /* No need to unmap again: hole-punching leaves COWed pages */
  1619. spin_lock(&inode->i_lock);
  1620. inode->i_private = NULL;
  1621. wake_up_all(&shmem_falloc_waitq);
  1622. spin_unlock(&inode->i_lock);
  1623. error = 0;
  1624. goto out;
  1625. }
  1626. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  1627. error = inode_newsize_ok(inode, offset + len);
  1628. if (error)
  1629. goto out;
  1630. start = offset >> PAGE_CACHE_SHIFT;
  1631. end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1632. /* Try to avoid a swapstorm if len is impossible to satisfy */
  1633. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  1634. error = -ENOSPC;
  1635. goto out;
  1636. }
  1637. shmem_falloc.waitq = NULL;
  1638. shmem_falloc.start = start;
  1639. shmem_falloc.next = start;
  1640. shmem_falloc.nr_falloced = 0;
  1641. shmem_falloc.nr_unswapped = 0;
  1642. spin_lock(&inode->i_lock);
  1643. inode->i_private = &shmem_falloc;
  1644. spin_unlock(&inode->i_lock);
  1645. for (index = start; index < end; index++) {
  1646. struct page *page;
  1647. /*
  1648. * Good, the fallocate(2) manpage permits EINTR: we may have
  1649. * been interrupted because we are using up too much memory.
  1650. */
  1651. if (signal_pending(current))
  1652. error = -EINTR;
  1653. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  1654. error = -ENOMEM;
  1655. else
  1656. error = shmem_getpage(inode, index, &page, SGP_FALLOC,
  1657. NULL);
  1658. if (error) {
  1659. /* Remove the !PageUptodate pages we added */
  1660. shmem_undo_range(inode,
  1661. (loff_t)start << PAGE_CACHE_SHIFT,
  1662. (loff_t)index << PAGE_CACHE_SHIFT, true);
  1663. goto undone;
  1664. }
  1665. /*
  1666. * Inform shmem_writepage() how far we have reached.
  1667. * No need for lock or barrier: we have the page lock.
  1668. */
  1669. shmem_falloc.next++;
  1670. if (!PageUptodate(page))
  1671. shmem_falloc.nr_falloced++;
  1672. /*
  1673. * If !PageUptodate, leave it that way so that freeable pages
  1674. * can be recognized if we need to rollback on error later.
  1675. * But set_page_dirty so that memory pressure will swap rather
  1676. * than free the pages we are allocating (and SGP_CACHE pages
  1677. * might still be clean: we now need to mark those dirty too).
  1678. */
  1679. set_page_dirty(page);
  1680. unlock_page(page);
  1681. page_cache_release(page);
  1682. cond_resched();
  1683. }
  1684. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  1685. i_size_write(inode, offset + len);
  1686. inode->i_ctime = CURRENT_TIME;
  1687. undone:
  1688. spin_lock(&inode->i_lock);
  1689. inode->i_private = NULL;
  1690. spin_unlock(&inode->i_lock);
  1691. out:
  1692. mutex_unlock(&inode->i_mutex);
  1693. return error;
  1694. }
  1695. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1696. {
  1697. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1698. buf->f_type = TMPFS_MAGIC;
  1699. buf->f_bsize = PAGE_CACHE_SIZE;
  1700. buf->f_namelen = NAME_MAX;
  1701. if (sbinfo->max_blocks) {
  1702. buf->f_blocks = sbinfo->max_blocks;
  1703. buf->f_bavail =
  1704. buf->f_bfree = sbinfo->max_blocks -
  1705. percpu_counter_sum(&sbinfo->used_blocks);
  1706. }
  1707. if (sbinfo->max_inodes) {
  1708. buf->f_files = sbinfo->max_inodes;
  1709. buf->f_ffree = sbinfo->free_inodes;
  1710. }
  1711. /* else leave those fields 0 like simple_statfs */
  1712. return 0;
  1713. }
  1714. /*
  1715. * File creation. Allocate an inode, and we're done..
  1716. */
  1717. static int
  1718. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  1719. {
  1720. struct inode *inode;
  1721. int error = -ENOSPC;
  1722. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1723. if (inode) {
  1724. error = simple_acl_create(dir, inode);
  1725. if (error)
  1726. goto out_iput;
  1727. error = security_inode_init_security(inode, dir,
  1728. &dentry->d_name,
  1729. shmem_initxattrs, NULL);
  1730. if (error && error != -EOPNOTSUPP)
  1731. goto out_iput;
  1732. error = 0;
  1733. dir->i_size += BOGO_DIRENT_SIZE;
  1734. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1735. d_instantiate(dentry, inode);
  1736. dget(dentry); /* Extra count - pin the dentry in core */
  1737. }
  1738. return error;
  1739. out_iput:
  1740. iput(inode);
  1741. return error;
  1742. }
  1743. static int
  1744. shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  1745. {
  1746. struct inode *inode;
  1747. int error = -ENOSPC;
  1748. inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
  1749. if (inode) {
  1750. error = security_inode_init_security(inode, dir,
  1751. NULL,
  1752. shmem_initxattrs, NULL);
  1753. if (error && error != -EOPNOTSUPP)
  1754. goto out_iput;
  1755. error = simple_acl_create(dir, inode);
  1756. if (error)
  1757. goto out_iput;
  1758. d_tmpfile(dentry, inode);
  1759. }
  1760. return error;
  1761. out_iput:
  1762. iput(inode);
  1763. return error;
  1764. }
  1765. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  1766. {
  1767. int error;
  1768. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1769. return error;
  1770. inc_nlink(dir);
  1771. return 0;
  1772. }
  1773. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1774. bool excl)
  1775. {
  1776. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1777. }
  1778. /*
  1779. * Link a file..
  1780. */
  1781. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1782. {
  1783. struct inode *inode = old_dentry->d_inode;
  1784. int ret;
  1785. /*
  1786. * No ordinary (disk based) filesystem counts links as inodes;
  1787. * but each new link needs a new dentry, pinning lowmem, and
  1788. * tmpfs dentries cannot be pruned until they are unlinked.
  1789. */
  1790. ret = shmem_reserve_inode(inode->i_sb);
  1791. if (ret)
  1792. goto out;
  1793. dir->i_size += BOGO_DIRENT_SIZE;
  1794. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1795. inc_nlink(inode);
  1796. ihold(inode); /* New dentry reference */
  1797. dget(dentry); /* Extra pinning count for the created dentry */
  1798. d_instantiate(dentry, inode);
  1799. out:
  1800. return ret;
  1801. }
  1802. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1803. {
  1804. struct inode *inode = dentry->d_inode;
  1805. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1806. shmem_free_inode(inode->i_sb);
  1807. dir->i_size -= BOGO_DIRENT_SIZE;
  1808. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1809. drop_nlink(inode);
  1810. dput(dentry); /* Undo the count from "create" - this does all the work */
  1811. return 0;
  1812. }
  1813. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1814. {
  1815. if (!simple_empty(dentry))
  1816. return -ENOTEMPTY;
  1817. drop_nlink(dentry->d_inode);
  1818. drop_nlink(dir);
  1819. return shmem_unlink(dir, dentry);
  1820. }
  1821. /*
  1822. * The VFS layer already does all the dentry stuff for rename,
  1823. * we just have to decrement the usage count for the target if
  1824. * it exists so that the VFS layer correctly free's it when it
  1825. * gets overwritten.
  1826. */
  1827. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1828. {
  1829. struct inode *inode = old_dentry->d_inode;
  1830. int they_are_dirs = S_ISDIR(inode->i_mode);
  1831. if (!simple_empty(new_dentry))
  1832. return -ENOTEMPTY;
  1833. if (new_dentry->d_inode) {
  1834. (void) shmem_unlink(new_dir, new_dentry);
  1835. if (they_are_dirs)
  1836. drop_nlink(old_dir);
  1837. } else if (they_are_dirs) {
  1838. drop_nlink(old_dir);
  1839. inc_nlink(new_dir);
  1840. }
  1841. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1842. new_dir->i_size += BOGO_DIRENT_SIZE;
  1843. old_dir->i_ctime = old_dir->i_mtime =
  1844. new_dir->i_ctime = new_dir->i_mtime =
  1845. inode->i_ctime = CURRENT_TIME;
  1846. return 0;
  1847. }
  1848. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1849. {
  1850. int error;
  1851. int len;
  1852. struct inode *inode;
  1853. struct page *page;
  1854. char *kaddr;
  1855. struct shmem_inode_info *info;
  1856. len = strlen(symname) + 1;
  1857. if (len > PAGE_CACHE_SIZE)
  1858. return -ENAMETOOLONG;
  1859. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1860. if (!inode)
  1861. return -ENOSPC;
  1862. error = security_inode_init_security(inode, dir, &dentry->d_name,
  1863. shmem_initxattrs, NULL);
  1864. if (error) {
  1865. if (error != -EOPNOTSUPP) {
  1866. iput(inode);
  1867. return error;
  1868. }
  1869. error = 0;
  1870. }
  1871. info = SHMEM_I(inode);
  1872. inode->i_size = len-1;
  1873. if (len <= SHORT_SYMLINK_LEN) {
  1874. info->symlink = kmemdup(symname, len, GFP_KERNEL);
  1875. if (!info->symlink) {
  1876. iput(inode);
  1877. return -ENOMEM;
  1878. }
  1879. inode->i_op = &shmem_short_symlink_operations;
  1880. } else {
  1881. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1882. if (error) {
  1883. iput(inode);
  1884. return error;
  1885. }
  1886. inode->i_mapping->a_ops = &shmem_aops;
  1887. inode->i_op = &shmem_symlink_inode_operations;
  1888. kaddr = kmap_atomic(page);
  1889. memcpy(kaddr, symname, len);
  1890. kunmap_atomic(kaddr);
  1891. SetPageUptodate(page);
  1892. set_page_dirty(page);
  1893. unlock_page(page);
  1894. page_cache_release(page);
  1895. }
  1896. dir->i_size += BOGO_DIRENT_SIZE;
  1897. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1898. d_instantiate(dentry, inode);
  1899. dget(dentry);
  1900. return 0;
  1901. }
  1902. static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
  1903. {
  1904. nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
  1905. return NULL;
  1906. }
  1907. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1908. {
  1909. struct page *page = NULL;
  1910. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1911. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1912. if (page)
  1913. unlock_page(page);
  1914. return page;
  1915. }
  1916. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1917. {
  1918. if (!IS_ERR(nd_get_link(nd))) {
  1919. struct page *page = cookie;
  1920. kunmap(page);
  1921. mark_page_accessed(page);
  1922. page_cache_release(page);
  1923. }
  1924. }
  1925. #ifdef CONFIG_TMPFS_XATTR
  1926. /*
  1927. * Superblocks without xattr inode operations may get some security.* xattr
  1928. * support from the LSM "for free". As soon as we have any other xattrs
  1929. * like ACLs, we also need to implement the security.* handlers at
  1930. * filesystem level, though.
  1931. */
  1932. /*
  1933. * Callback for security_inode_init_security() for acquiring xattrs.
  1934. */
  1935. static int shmem_initxattrs(struct inode *inode,
  1936. const struct xattr *xattr_array,
  1937. void *fs_info)
  1938. {
  1939. struct shmem_inode_info *info = SHMEM_I(inode);
  1940. const struct xattr *xattr;
  1941. struct simple_xattr *new_xattr;
  1942. size_t len;
  1943. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  1944. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  1945. if (!new_xattr)
  1946. return -ENOMEM;
  1947. len = strlen(xattr->name) + 1;
  1948. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  1949. GFP_KERNEL);
  1950. if (!new_xattr->name) {
  1951. kfree(new_xattr);
  1952. return -ENOMEM;
  1953. }
  1954. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  1955. XATTR_SECURITY_PREFIX_LEN);
  1956. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  1957. xattr->name, len);
  1958. simple_xattr_list_add(&info->xattrs, new_xattr);
  1959. }
  1960. return 0;
  1961. }
  1962. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1963. #ifdef CONFIG_TMPFS_POSIX_ACL
  1964. &posix_acl_access_xattr_handler,
  1965. &posix_acl_default_xattr_handler,
  1966. #endif
  1967. NULL
  1968. };
  1969. static int shmem_xattr_validate(const char *name)
  1970. {
  1971. struct { const char *prefix; size_t len; } arr[] = {
  1972. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1973. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1974. };
  1975. int i;
  1976. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1977. size_t preflen = arr[i].len;
  1978. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1979. if (!name[preflen])
  1980. return -EINVAL;
  1981. return 0;
  1982. }
  1983. }
  1984. return -EOPNOTSUPP;
  1985. }
  1986. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1987. void *buffer, size_t size)
  1988. {
  1989. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  1990. int err;
  1991. /*
  1992. * If this is a request for a synthetic attribute in the system.*
  1993. * namespace use the generic infrastructure to resolve a handler
  1994. * for it via sb->s_xattr.
  1995. */
  1996. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1997. return generic_getxattr(dentry, name, buffer, size);
  1998. err = shmem_xattr_validate(name);
  1999. if (err)
  2000. return err;
  2001. return simple_xattr_get(&info->xattrs, name, buffer, size);
  2002. }
  2003. static int shmem_setxattr(struct dentry *dentry, const char *name,
  2004. const void *value, size_t size, int flags)
  2005. {
  2006. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2007. int err;
  2008. /*
  2009. * If this is a request for a synthetic attribute in the system.*
  2010. * namespace use the generic infrastructure to resolve a handler
  2011. * for it via sb->s_xattr.
  2012. */
  2013. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2014. return generic_setxattr(dentry, name, value, size, flags);
  2015. err = shmem_xattr_validate(name);
  2016. if (err)
  2017. return err;
  2018. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  2019. }
  2020. static int shmem_removexattr(struct dentry *dentry, const char *name)
  2021. {
  2022. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2023. int err;
  2024. /*
  2025. * If this is a request for a synthetic attribute in the system.*
  2026. * namespace use the generic infrastructure to resolve a handler
  2027. * for it via sb->s_xattr.
  2028. */
  2029. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  2030. return generic_removexattr(dentry, name);
  2031. err = shmem_xattr_validate(name);
  2032. if (err)
  2033. return err;
  2034. return simple_xattr_remove(&info->xattrs, name);
  2035. }
  2036. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2037. {
  2038. struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
  2039. return simple_xattr_list(&info->xattrs, buffer, size);
  2040. }
  2041. #endif /* CONFIG_TMPFS_XATTR */
  2042. static const struct inode_operations shmem_short_symlink_operations = {
  2043. .readlink = generic_readlink,
  2044. .follow_link = shmem_follow_short_symlink,
  2045. #ifdef CONFIG_TMPFS_XATTR
  2046. .setxattr = shmem_setxattr,
  2047. .getxattr = shmem_getxattr,
  2048. .listxattr = shmem_listxattr,
  2049. .removexattr = shmem_removexattr,
  2050. #endif
  2051. };
  2052. static const struct inode_operations shmem_symlink_inode_operations = {
  2053. .readlink = generic_readlink,
  2054. .follow_link = shmem_follow_link,
  2055. .put_link = shmem_put_link,
  2056. #ifdef CONFIG_TMPFS_XATTR
  2057. .setxattr = shmem_setxattr,
  2058. .getxattr = shmem_getxattr,
  2059. .listxattr = shmem_listxattr,
  2060. .removexattr = shmem_removexattr,
  2061. #endif
  2062. };
  2063. static struct dentry *shmem_get_parent(struct dentry *child)
  2064. {
  2065. return ERR_PTR(-ESTALE);
  2066. }
  2067. static int shmem_match(struct inode *ino, void *vfh)
  2068. {
  2069. __u32 *fh = vfh;
  2070. __u64 inum = fh[2];
  2071. inum = (inum << 32) | fh[1];
  2072. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2073. }
  2074. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2075. struct fid *fid, int fh_len, int fh_type)
  2076. {
  2077. struct inode *inode;
  2078. struct dentry *dentry = NULL;
  2079. u64 inum;
  2080. if (fh_len < 3)
  2081. return NULL;
  2082. inum = fid->raw[2];
  2083. inum = (inum << 32) | fid->raw[1];
  2084. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2085. shmem_match, fid->raw);
  2086. if (inode) {
  2087. dentry = d_find_alias(inode);
  2088. iput(inode);
  2089. }
  2090. return dentry;
  2091. }
  2092. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  2093. struct inode *parent)
  2094. {
  2095. if (*len < 3) {
  2096. *len = 3;
  2097. return FILEID_INVALID;
  2098. }
  2099. if (inode_unhashed(inode)) {
  2100. /* Unfortunately insert_inode_hash is not idempotent,
  2101. * so as we hash inodes here rather than at creation
  2102. * time, we need a lock to ensure we only try
  2103. * to do it once
  2104. */
  2105. static DEFINE_SPINLOCK(lock);
  2106. spin_lock(&lock);
  2107. if (inode_unhashed(inode))
  2108. __insert_inode_hash(inode,
  2109. inode->i_ino + inode->i_generation);
  2110. spin_unlock(&lock);
  2111. }
  2112. fh[0] = inode->i_generation;
  2113. fh[1] = inode->i_ino;
  2114. fh[2] = ((__u64)inode->i_ino) >> 32;
  2115. *len = 3;
  2116. return 1;
  2117. }
  2118. static const struct export_operations shmem_export_ops = {
  2119. .get_parent = shmem_get_parent,
  2120. .encode_fh = shmem_encode_fh,
  2121. .fh_to_dentry = shmem_fh_to_dentry,
  2122. };
  2123. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2124. bool remount)
  2125. {
  2126. char *this_char, *value, *rest;
  2127. struct mempolicy *mpol = NULL;
  2128. uid_t uid;
  2129. gid_t gid;
  2130. while (options != NULL) {
  2131. this_char = options;
  2132. for (;;) {
  2133. /*
  2134. * NUL-terminate this option: unfortunately,
  2135. * mount options form a comma-separated list,
  2136. * but mpol's nodelist may also contain commas.
  2137. */
  2138. options = strchr(options, ',');
  2139. if (options == NULL)
  2140. break;
  2141. options++;
  2142. if (!isdigit(*options)) {
  2143. options[-1] = '\0';
  2144. break;
  2145. }
  2146. }
  2147. if (!*this_char)
  2148. continue;
  2149. if ((value = strchr(this_char,'=')) != NULL) {
  2150. *value++ = 0;
  2151. } else {
  2152. printk(KERN_ERR
  2153. "tmpfs: No value for mount option '%s'\n",
  2154. this_char);
  2155. goto error;
  2156. }
  2157. if (!strcmp(this_char,"size")) {
  2158. unsigned long long size;
  2159. size = memparse(value,&rest);
  2160. if (*rest == '%') {
  2161. size <<= PAGE_SHIFT;
  2162. size *= totalram_pages;
  2163. do_div(size, 100);
  2164. rest++;
  2165. }
  2166. if (*rest)
  2167. goto bad_val;
  2168. sbinfo->max_blocks =
  2169. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  2170. } else if (!strcmp(this_char,"nr_blocks")) {
  2171. sbinfo->max_blocks = memparse(value, &rest);
  2172. if (*rest)
  2173. goto bad_val;
  2174. } else if (!strcmp(this_char,"nr_inodes")) {
  2175. sbinfo->max_inodes = memparse(value, &rest);
  2176. if (*rest)
  2177. goto bad_val;
  2178. } else if (!strcmp(this_char,"mode")) {
  2179. if (remount)
  2180. continue;
  2181. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2182. if (*rest)
  2183. goto bad_val;
  2184. } else if (!strcmp(this_char,"uid")) {
  2185. if (remount)
  2186. continue;
  2187. uid = simple_strtoul(value, &rest, 0);
  2188. if (*rest)
  2189. goto bad_val;
  2190. sbinfo->uid = make_kuid(current_user_ns(), uid);
  2191. if (!uid_valid(sbinfo->uid))
  2192. goto bad_val;
  2193. } else if (!strcmp(this_char,"gid")) {
  2194. if (remount)
  2195. continue;
  2196. gid = simple_strtoul(value, &rest, 0);
  2197. if (*rest)
  2198. goto bad_val;
  2199. sbinfo->gid = make_kgid(current_user_ns(), gid);
  2200. if (!gid_valid(sbinfo->gid))
  2201. goto bad_val;
  2202. } else if (!strcmp(this_char,"mpol")) {
  2203. mpol_put(mpol);
  2204. mpol = NULL;
  2205. if (mpol_parse_str(value, &mpol))
  2206. goto bad_val;
  2207. } else {
  2208. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  2209. this_char);
  2210. goto error;
  2211. }
  2212. }
  2213. sbinfo->mpol = mpol;
  2214. return 0;
  2215. bad_val:
  2216. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  2217. value, this_char);
  2218. error:
  2219. mpol_put(mpol);
  2220. return 1;
  2221. }
  2222. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  2223. {
  2224. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2225. struct shmem_sb_info config = *sbinfo;
  2226. unsigned long inodes;
  2227. int error = -EINVAL;
  2228. config.mpol = NULL;
  2229. if (shmem_parse_options(data, &config, true))
  2230. return error;
  2231. spin_lock(&sbinfo->stat_lock);
  2232. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  2233. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  2234. goto out;
  2235. if (config.max_inodes < inodes)
  2236. goto out;
  2237. /*
  2238. * Those tests disallow limited->unlimited while any are in use;
  2239. * but we must separately disallow unlimited->limited, because
  2240. * in that case we have no record of how much is already in use.
  2241. */
  2242. if (config.max_blocks && !sbinfo->max_blocks)
  2243. goto out;
  2244. if (config.max_inodes && !sbinfo->max_inodes)
  2245. goto out;
  2246. error = 0;
  2247. sbinfo->max_blocks = config.max_blocks;
  2248. sbinfo->max_inodes = config.max_inodes;
  2249. sbinfo->free_inodes = config.max_inodes - inodes;
  2250. /*
  2251. * Preserve previous mempolicy unless mpol remount option was specified.
  2252. */
  2253. if (config.mpol) {
  2254. mpol_put(sbinfo->mpol);
  2255. sbinfo->mpol = config.mpol; /* transfers initial ref */
  2256. }
  2257. out:
  2258. spin_unlock(&sbinfo->stat_lock);
  2259. return error;
  2260. }
  2261. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  2262. {
  2263. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  2264. if (sbinfo->max_blocks != shmem_default_max_blocks())
  2265. seq_printf(seq, ",size=%luk",
  2266. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  2267. if (sbinfo->max_inodes != shmem_default_max_inodes())
  2268. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  2269. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  2270. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  2271. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  2272. seq_printf(seq, ",uid=%u",
  2273. from_kuid_munged(&init_user_ns, sbinfo->uid));
  2274. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  2275. seq_printf(seq, ",gid=%u",
  2276. from_kgid_munged(&init_user_ns, sbinfo->gid));
  2277. shmem_show_mpol(seq, sbinfo->mpol);
  2278. return 0;
  2279. }
  2280. #endif /* CONFIG_TMPFS */
  2281. static void shmem_put_super(struct super_block *sb)
  2282. {
  2283. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  2284. percpu_counter_destroy(&sbinfo->used_blocks);
  2285. mpol_put(sbinfo->mpol);
  2286. kfree(sbinfo);
  2287. sb->s_fs_info = NULL;
  2288. }
  2289. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  2290. {
  2291. struct inode *inode;
  2292. struct shmem_sb_info *sbinfo;
  2293. int err = -ENOMEM;
  2294. /* Round up to L1_CACHE_BYTES to resist false sharing */
  2295. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  2296. L1_CACHE_BYTES), GFP_KERNEL);
  2297. if (!sbinfo)
  2298. return -ENOMEM;
  2299. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  2300. sbinfo->uid = current_fsuid();
  2301. sbinfo->gid = current_fsgid();
  2302. sb->s_fs_info = sbinfo;
  2303. #ifdef CONFIG_TMPFS
  2304. /*
  2305. * Per default we only allow half of the physical ram per
  2306. * tmpfs instance, limiting inodes to one per page of lowmem;
  2307. * but the internal instance is left unlimited.
  2308. */
  2309. if (!(sb->s_flags & MS_KERNMOUNT)) {
  2310. sbinfo->max_blocks = shmem_default_max_blocks();
  2311. sbinfo->max_inodes = shmem_default_max_inodes();
  2312. if (shmem_parse_options(data, sbinfo, false)) {
  2313. err = -EINVAL;
  2314. goto failed;
  2315. }
  2316. } else {
  2317. sb->s_flags |= MS_NOUSER;
  2318. }
  2319. sb->s_export_op = &shmem_export_ops;
  2320. sb->s_flags |= MS_NOSEC;
  2321. #else
  2322. sb->s_flags |= MS_NOUSER;
  2323. #endif
  2324. spin_lock_init(&sbinfo->stat_lock);
  2325. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  2326. goto failed;
  2327. sbinfo->free_inodes = sbinfo->max_inodes;
  2328. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2329. sb->s_blocksize = PAGE_CACHE_SIZE;
  2330. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  2331. sb->s_magic = TMPFS_MAGIC;
  2332. sb->s_op = &shmem_ops;
  2333. sb->s_time_gran = 1;
  2334. #ifdef CONFIG_TMPFS_XATTR
  2335. sb->s_xattr = shmem_xattr_handlers;
  2336. #endif
  2337. #ifdef CONFIG_TMPFS_POSIX_ACL
  2338. sb->s_flags |= MS_POSIXACL;
  2339. #endif
  2340. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2341. if (!inode)
  2342. goto failed;
  2343. inode->i_uid = sbinfo->uid;
  2344. inode->i_gid = sbinfo->gid;
  2345. sb->s_root = d_make_root(inode);
  2346. if (!sb->s_root)
  2347. goto failed;
  2348. return 0;
  2349. failed:
  2350. shmem_put_super(sb);
  2351. return err;
  2352. }
  2353. static struct kmem_cache *shmem_inode_cachep;
  2354. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2355. {
  2356. struct shmem_inode_info *info;
  2357. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2358. if (!info)
  2359. return NULL;
  2360. return &info->vfs_inode;
  2361. }
  2362. static void shmem_destroy_callback(struct rcu_head *head)
  2363. {
  2364. struct inode *inode = container_of(head, struct inode, i_rcu);
  2365. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2366. }
  2367. static void shmem_destroy_inode(struct inode *inode)
  2368. {
  2369. if (S_ISREG(inode->i_mode))
  2370. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2371. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2372. }
  2373. static void shmem_init_inode(void *foo)
  2374. {
  2375. struct shmem_inode_info *info = foo;
  2376. inode_init_once(&info->vfs_inode);
  2377. }
  2378. static int shmem_init_inodecache(void)
  2379. {
  2380. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2381. sizeof(struct shmem_inode_info),
  2382. 0, SLAB_PANIC, shmem_init_inode);
  2383. return 0;
  2384. }
  2385. static void shmem_destroy_inodecache(void)
  2386. {
  2387. kmem_cache_destroy(shmem_inode_cachep);
  2388. }
  2389. static const struct address_space_operations shmem_aops = {
  2390. .writepage = shmem_writepage,
  2391. .set_page_dirty = __set_page_dirty_no_writeback,
  2392. #ifdef CONFIG_TMPFS
  2393. .write_begin = shmem_write_begin,
  2394. .write_end = shmem_write_end,
  2395. #endif
  2396. .migratepage = migrate_page,
  2397. .error_remove_page = generic_error_remove_page,
  2398. };
  2399. static const struct file_operations shmem_file_operations = {
  2400. .mmap = shmem_mmap,
  2401. #ifdef CONFIG_TMPFS
  2402. .llseek = shmem_file_llseek,
  2403. .read = new_sync_read,
  2404. .write = new_sync_write,
  2405. .read_iter = shmem_file_read_iter,
  2406. .write_iter = generic_file_write_iter,
  2407. .fsync = noop_fsync,
  2408. .splice_read = shmem_file_splice_read,
  2409. .splice_write = iter_file_splice_write,
  2410. .fallocate = shmem_fallocate,
  2411. #endif
  2412. };
  2413. static const struct inode_operations shmem_inode_operations = {
  2414. .setattr = shmem_setattr,
  2415. #ifdef CONFIG_TMPFS_XATTR
  2416. .setxattr = shmem_setxattr,
  2417. .getxattr = shmem_getxattr,
  2418. .listxattr = shmem_listxattr,
  2419. .removexattr = shmem_removexattr,
  2420. .set_acl = simple_set_acl,
  2421. #endif
  2422. };
  2423. static const struct inode_operations shmem_dir_inode_operations = {
  2424. #ifdef CONFIG_TMPFS
  2425. .create = shmem_create,
  2426. .lookup = simple_lookup,
  2427. .link = shmem_link,
  2428. .unlink = shmem_unlink,
  2429. .symlink = shmem_symlink,
  2430. .mkdir = shmem_mkdir,
  2431. .rmdir = shmem_rmdir,
  2432. .mknod = shmem_mknod,
  2433. .rename = shmem_rename,
  2434. .tmpfile = shmem_tmpfile,
  2435. #endif
  2436. #ifdef CONFIG_TMPFS_XATTR
  2437. .setxattr = shmem_setxattr,
  2438. .getxattr = shmem_getxattr,
  2439. .listxattr = shmem_listxattr,
  2440. .removexattr = shmem_removexattr,
  2441. #endif
  2442. #ifdef CONFIG_TMPFS_POSIX_ACL
  2443. .setattr = shmem_setattr,
  2444. .set_acl = simple_set_acl,
  2445. #endif
  2446. };
  2447. static const struct inode_operations shmem_special_inode_operations = {
  2448. #ifdef CONFIG_TMPFS_XATTR
  2449. .setxattr = shmem_setxattr,
  2450. .getxattr = shmem_getxattr,
  2451. .listxattr = shmem_listxattr,
  2452. .removexattr = shmem_removexattr,
  2453. #endif
  2454. #ifdef CONFIG_TMPFS_POSIX_ACL
  2455. .setattr = shmem_setattr,
  2456. .set_acl = simple_set_acl,
  2457. #endif
  2458. };
  2459. static const struct super_operations shmem_ops = {
  2460. .alloc_inode = shmem_alloc_inode,
  2461. .destroy_inode = shmem_destroy_inode,
  2462. #ifdef CONFIG_TMPFS
  2463. .statfs = shmem_statfs,
  2464. .remount_fs = shmem_remount_fs,
  2465. .show_options = shmem_show_options,
  2466. #endif
  2467. .evict_inode = shmem_evict_inode,
  2468. .drop_inode = generic_delete_inode,
  2469. .put_super = shmem_put_super,
  2470. };
  2471. static const struct vm_operations_struct shmem_vm_ops = {
  2472. .fault = shmem_fault,
  2473. .map_pages = filemap_map_pages,
  2474. #ifdef CONFIG_NUMA
  2475. .set_policy = shmem_set_policy,
  2476. .get_policy = shmem_get_policy,
  2477. #endif
  2478. .remap_pages = generic_file_remap_pages,
  2479. };
  2480. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2481. int flags, const char *dev_name, void *data)
  2482. {
  2483. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2484. }
  2485. static struct file_system_type shmem_fs_type = {
  2486. .owner = THIS_MODULE,
  2487. .name = "tmpfs",
  2488. .mount = shmem_mount,
  2489. .kill_sb = kill_litter_super,
  2490. .fs_flags = FS_USERNS_MOUNT,
  2491. };
  2492. int __init shmem_init(void)
  2493. {
  2494. int error;
  2495. /* If rootfs called this, don't re-init */
  2496. if (shmem_inode_cachep)
  2497. return 0;
  2498. error = bdi_init(&shmem_backing_dev_info);
  2499. if (error)
  2500. goto out4;
  2501. error = shmem_init_inodecache();
  2502. if (error)
  2503. goto out3;
  2504. error = register_filesystem(&shmem_fs_type);
  2505. if (error) {
  2506. printk(KERN_ERR "Could not register tmpfs\n");
  2507. goto out2;
  2508. }
  2509. shm_mnt = kern_mount(&shmem_fs_type);
  2510. if (IS_ERR(shm_mnt)) {
  2511. error = PTR_ERR(shm_mnt);
  2512. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2513. goto out1;
  2514. }
  2515. return 0;
  2516. out1:
  2517. unregister_filesystem(&shmem_fs_type);
  2518. out2:
  2519. shmem_destroy_inodecache();
  2520. out3:
  2521. bdi_destroy(&shmem_backing_dev_info);
  2522. out4:
  2523. shm_mnt = ERR_PTR(error);
  2524. return error;
  2525. }
  2526. #else /* !CONFIG_SHMEM */
  2527. /*
  2528. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2529. *
  2530. * This is intended for small system where the benefits of the full
  2531. * shmem code (swap-backed and resource-limited) are outweighed by
  2532. * their complexity. On systems without swap this code should be
  2533. * effectively equivalent, but much lighter weight.
  2534. */
  2535. static struct file_system_type shmem_fs_type = {
  2536. .name = "tmpfs",
  2537. .mount = ramfs_mount,
  2538. .kill_sb = kill_litter_super,
  2539. .fs_flags = FS_USERNS_MOUNT,
  2540. };
  2541. int __init shmem_init(void)
  2542. {
  2543. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2544. shm_mnt = kern_mount(&shmem_fs_type);
  2545. BUG_ON(IS_ERR(shm_mnt));
  2546. return 0;
  2547. }
  2548. int shmem_unuse(swp_entry_t swap, struct page *page)
  2549. {
  2550. return 0;
  2551. }
  2552. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2553. {
  2554. return 0;
  2555. }
  2556. void shmem_unlock_mapping(struct address_space *mapping)
  2557. {
  2558. }
  2559. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2560. {
  2561. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2562. }
  2563. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2564. #define shmem_vm_ops generic_file_vm_ops
  2565. #define shmem_file_operations ramfs_file_operations
  2566. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2567. #define shmem_acct_size(flags, size) 0
  2568. #define shmem_unacct_size(flags, size) do {} while (0)
  2569. #endif /* CONFIG_SHMEM */
  2570. /* common code */
  2571. static struct dentry_operations anon_ops = {
  2572. .d_dname = simple_dname
  2573. };
  2574. static struct file *__shmem_file_setup(const char *name, loff_t size,
  2575. unsigned long flags, unsigned int i_flags)
  2576. {
  2577. struct file *res;
  2578. struct inode *inode;
  2579. struct path path;
  2580. struct super_block *sb;
  2581. struct qstr this;
  2582. if (IS_ERR(shm_mnt))
  2583. return ERR_CAST(shm_mnt);
  2584. if (size < 0 || size > MAX_LFS_FILESIZE)
  2585. return ERR_PTR(-EINVAL);
  2586. if (shmem_acct_size(flags, size))
  2587. return ERR_PTR(-ENOMEM);
  2588. res = ERR_PTR(-ENOMEM);
  2589. this.name = name;
  2590. this.len = strlen(name);
  2591. this.hash = 0; /* will go */
  2592. sb = shm_mnt->mnt_sb;
  2593. path.dentry = d_alloc_pseudo(sb, &this);
  2594. if (!path.dentry)
  2595. goto put_memory;
  2596. d_set_d_op(path.dentry, &anon_ops);
  2597. path.mnt = mntget(shm_mnt);
  2598. res = ERR_PTR(-ENOSPC);
  2599. inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2600. if (!inode)
  2601. goto put_dentry;
  2602. inode->i_flags |= i_flags;
  2603. d_instantiate(path.dentry, inode);
  2604. inode->i_size = size;
  2605. clear_nlink(inode); /* It is unlinked */
  2606. res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
  2607. if (IS_ERR(res))
  2608. goto put_dentry;
  2609. res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2610. &shmem_file_operations);
  2611. if (IS_ERR(res))
  2612. goto put_dentry;
  2613. return res;
  2614. put_dentry:
  2615. path_put(&path);
  2616. put_memory:
  2617. shmem_unacct_size(flags, size);
  2618. return res;
  2619. }
  2620. /**
  2621. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  2622. * kernel internal. There will be NO LSM permission checks against the
  2623. * underlying inode. So users of this interface must do LSM checks at a
  2624. * higher layer. The one user is the big_key implementation. LSM checks
  2625. * are provided at the key level rather than the inode level.
  2626. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2627. * @size: size to be set for the file
  2628. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2629. */
  2630. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
  2631. {
  2632. return __shmem_file_setup(name, size, flags, S_PRIVATE);
  2633. }
  2634. /**
  2635. * shmem_file_setup - get an unlinked file living in tmpfs
  2636. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2637. * @size: size to be set for the file
  2638. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2639. */
  2640. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2641. {
  2642. return __shmem_file_setup(name, size, flags, 0);
  2643. }
  2644. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2645. /**
  2646. * shmem_zero_setup - setup a shared anonymous mapping
  2647. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2648. */
  2649. int shmem_zero_setup(struct vm_area_struct *vma)
  2650. {
  2651. struct file *file;
  2652. loff_t size = vma->vm_end - vma->vm_start;
  2653. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2654. if (IS_ERR(file))
  2655. return PTR_ERR(file);
  2656. if (vma->vm_file)
  2657. fput(vma->vm_file);
  2658. vma->vm_file = file;
  2659. vma->vm_ops = &shmem_vm_ops;
  2660. return 0;
  2661. }
  2662. /**
  2663. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2664. * @mapping: the page's address_space
  2665. * @index: the page index
  2666. * @gfp: the page allocator flags to use if allocating
  2667. *
  2668. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2669. * with any new page allocations done using the specified allocation flags.
  2670. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2671. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2672. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2673. *
  2674. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2675. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2676. */
  2677. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2678. pgoff_t index, gfp_t gfp)
  2679. {
  2680. #ifdef CONFIG_SHMEM
  2681. struct inode *inode = mapping->host;
  2682. struct page *page;
  2683. int error;
  2684. BUG_ON(mapping->a_ops != &shmem_aops);
  2685. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2686. if (error)
  2687. page = ERR_PTR(error);
  2688. else
  2689. unlock_page(page);
  2690. return page;
  2691. #else
  2692. /*
  2693. * The tiny !SHMEM case uses ramfs without swap
  2694. */
  2695. return read_cache_page_gfp(mapping, index, gfp);
  2696. #endif
  2697. }
  2698. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);