shmem.c 76 KB

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