shmem.c 77 KB

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