shmem.c 71 KB

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